]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/Max2Collection.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / Max2Collection.java
diff --git a/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/Max2Collection.java b/bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/Max2Collection.java
new file mode 100644 (file)
index 0000000..f100ba5
--- /dev/null
@@ -0,0 +1,175 @@
+/*******************************************************************************\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
+package org.simantics.utils.datastructures;\r
+\r
+import java.util.Collection;\r
+import java.util.Iterator;\r
+\r
+/**\r
+ * Collection with at most 2 elements. Does not use equals for comparison.\r
+ */\r
+public class Max2Collection<T> implements Collection<T> {\r
+\r
+    public T first;\r
+    public T second;    \r
+    \r
+    static class CollectionIsFullException extends RuntimeException {\r
+\r
+        private static final long serialVersionUID = 8693601196559590064L;\r
+        \r
+    }        \r
+    \r
+    public Max2Collection() {        \r
+    }\r
+            \r
+    public Max2Collection(T first, T second) {\r
+        this.first = first;\r
+        this.second = second;\r
+    }\r
+\r
+    public Max2Collection(T first) {\r
+        this.first = first;\r
+    }\r
+\r
+    final public T get(int i) {\r
+        if(i==0)\r
+            return first;\r
+        else if(i==1)\r
+            return second;\r
+        else\r
+            throw new IndexOutOfBoundsException();\r
+    }\r
+    \r
+    final public T getOther(T el) {\r
+        if(first != el)\r
+            return first;\r
+        else\r
+            return second;        \r
+    }\r
+    \r
+    final public boolean add(T el) {\r
+        if(first == null)\r
+            first = el;\r
+        else if(second == null)\r
+            second = el;\r
+        else\r
+            throw new CollectionIsFullException();\r
+        return true;\r
+    }\r
+    \r
+    final public boolean remove(Object el) {\r
+        if(first == el) {\r
+            first = second;\r
+            second = null;\r
+            return true;\r
+        }\r
+        else if(second == el) {\r
+            second = null;\r
+            return true;\r
+        }\r
+        else\r
+            return false;        \r
+    }\r
+    \r
+    final public void setFirstElement(T el) {\r
+        if(second == el) {\r
+            second = first;\r
+            first = el;\r
+        }               \r
+    }\r
+    \r
+    final public void setSecondElement(T el) {\r
+        if(first == el) {\r
+            first = second;\r
+            second = el;\r
+        }\r
+                \r
+    }\r
+    \r
+    final public int size() {\r
+        if(first == null)\r
+            return 0;\r
+        else if(second == null)\r
+            return 1;\r
+        else\r
+            return 2;\r
+    }\r
+    \r
+    final public boolean isEmpty() {\r
+        return first == null;\r
+    }\r
+    \r
+    final public boolean isFull() {\r
+        return second != null;\r
+    }\r
+\r
+    @Override\r
+    public boolean addAll(Collection<? extends T> c) {        \r
+        for(T o : c)\r
+            add(o);\r
+        return !c.isEmpty();\r
+    }\r
+\r
+    @Override\r
+    public void clear() {\r
+        first = null;\r
+        second = null; // necessary for gc\r
+    }\r
+\r
+    @Override\r
+    public boolean contains(Object o) {\r
+        return first == o || second == o;\r
+    }\r
+\r
+    @Override\r
+    public boolean containsAll(Collection<?> c) {\r
+        for(Object o : c)\r
+            if(!contains(o))\r
+                return false;\r
+        return true;\r
+    }\r
+\r
+    @Override\r
+    public Iterator<T> iterator() {\r
+        throw new UnsupportedOperationException();\r
+    }\r
+\r
+    @Override\r
+    public boolean removeAll(Collection<?> c) {\r
+        boolean removed = false;\r
+        for(Object o : c)\r
+            removed |= remove(o);\r
+        return removed;\r
+    }\r
+\r
+    @Override\r
+    public boolean retainAll(Collection<?> c) {\r
+        throw new UnsupportedOperationException();\r
+    }\r
+\r
+    @Override\r
+    public Object[] toArray() {\r
+        if(first == null)\r
+            return new Object[0];\r
+        if(second == null)\r
+            return new Object[] {first};\r
+        else\r
+            return new Object[] {first, second};        \r
+    }\r
+\r
+    @SuppressWarnings("hiding")\r
+    @Override\r
+    public <T> T[] toArray(T[] a) {       \r
+        throw new UnsupportedOperationException();\r
+    }\r
+    \r
+}\r