2 * Copyright (C) 2015, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
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
10 * http://www.apache.org/licenses/LICENSE-2.0.
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.
18 package gov.nasa.jpf.util;
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;
27 * a immutable list that just contains a single element
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
32 * While java.util.Collections provides optimizations for empty lists, there is
33 * no optimization for single element lists
35 public class SingleElementList<E> implements List<E> {
39 class SingleElemIterator implements ListIterator<E>{
43 public boolean hasNext() {
53 throw new NoSuchElementException();
58 public boolean hasPrevious() {
64 throw new NoSuchElementException();
68 public int nextIndex() {
77 public int previousIndex() {
86 public void remove() {
87 throw new UnsupportedOperationException("list is immutable");
91 public void set(E e) {
92 throw new UnsupportedOperationException("list is immutable");
96 public void add(E e) {
97 throw new UnsupportedOperationException("list is immutable");
101 public SingleElementList (E e){
111 public boolean isEmpty() {
116 public boolean contains(Object o) {
118 return elem.equals(o);
125 public Iterator<E> iterator() {
126 return new SingleElemIterator();
130 public Object[] toArray() {
131 Object[] a = { elem };
136 public <T> T[] toArray(T[] a) {
142 public boolean add(E e) {
147 public boolean remove(Object o) {
152 public boolean containsAll(Collection<?> c) {
162 public boolean addAll(Collection<? extends E> c) {
167 public boolean addAll(int index, Collection<? extends E> c) {
172 public boolean removeAll(Collection<?> c) {
177 public boolean retainAll(Collection<?> c) {
187 public void clear() {
188 throw new UnsupportedOperationException("list is immutable");
192 public E get(int index) {
196 throw new IndexOutOfBoundsException(Integer.toString(index));
201 public E set(int index, E element) {
202 throw new UnsupportedOperationException("list is immutable");
206 public void add(int index, E element) {
207 throw new UnsupportedOperationException("list is immutable");
211 public E remove(int index) {
212 throw new UnsupportedOperationException("list is immutable");
216 public int indexOf(Object o) {
225 public int lastIndexOf(Object o) {
230 public ListIterator<E> listIterator() {
231 return new SingleElemIterator();
235 public ListIterator<E> listIterator(int index) {
237 return new SingleElemIterator();
239 throw new IndexOutOfBoundsException(Integer.toString(index));
244 public List<E> subList(int fromIndex, int toIndex) {
245 throw new UnsupportedOperationException("single element list");