001    /*
002    // $Id: //open/mondrian/src/main/mondrian/util/UnsupportedList.java#6 $
003    // This software is subject to the terms of the Common Public License
004    // Agreement, available at the following URL:
005    // http://www.opensource.org/licenses/cpl.html.
006    // Copyright (C) 2006-2008 Julian Hyde
007    // All Rights Reserved.
008    // You must accept the terms of that agreement to use this software.
009    */
010    package mondrian.util;
011    
012    import java.util.Collection;
013    import java.util.Iterator;
014    import java.util.ListIterator;
015    import java.util.List;
016    import java.util.NoSuchElementException;
017    
018    /**
019     * Implementation of {@link java.util.List} where all methods throw
020     * an UnsupportedOperationException exception except for the
021     * <code>isEmpty</code> method. The <code>iterator</code> and
022     * <code>listIterator</code> methods can be easily implemented in
023     * derived classes by using the helper inner classes:
024     * <code>Itr</code> and <code>ListItr</code>.
025     * These iterators are all read only,
026     * their <code>remove</code>, <code>add</code> and <code>set</code>
027     * methods throw the
028     * UnsupportedOperationException exception.
029     * <p>
030     * This class can be used for List implementations that only implement
031     * a subset of all the methods.
032     *
033     * @author Richard Emberson
034     * @version $Id: //open/mondrian/src/main/mondrian/util/UnsupportedList.java#6 $
035     * @since Jan 16, 2007
036     */
037    public abstract class UnsupportedList<T> implements List<T> {
038        protected UnsupportedList() {
039        }
040    
041        public boolean isEmpty() {
042            return (size() == 0);
043        }
044    
045        public int size() {
046            throw new UnsupportedOperationException(getClass().getName() + ".size");
047        }
048    
049        public T get(int index) {
050            throw new UnsupportedOperationException(getClass().getName() + ".get");
051        }
052    
053        public T set(int index, T element) {
054            throw new UnsupportedOperationException(getClass().getName() + ".set");
055        }
056    
057        public Object[] toArray() {
058            throw new UnsupportedOperationException(getClass().getName() + ".toArray");
059        }
060    
061        public void add(int index, T element) {
062            throw new UnsupportedOperationException(getClass().getName() + ".add");
063        }
064    
065        public T remove(int index) {
066            throw new UnsupportedOperationException(getClass().getName() + ".remove");
067        }
068    
069        public int indexOf(Object o) {
070            throw new UnsupportedOperationException(getClass().getName() + ".indexOf");
071        }
072    
073        public int lastIndexOf(Object o) {
074            throw new UnsupportedOperationException(getClass().getName() + ".lastIndexOf");
075        }
076    
077        public List<T> subList(int fromIndex, int toIndex) {
078            throw new UnsupportedOperationException(getClass().getName() + ".subList");
079        }
080    
081        public boolean contains(Object o) {
082            throw new UnsupportedOperationException(getClass().getName() + ".contains");
083        }
084    
085        public <T> T[] toArray(T[] a) {
086            throw new UnsupportedOperationException(getClass().getName() + ".toArray");
087        }
088    
089        public boolean add(T o) {
090            throw new UnsupportedOperationException(getClass().getName() + ".add");
091        }
092    
093        public boolean remove(Object o) {
094            throw new UnsupportedOperationException(getClass().getName() + ".remove");
095        }
096    
097        public boolean containsAll(Collection<?> c) {
098            throw new UnsupportedOperationException(getClass().getName() + ".containsAll");
099        }
100    
101        public boolean addAll(Collection<? extends T> c) {
102            throw new UnsupportedOperationException(getClass().getName() + ".addAll");
103        }
104    
105        public boolean addAll(int index, Collection<? extends T> c) {
106            throw new UnsupportedOperationException(getClass().getName() + ".addAll");
107        }
108    
109        public boolean removeAll(Collection<?> c) {
110            throw new UnsupportedOperationException(getClass().getName() + ".removeAll");
111        }
112    
113        public boolean retainAll(Collection<?> c) {
114            throw new UnsupportedOperationException(getClass().getName() + ".retainAll");
115        }
116    
117        public void clear() {
118            throw new UnsupportedOperationException(getClass().getName() + ".clear");
119        }
120    
121        public boolean equals(Object o) {
122            throw new UnsupportedOperationException(getClass().getName() + ".equals");
123        }
124    
125        public int hashCode() {
126            throw new UnsupportedOperationException(getClass().getName() + ".hashCode");
127        }
128    
129        public ListIterator<T> listIterator() {
130            throw new UnsupportedOperationException(getClass().getName() + ".listIterator");
131        }
132    
133        public ListIterator<T> listIterator(int index) {
134            throw new UnsupportedOperationException(getClass().getName() + ".listIterator");
135        }
136    
137        public Iterator<T> iterator() {
138            throw new UnsupportedOperationException(getClass().getName() + ".iterator");
139        }
140    
141    
142    
143        protected class Itr implements Iterator<T> {
144            protected int cursor;
145            protected int lastRet;
146    
147            public Itr() {
148                this.cursor = 0;
149                this.lastRet = -1;
150            }
151    
152            public boolean hasNext() {
153                return (cursor != size());
154            }
155            public T next() {
156                try {
157                    T next = get(cursor);
158                    lastRet = cursor++;
159                    return next;
160                } catch (IndexOutOfBoundsException e) {
161                    System.out.println("UnsupportedList.Itr.next: cursor="  +  cursor);
162                    System.out.println("UnsupportedList.Itr.next: size="  +  size());
163                    throw new NoSuchElementException();
164                }
165            }
166            public void remove() {
167                throw new UnsupportedOperationException(getClass().getName() + ".remove");
168            }
169        }
170    
171        protected class ListItr extends Itr implements ListIterator<T> {
172            public ListItr(int index) {
173                this.cursor = index;
174            }
175    
176            public boolean hasPrevious() {
177                return cursor != 0;
178            }
179    
180            public T previous() {
181                try {
182                    int i = cursor - 1;
183                    T previous = get(i);
184                    lastRet = cursor = i;
185                    return previous;
186                } catch (IndexOutOfBoundsException e) {
187                    throw new NoSuchElementException();
188                }
189            }
190    
191            public int nextIndex() {
192                return cursor;
193            }
194    
195            public int previousIndex() {
196                return cursor - 1;
197            }
198    
199            public void set(T o) {
200    /*
201                if (lastRet == -1)
202                    throw new IllegalStateException();
203                try {
204                    MemberList.this.set(lastRet, o);
205                } catch (IndexOutOfBoundsException e) {
206                    throw new ConcurrentModificationException();
207                }
208    */
209                throw new UnsupportedOperationException(getClass().getName() + ".set");
210            }
211    
212            public void add(T o) {
213                throw new UnsupportedOperationException(getClass().getName() + ".add");
214            }
215        }
216    
217        /**
218         * Iterator for arrays of a priori unknown size.
219         */
220        protected class ItrUnknownSize extends Itr {
221            public ItrUnknownSize() {
222                super();
223            }
224    
225            public boolean hasNext() {
226                try {
227                    get(cursor);
228                    return true;
229                } catch (IndexOutOfBoundsException e) {
230                    return false;
231                }
232            }
233        }
234    }
235    
236    // End UnsupportedList.java
237