Initial import
[jpf-core.git] / src / main / gov / nasa / jpf / util / SingleElementList.java
1 /*
2  * Copyright (C) 2015, United States Government, as represented by the
3  * Administrator of the National Aeronautics and Space Administration.
4  * All rights reserved.
5  *
6  * The Java Pathfinder core (jpf-core) platform is licensed under the
7  * Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  * 
10  *        http://www.apache.org/licenses/LICENSE-2.0. 
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and 
16  * limitations under the License.
17  */
18 package gov.nasa.jpf.util;
19
20 import java.util.Collection;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.ListIterator;
24 import java.util.NoSuchElementException;
25
26 /**
27  * a immutable list that just contains a single element
28  * 
29  * This is just an optimization for constructs such as CGs that inherently can
30  * contain lists, but frequently don't have more than a single element
31  * 
32  * While java.util.Collections provides optimizations for empty lists, there is
33  * no optimization for single element lists
34  */
35 public class SingleElementList<E> implements List<E> {
36   
37   protected E elem;
38
39   class SingleElemIterator implements ListIterator<E>{
40     boolean done;
41
42     @Override
43     public boolean hasNext() {
44       return !done;
45     }
46
47     @Override
48     public E next() {
49       if (!done){
50         done = true;
51         return elem;
52       } else {
53         throw new NoSuchElementException();
54       }
55     }
56
57     @Override
58     public boolean hasPrevious() {
59       return false;
60     }
61
62     @Override
63     public E previous() {
64       throw new NoSuchElementException();
65     }
66
67     @Override
68     public int nextIndex() {
69       if (!done){
70         return 0;
71       } else {
72         return 1;
73       }
74     }
75
76     @Override
77     public int previousIndex() {
78       if (done){
79         return 0;
80       } else {
81         return -1;
82       }
83     }
84
85     @Override
86     public void remove() {
87       throw new UnsupportedOperationException("list is immutable");
88     }
89
90     @Override
91     public void set(E e) {
92       throw new UnsupportedOperationException("list is immutable");
93     }
94
95     @Override
96     public void add(E e) {
97       throw new UnsupportedOperationException("list is immutable");
98     }
99   }
100   
101   public SingleElementList (E e){
102     elem = e;
103   }
104
105   @Override
106   public int size() {
107     return 1;
108   }
109
110   @Override
111   public boolean isEmpty() {
112     return false;
113   }
114
115   @Override
116   public boolean contains(Object o) {
117     if (elem != null){
118       return elem.equals(o);
119     } else {
120       return o == null;
121     }
122   }
123
124   @Override
125   public Iterator<E> iterator() {
126     return new SingleElemIterator();
127   }
128
129   @Override
130   public Object[] toArray() {
131     Object[] a = { elem };
132     return a;
133   }
134
135   @Override
136   public <T> T[] toArray(T[] a) {
137     a[0] = (T)elem;
138     return a;
139   }
140
141   @Override
142   public boolean add(E e) {
143     return false;
144   }
145
146   @Override
147   public boolean remove(Object o) {
148     return false;
149   }
150
151   @Override
152   public boolean containsAll(Collection<?> c) {
153     for (Object o : c){
154       if (!contains(o)){
155         return false;
156       }
157     }
158     return true;
159   }
160
161   @Override
162   public boolean addAll(Collection<? extends E> c) {
163     return false;
164   }
165
166   @Override
167   public boolean addAll(int index, Collection<? extends E> c) {
168     return false;
169   }
170
171   @Override
172   public boolean removeAll(Collection<?> c) {
173     return false;
174   }
175
176   @Override
177   public boolean retainAll(Collection<?> c) {
178     for (Object o : c){
179       if (!contains(o)){
180         return false;
181       }
182     }
183     return true;
184   }
185
186   @Override
187   public void clear() {
188     throw new UnsupportedOperationException("list is immutable");
189   }
190
191   @Override
192   public E get(int index) {
193     if (index == 0){
194       return elem;
195     } else {
196       throw new IndexOutOfBoundsException(Integer.toString(index));
197     }
198   }
199
200   @Override
201   public E set(int index, E element) {
202     throw new UnsupportedOperationException("list is immutable");
203   }
204
205   @Override
206   public void add(int index, E element) {
207     throw new UnsupportedOperationException("list is immutable");
208   }
209
210   @Override
211   public E remove(int index) {
212     throw new UnsupportedOperationException("list is immutable");
213   }
214
215   @Override
216   public int indexOf(Object o) {
217     if (elem.equals(o)){
218       return 0;
219     } else {
220       return -1;
221     }
222   }
223
224   @Override
225   public int lastIndexOf(Object o) {
226     return indexOf(o);
227   }
228
229   @Override
230   public ListIterator<E> listIterator() {
231     return new SingleElemIterator();
232   }
233
234   @Override
235   public ListIterator<E> listIterator(int index) {
236     if (index == 0){
237       return new SingleElemIterator();
238     } else {
239       throw new IndexOutOfBoundsException(Integer.toString(index));      
240     }
241   }
242
243   @Override
244   public List<E> subList(int fromIndex, int toIndex) {
245     throw new UnsupportedOperationException("single element list");
246   }
247   
248 }