--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+/*\r
+ * Created on Jan 21, 2005\r
+ * \r
+ * Copyright Toni Kalajainen\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package fi.vtt.simantics.procore.internal;\r
+\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+/**\r
+ * Bijection map is a Map that has no values or keys, only 1:1 mappings\r
+ * of values. These value/keys will be called with left and right side\r
+ * values.\r
+ * \r
+ * Each value can exist only once on a side\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class BijectionMap<L, R> {\r
+\r
+ /** The keys of tableLeft are left-side-values and\r
+ * values are right-side-values */\r
+ private final Map<L, R> tableLeft = new HashMap<L, R>();\r
+ /** The keys of tableRight are right-side-values and\r
+ * values on it are left-side-values */\r
+ private final Map<R, L> tableRight = new HashMap<R, L>();\r
+\r
+ public boolean containsLeft(L leftValue)\r
+ {\r
+ return tableLeft.containsKey(leftValue);\r
+ }\r
+\r
+ public boolean containsRight(R rightValue)\r
+ {\r
+ return tableRight.containsKey(rightValue);\r
+ }\r
+\r
+ public void map(L leftValue, R rightValue)\r
+ {\r
+ // Remove possible old mapping\r
+ R oldRight = tableLeft.remove(leftValue);\r
+ if (oldRight != null) {\r
+ tableRight.remove(oldRight);\r
+ } else {\r
+ L oldLeft = tableRight.remove(rightValue);\r
+ if (oldLeft != null) {\r
+ tableLeft.remove(oldLeft);\r
+ }\r
+ }\r
+\r
+ tableLeft.put(leftValue, rightValue);\r
+ tableRight.put(rightValue, leftValue);\r
+ }\r
+\r
+ public int size()\r
+ {\r
+ return tableLeft.size();\r
+ }\r
+\r
+ public L getLeft(R rightValue) {\r
+ return tableRight.get(rightValue);\r
+ }\r
+\r
+ public R getRight(L leftValue) {\r
+ return tableLeft.get(leftValue);\r
+ }\r
+\r
+ public R removeWithLeft(L leftValue) {\r
+ R rightValue = tableLeft.remove(leftValue);\r
+ if (rightValue!=null)\r
+ tableRight.remove(rightValue);\r
+ return rightValue;\r
+ }\r
+\r
+ public L removeWithRight(R rightValue) {\r
+ L leftValue = tableRight.remove(rightValue);\r
+ if (leftValue!=null)\r
+ tableLeft.remove(leftValue);\r
+ return leftValue;\r
+ }\r
+\r
+ public Set<L> getLeftSet() {\r
+ return tableLeft.keySet();\r
+ }\r
+\r
+ public Set<R> getRightSet() {\r
+ return tableRight.keySet();\r
+ }\r
+\r
+ public void clear() {\r
+ tableLeft.clear();\r
+ tableRight.clear();\r
+ }\r
+}\r