]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/Max2Collection.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / Max2Collection.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.utils.datastructures;
13
14 import java.util.Collection;
15 import java.util.Iterator;
16
17 /**
18  * Collection with at most 2 elements. Does not use equals for comparison.
19  */
20 public class Max2Collection<T> implements Collection<T> {
21
22     public T first;
23     public T second;    
24     
25     static class CollectionIsFullException extends RuntimeException {
26
27         private static final long serialVersionUID = 8693601196559590064L;
28         
29     }        
30     
31     public Max2Collection() {        
32     }
33             
34     public Max2Collection(T first, T second) {
35         this.first = first;
36         this.second = second;
37     }
38
39     public Max2Collection(T first) {
40         this.first = first;
41     }
42
43     final public T get(int i) {
44         if(i==0)
45             return first;
46         else if(i==1)
47             return second;
48         else
49             throw new IndexOutOfBoundsException();
50     }
51     
52     final public T getOther(T el) {
53         if(first != el)
54             return first;
55         else
56             return second;        
57     }
58     
59     final public boolean add(T el) {
60         if(first == null)
61             first = el;
62         else if(second == null)
63             second = el;
64         else
65             throw new CollectionIsFullException();
66         return true;
67     }
68     
69     final public boolean remove(Object el) {
70         if(first == el) {
71             first = second;
72             second = null;
73             return true;
74         }
75         else if(second == el) {
76             second = null;
77             return true;
78         }
79         else
80             return false;        
81     }
82     
83     final public void setFirstElement(T el) {
84         if(second == el) {
85             second = first;
86             first = el;
87         }               
88     }
89     
90     final public void setSecondElement(T el) {
91         if(first == el) {
92             first = second;
93             second = el;
94         }
95                 
96     }
97     
98     final public int size() {
99         if(first == null)
100             return 0;
101         else if(second == null)
102             return 1;
103         else
104             return 2;
105     }
106     
107     final public boolean isEmpty() {
108         return first == null;
109     }
110     
111     final public boolean isFull() {
112         return second != null;
113     }
114
115     @Override
116     public boolean addAll(Collection<? extends T> c) {        
117         for(T o : c)
118             add(o);
119         return !c.isEmpty();
120     }
121
122     @Override
123     public void clear() {
124         first = null;
125         second = null; // necessary for gc
126     }
127
128     @Override
129     public boolean contains(Object o) {
130         return first == o || second == o;
131     }
132
133     @Override
134     public boolean containsAll(Collection<?> c) {
135         for(Object o : c)
136             if(!contains(o))
137                 return false;
138         return true;
139     }
140
141     @Override
142     public Iterator<T> iterator() {
143         throw new UnsupportedOperationException();
144     }
145
146     @Override
147     public boolean removeAll(Collection<?> c) {
148         boolean removed = false;
149         for(Object o : c)
150             removed |= remove(o);
151         return removed;
152     }
153
154     @Override
155     public boolean retainAll(Collection<?> c) {
156         throw new UnsupportedOperationException();
157     }
158
159     @Override
160     public Object[] toArray() {
161         if(first == null)
162             return new Object[0];
163         if(second == null)
164             return new Object[] {first};
165         else
166             return new Object[] {first, second};        
167     }
168
169     @SuppressWarnings("hiding")
170     @Override
171     public <T> T[] toArray(T[] a) {       
172         throw new UnsupportedOperationException();
173     }
174     
175 }