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