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