-/*******************************************************************************\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
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * 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
+ *******************************************************************************/
+package org.simantics.utils.datastructures;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+/**
+ * Collection with at most 2 elements. Does not use equals for comparison.
+ */
+public class Max2Collection<T> implements Collection<T> {
+
+ public T first;
+ public T second;
+
+ static class CollectionIsFullException extends RuntimeException {
+
+ private static final long serialVersionUID = 8693601196559590064L;
+
+ }
+
+ public Max2Collection() {
+ }
+
+ public Max2Collection(T first, T second) {
+ this.first = first;
+ this.second = second;
+ }
+
+ public Max2Collection(T first) {
+ this.first = first;
+ }
+
+ final public T get(int i) {
+ if(i==0)
+ return first;
+ else if(i==1)
+ return second;
+ else
+ throw new IndexOutOfBoundsException();
+ }
+
+ final public T getOther(T el) {
+ if(first != el)
+ return first;
+ else
+ return second;
+ }
+
+ final public boolean add(T el) {
+ if(first == null)
+ first = el;
+ else if(second == null)
+ second = el;
+ else
+ throw new CollectionIsFullException();
+ return true;
+ }
+
+ final public boolean remove(Object el) {
+ if(first == el) {
+ first = second;
+ second = null;
+ return true;
+ }
+ else if(second == el) {
+ second = null;
+ return true;
+ }
+ else
+ return false;
+ }
+
+ final public void setFirstElement(T el) {
+ if(second == el) {
+ second = first;
+ first = el;
+ }
+ }
+
+ final public void setSecondElement(T el) {
+ if(first == el) {
+ first = second;
+ second = el;
+ }
+
+ }
+
+ final public int size() {
+ if(first == null)
+ return 0;
+ else if(second == null)
+ return 1;
+ else
+ return 2;
+ }
+
+ final public boolean isEmpty() {
+ return first == null;
+ }
+
+ final public boolean isFull() {
+ return second != null;
+ }
+
+ @Override
+ public boolean addAll(Collection<? extends T> c) {
+ for(T o : c)
+ add(o);
+ return !c.isEmpty();
+ }
+
+ @Override
+ public void clear() {
+ first = null;
+ second = null; // necessary for gc
+ }
+
+ @Override
+ public boolean contains(Object o) {
+ return first == o || second == o;
+ }
+
+ @Override
+ public boolean containsAll(Collection<?> c) {
+ for(Object o : c)
+ if(!contains(o))
+ return false;
+ return true;
+ }
+
+ @Override
+ public Iterator<T> iterator() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public boolean removeAll(Collection<?> c) {
+ boolean removed = false;
+ for(Object o : c)
+ removed |= remove(o);
+ return removed;
+ }
+
+ @Override
+ public boolean retainAll(Collection<?> c) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Object[] toArray() {
+ if(first == null)
+ return new Object[0];
+ if(second == null)
+ return new Object[] {first};
+ else
+ return new Object[] {first, second};
+ }
+
+ @SuppressWarnings("hiding")
+ @Override
+ public <T> T[] toArray(T[] a) {
+ throw new UnsupportedOperationException();
+ }
+
+}