]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
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 package org.simantics.utils.datastructures;\r
13 \r
14 import java.util.Collection;\r
15 import java.util.Iterator;\r
16 \r
17 /**\r
18  * Collection with at most 2 elements. Does not use equals for comparison.\r
19  */\r
20 public class Max2Collection<T> implements Collection<T> {\r
21 \r
22     public T first;\r
23     public T second;    \r
24     \r
25     static class CollectionIsFullException extends RuntimeException {\r
26 \r
27         private static final long serialVersionUID = 8693601196559590064L;\r
28         \r
29     }        \r
30     \r
31     public Max2Collection() {        \r
32     }\r
33             \r
34     public Max2Collection(T first, T second) {\r
35         this.first = first;\r
36         this.second = second;\r
37     }\r
38 \r
39     public Max2Collection(T first) {\r
40         this.first = first;\r
41     }\r
42 \r
43     final public T get(int i) {\r
44         if(i==0)\r
45             return first;\r
46         else if(i==1)\r
47             return second;\r
48         else\r
49             throw new IndexOutOfBoundsException();\r
50     }\r
51     \r
52     final public T getOther(T el) {\r
53         if(first != el)\r
54             return first;\r
55         else\r
56             return second;        \r
57     }\r
58     \r
59     final public boolean add(T el) {\r
60         if(first == null)\r
61             first = el;\r
62         else if(second == null)\r
63             second = el;\r
64         else\r
65             throw new CollectionIsFullException();\r
66         return true;\r
67     }\r
68     \r
69     final public boolean remove(Object el) {\r
70         if(first == el) {\r
71             first = second;\r
72             second = null;\r
73             return true;\r
74         }\r
75         else if(second == el) {\r
76             second = null;\r
77             return true;\r
78         }\r
79         else\r
80             return false;        \r
81     }\r
82     \r
83     final public void setFirstElement(T el) {\r
84         if(second == el) {\r
85             second = first;\r
86             first = el;\r
87         }               \r
88     }\r
89     \r
90     final public void setSecondElement(T el) {\r
91         if(first == el) {\r
92             first = second;\r
93             second = el;\r
94         }\r
95                 \r
96     }\r
97     \r
98     final public int size() {\r
99         if(first == null)\r
100             return 0;\r
101         else if(second == null)\r
102             return 1;\r
103         else\r
104             return 2;\r
105     }\r
106     \r
107     final public boolean isEmpty() {\r
108         return first == null;\r
109     }\r
110     \r
111     final public boolean isFull() {\r
112         return second != null;\r
113     }\r
114 \r
115     @Override\r
116     public boolean addAll(Collection<? extends T> c) {        \r
117         for(T o : c)\r
118             add(o);\r
119         return !c.isEmpty();\r
120     }\r
121 \r
122     @Override\r
123     public void clear() {\r
124         first = null;\r
125         second = null; // necessary for gc\r
126     }\r
127 \r
128     @Override\r
129     public boolean contains(Object o) {\r
130         return first == o || second == o;\r
131     }\r
132 \r
133     @Override\r
134     public boolean containsAll(Collection<?> c) {\r
135         for(Object o : c)\r
136             if(!contains(o))\r
137                 return false;\r
138         return true;\r
139     }\r
140 \r
141     @Override\r
142     public Iterator<T> iterator() {\r
143         throw new UnsupportedOperationException();\r
144     }\r
145 \r
146     @Override\r
147     public boolean removeAll(Collection<?> c) {\r
148         boolean removed = false;\r
149         for(Object o : c)\r
150             removed |= remove(o);\r
151         return removed;\r
152     }\r
153 \r
154     @Override\r
155     public boolean retainAll(Collection<?> c) {\r
156         throw new UnsupportedOperationException();\r
157     }\r
158 \r
159     @Override\r
160     public Object[] toArray() {\r
161         if(first == null)\r
162             return new Object[0];\r
163         if(second == null)\r
164             return new Object[] {first};\r
165         else\r
166             return new Object[] {first, second};        \r
167     }\r
168 \r
169     @SuppressWarnings("hiding")\r
170     @Override\r
171     public <T> T[] toArray(T[] a) {       \r
172         throw new UnsupportedOperationException();\r
173     }\r
174     \r
175 }\r