]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.acorn/src/org/simantics/acorn/internal/BijectionMap.java
Merge commit '0e8cf1e'
[simantics/platform.git] / bundles / org.simantics.acorn / src / org / simantics / acorn / internal / BijectionMap.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 /*
13  * Created on Jan 21, 2005
14  * 
15  * Copyright Toni Kalajainen
16  * 
17  * Licensed under the Apache License, Version 2.0 (the "License");
18  * you may not use this file except in compliance with the License.
19  * You may obtain a copy of the License at
20  *
21  *     http://www.apache.org/licenses/LICENSE-2.0
22  *
23  * Unless required by applicable law or agreed to in writing, software
24  * distributed under the License is distributed on an "AS IS" BASIS,
25  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26  * See the License for the specific language governing permissions and
27  * limitations under the License.
28  */
29 package org.simantics.acorn.internal;
30
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.Set;
34
35 /**
36  * Bijection map is a Map that has no values or keys, only 1:1 mappings
37  * of values. These value/keys will be called with left and right side
38  * values.
39  * 
40  * Each value can exist only once on a side
41  * 
42  * @author Toni Kalajainen
43  */
44 public class BijectionMap<L, R> {
45
46     /** The keys of tableLeft are left-side-values and
47      * values are right-side-values */
48     private final Map<L, R> tableLeft = new HashMap<L, R>();
49     /** The keys of tableRight are right-side-values and
50      * values on it are left-side-values */
51     private final Map<R, L> tableRight = new HashMap<R, L>();
52
53     public boolean containsLeft(L leftValue)
54     {
55         return tableLeft.containsKey(leftValue);
56     }
57
58     public boolean containsRight(R rightValue)
59     {
60         return tableRight.containsKey(rightValue);
61     }
62
63     public void map(L leftValue, R rightValue)
64     {
65         // Remove possible old mapping
66         R oldRight = tableLeft.remove(leftValue);
67         if (oldRight != null) {
68             tableRight.remove(oldRight);
69         } else {
70             L oldLeft = tableRight.remove(rightValue);
71             if (oldLeft != null) {
72                 tableLeft.remove(oldLeft);
73             }
74         }
75
76         tableLeft.put(leftValue, rightValue);
77         tableRight.put(rightValue, leftValue);
78     }
79
80     public int size()
81     {
82         return tableLeft.size();
83     }
84
85     public L getLeft(R rightValue) {
86         return tableRight.get(rightValue);
87     }
88
89     public R getRight(L leftValue) {
90         return tableLeft.get(leftValue);
91     }
92
93     public R removeWithLeft(L leftValue) {
94         R rightValue = tableLeft.remove(leftValue);
95         if (rightValue!=null)
96             tableRight.remove(rightValue);
97         return rightValue;
98     }
99
100     public L removeWithRight(R rightValue) {
101         L leftValue = tableRight.remove(rightValue);
102         if (leftValue!=null)
103             tableLeft.remove(leftValue);
104         return leftValue;
105     }
106
107     public Set<L> getLeftSet() {
108         return tableLeft.keySet();
109     }
110
111     public Set<R> getRightSet() {
112         return tableRight.keySet();
113     }
114
115     public void clear() {
116         tableLeft.clear();
117         tableRight.clear();
118     }
119 }