]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/BijectionMap.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / BijectionMap.java
index ab48c195e208ba0dd88cfbe2c6241dfee4bf58f5..44eae66eadd2b388288b61d654dfe5c539612584 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007- VTT Technical Research Centre of Finland.\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 org.simantics.utils.datastructures;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Collections;\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-import java.util.Set;\r
-import java.util.Map.Entry;\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
-    /** The keys of tableLeft are left-side-values and\r
-     * values are right-side-values */\r
-    private final Map<L, R> imTableLeft = Collections.unmodifiableMap(tableLeft);\r
-    /** The keys of tableRight are right-side-values and\r
-     * values on it are left-side-values */\r
-    private final Map<R, L> imTableRight = Collections.unmodifiableMap(tableRight);\r
-\r
-    private final Set<L> imLeftSet = Collections.unmodifiableSet(tableLeft.keySet());\r
-    private final Set<R> imRightSet = Collections.unmodifiableSet(tableRight.keySet());\r
-\r
-       public BijectionMap() {\r
-\r
-       }\r
-\r
-       public BijectionMap(BijectionMap<L, R> copyFrom) {\r
-               this.tableLeft.putAll(copyFrom.tableLeft);\r
-               this.tableRight.putAll(copyFrom.tableRight);\r
-       }\r
-\r
-       public void addAll(BijectionMap<L, R> map)\r
-       {\r
-               for (Entry<L, R> e : map.getEntries())\r
-                       map(e.getKey(), e.getValue());\r
-       }\r
-\r
-    public boolean retainAllLeft(Collection<L> values)\r
-    {\r
-        boolean result = false;\r
-        Collection<L> remove = new ArrayList<L>(size());\r
-        for (L lValue : imLeftSet) {\r
-            if (!values.contains(lValue)) {\r
-                remove.add(lValue);\r
-                result = true;\r
-            }\r
-        }\r
-        if (!remove.isEmpty()) {\r
-            for (L lValue : remove)\r
-                removeWithLeft(lValue);\r
-        }\r
-        return result;\r
-    }\r
-\r
-    public boolean retainAllRight(Collection<R> values)\r
-    {\r
-        boolean result = false;\r
-        Collection<R> remove = new ArrayList<R>(size());\r
-        for (R rValue : imRightSet) {\r
-            if (!values.contains(rValue)) {\r
-                remove.add(rValue);\r
-                result = true;\r
-            }\r
-        }\r
-        if (!remove.isEmpty()) {\r
-            for (R rValue : remove)\r
-                removeWithRight(rValue);\r
-        }\r
-        return result;\r
-    }\r
-\r
-       public Set<Entry<L, R>> getEntries()\r
-       {\r
-               return tableLeft.entrySet();\r
-       }\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 boolean contains(L leftValue, R rightValue)\r
-       {\r
-               if (leftValue==null || rightValue==null) return false;\r
-               return rightValue.equals(tableLeft.get(leftValue));\r
-       }\r
-\r
-       public void map(L leftValue, R rightValue)\r
-       {\r
-        if (leftValue == null || rightValue == null)\r
-            throw new NullPointerException();\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 boolean isEmpty() {\r
-           return tableLeft.isEmpty();\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
-       /**\r
-        * Get set of left values\r
-        *\r
-        * @return read-only set\r
-        */\r
-    public Set<L> getLeftSet() {\r
-        return imLeftSet;\r
-    }\r
-\r
-    /**\r
-     * Get set of right values\r
-     *\r
-     * @return read-only set\r
-     */\r
-    public Set<R> getRightSet() {\r
-        return imRightSet;\r
-    }\r
-\r
-    /**\r
-     * Get left-to-right map\r
-     *\r
-     * @return read only map\r
-     */\r
-    public Map<L, R> getLeftToRightMap() {\r
-        return imTableLeft;\r
-    }\r
-\r
-    /**\r
-     * Get right-to-left map\r
-     *\r
-     * @return read only map\r
-     */\r
-    public Map<R, L> getRightToLeftMap() {\r
-        return imTableRight;\r
-    }\r
-\r
-    public void clear() {\r
-        tableLeft.clear();\r
-        tableRight.clear();\r
-    }\r
-\r
-    @Override\r
-    public String toString() {\r
-       int count = 0;\r
-       StringBuilder sb = new StringBuilder();\r
-       sb.append("[");\r
-       for (Entry<L, R> e : tableLeft.entrySet())\r
-       {\r
-               if (count++>0) sb.append(", ");\r
-               sb.append(e.getKey().toString());\r
-               sb.append("=");\r
-               sb.append(e.getValue().toString());\r
-       }\r
-       sb.append("]");\r
-       return sb.toString();\r
-    }\r
-\r
-    @Override\r
-    public BijectionMap<L, R> clone() {\r
-        return new BijectionMap<L, R>(this);\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007- VTT Technical Research Centre of Finland.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+/*
+ * Created on Jan 21, 2005
+ *
+ * Copyright Toni Kalajainen
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.simantics.utils.datastructures;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+
+/**
+ * Bijection map is a Map that has no values or keys, only 1:1 mappings
+ * of values. These value/keys will be called with left and right side
+ * values.
+ *
+ * Each value can exist only once on a side
+ *
+ * @author Toni Kalajainen
+ */
+public class BijectionMap<L, R> {
+
+    /** The keys of tableLeft are left-side-values and
+     * values are right-side-values */
+    private final Map<L, R> tableLeft = new HashMap<L, R>();
+    /** The keys of tableRight are right-side-values and
+     * values on it are left-side-values */
+    private final Map<R, L> tableRight = new HashMap<R, L>();
+
+    /** The keys of tableLeft are left-side-values and
+     * values are right-side-values */
+    private final Map<L, R> imTableLeft = Collections.unmodifiableMap(tableLeft);
+    /** The keys of tableRight are right-side-values and
+     * values on it are left-side-values */
+    private final Map<R, L> imTableRight = Collections.unmodifiableMap(tableRight);
+
+    private final Set<L> imLeftSet = Collections.unmodifiableSet(tableLeft.keySet());
+    private final Set<R> imRightSet = Collections.unmodifiableSet(tableRight.keySet());
+
+       public BijectionMap() {
+
+       }
+
+       public BijectionMap(BijectionMap<L, R> copyFrom) {
+               this.tableLeft.putAll(copyFrom.tableLeft);
+               this.tableRight.putAll(copyFrom.tableRight);
+       }
+
+       public void addAll(BijectionMap<L, R> map)
+       {
+               for (Entry<L, R> e : map.getEntries())
+                       map(e.getKey(), e.getValue());
+       }
+
+    public boolean retainAllLeft(Collection<L> values)
+    {
+        boolean result = false;
+        Collection<L> remove = new ArrayList<L>(size());
+        for (L lValue : imLeftSet) {
+            if (!values.contains(lValue)) {
+                remove.add(lValue);
+                result = true;
+            }
+        }
+        if (!remove.isEmpty()) {
+            for (L lValue : remove)
+                removeWithLeft(lValue);
+        }
+        return result;
+    }
+
+    public boolean retainAllRight(Collection<R> values)
+    {
+        boolean result = false;
+        Collection<R> remove = new ArrayList<R>(size());
+        for (R rValue : imRightSet) {
+            if (!values.contains(rValue)) {
+                remove.add(rValue);
+                result = true;
+            }
+        }
+        if (!remove.isEmpty()) {
+            for (R rValue : remove)
+                removeWithRight(rValue);
+        }
+        return result;
+    }
+
+       public Set<Entry<L, R>> getEntries()
+       {
+               return tableLeft.entrySet();
+       }
+
+       public boolean containsLeft(L leftValue)
+       {
+               return tableLeft.containsKey(leftValue);
+       }
+
+       public boolean containsRight(R rightValue)
+       {
+               return tableRight.containsKey(rightValue);
+       }
+
+       public boolean contains(L leftValue, R rightValue)
+       {
+               if (leftValue==null || rightValue==null) return false;
+               return rightValue.equals(tableLeft.get(leftValue));
+       }
+
+       public void map(L leftValue, R rightValue)
+       {
+        if (leftValue == null || rightValue == null)
+            throw new NullPointerException();
+        // Remove possible old mapping
+        R oldRight = tableLeft.remove(leftValue);
+        if (oldRight != null) {
+            tableRight.remove(oldRight);
+        } else {
+            L oldLeft = tableRight.remove(rightValue);
+            if (oldLeft != null) {
+                tableLeft.remove(oldLeft);
+            }
+        }
+
+               tableLeft.put(leftValue, rightValue);
+               tableRight.put(rightValue, leftValue);
+       }
+
+       public boolean isEmpty() {
+           return tableLeft.isEmpty();
+       }
+
+    public int size()
+    {
+        return tableLeft.size();
+    }
+
+       public L getLeft(R rightValue) {
+               return tableRight.get(rightValue);
+       }
+
+       public R getRight(L leftValue) {
+               return tableLeft.get(leftValue);
+       }
+
+       public R removeWithLeft(L leftValue) {
+               R rightValue = tableLeft.remove(leftValue);
+               if (rightValue!=null)
+                       tableRight.remove(rightValue);
+               return rightValue;
+       }
+
+       public L removeWithRight(R rightValue) {
+               L leftValue = tableRight.remove(rightValue);
+               if (leftValue!=null)
+                       tableLeft.remove(leftValue);
+               return leftValue;
+       }
+
+       /**
+        * Get set of left values
+        *
+        * @return read-only set
+        */
+    public Set<L> getLeftSet() {
+        return imLeftSet;
+    }
+
+    /**
+     * Get set of right values
+     *
+     * @return read-only set
+     */
+    public Set<R> getRightSet() {
+        return imRightSet;
+    }
+
+    /**
+     * Get left-to-right map
+     *
+     * @return read only map
+     */
+    public Map<L, R> getLeftToRightMap() {
+        return imTableLeft;
+    }
+
+    /**
+     * Get right-to-left map
+     *
+     * @return read only map
+     */
+    public Map<R, L> getRightToLeftMap() {
+        return imTableRight;
+    }
+
+    public void clear() {
+        tableLeft.clear();
+        tableRight.clear();
+    }
+
+    @Override
+    public String toString() {
+       int count = 0;
+       StringBuilder sb = new StringBuilder();
+       sb.append("[");
+       for (Entry<L, R> e : tableLeft.entrySet())
+       {
+               if (count++>0) sb.append(", ");
+               sb.append(e.getKey().toString());
+               sb.append("=");
+               sb.append(e.getValue().toString());
+       }
+       sb.append("]");
+       return sb.toString();
+    }
+
+    @Override
+    public BijectionMap<L, R> clone() {
+        return new BijectionMap<L, R>(this);
+    }
+
+}