KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mondrian > util > UnsupportedList


1 /*
2 // $Id: //open/mondrian/src/main/mondrian/util/UnsupportedList.java#2 $
3 // This software is subject to the terms of the Common Public License
4 // Agreement, available at the following URL:
5 // http://www.opensource.org/licenses/cpl.html.
6 // Copyright (C) 2006-2007 Julian Hyde
7 // All Rights Reserved.
8 // You must accept the terms of that agreement to use this software.
9 */

10 package mondrian.util;
11
12 import java.util.Collection JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.ListIterator JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.NoSuchElementException JavaDoc;
17
18 /**
19  * Implementation of {@link java.util.List} where all methods throw
20  * an UnsupportedOperationException exception except for the
21  * <code>isEmpty</code> method. The <code>iterator</code> and
22  * <code>listIterator</code> methods can be easily implemented in
23  * derived classes by using the helper inner classes:
24  * <code>Itr</code> and <code>ListItr</code>.
25  * These iterators are all read only,
26  * their <code>remove</code>, <code>add</code> and <code>set</code>
27  * methods throw the
28  * UnsupportedOperationException exception.
29  * <p>
30  * This class can be used for List implementations that only implement
31  * a subset of all the methods.
32  *
33  * @author Richard Emberson
34  * @version $Id: //open/mondrian/src/main/mondrian/util/UnsupportedList.java#2 $
35  * @since Jan 16, 2007
36  */

37 public abstract class UnsupportedList<T> implements List JavaDoc<T> {
38     protected UnsupportedList() {
39     }
40     public boolean isEmpty() {
41         return (size() == 0);
42     }
43
44     public int size() {
45         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".size");
46     }
47     public T get(int index) {
48         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".get");
49     }
50     public T set(int index, T element) {
51         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".set");
52     }
53
54     public Object JavaDoc[] toArray() {
55         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".toArray");
56     }
57
58     public void add(int index, T element) {
59         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".add");
60     }
61     public T remove(int index) {
62         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".remove");
63     }
64     public int indexOf(Object JavaDoc o) {
65         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".indexOf");
66     }
67     public int lastIndexOf(Object JavaDoc o) {
68         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".lastIndexOf");
69     }
70     public List JavaDoc<T> subList(int fromIndex, int toIndex) {
71         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".subList");
72     }
73     public boolean contains(Object JavaDoc o) {
74         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".contains");
75     }
76     public <T> T[] toArray(T[] a) {
77         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".toArray");
78     }
79     public boolean add(T o) {
80         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".add");
81     }
82     public boolean remove(Object JavaDoc o) {
83         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".remove");
84     }
85     public boolean containsAll(Collection JavaDoc<?> c) {
86         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".containsAll");
87     }
88     public boolean addAll(Collection JavaDoc<? extends T> c) {
89         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".addAll");
90     }
91     public boolean addAll(int index, Collection JavaDoc<? extends T> c) {
92         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".addAll");
93     }
94     public boolean removeAll(Collection JavaDoc<?> c) {
95         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".removeAll");
96     }
97     public boolean retainAll(Collection JavaDoc<?> c) {
98         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".retainAll");
99     }
100     public void clear() {
101         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".clear");
102     }
103     public boolean equals(Object JavaDoc o) {
104         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".equals");
105     }
106     public int hashCode() {
107         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".hashCode");
108     }
109     public ListIterator JavaDoc<T> listIterator() {
110         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".listIterator");
111     }
112     public ListIterator JavaDoc<T> listIterator(int index) {
113         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".listIterator");
114     }
115     public Iterator JavaDoc<T> iterator() {
116         throw new UnsupportedOperationException JavaDoc(getClass().getName()+".iterator");
117     }
118
119     protected class Itr implements Iterator JavaDoc<T> {
120         protected int cursor;
121         protected int lastRet;
122
123         public Itr() {
124             this.cursor = 0;
125             this.lastRet = -1;
126         }
127
128         public boolean hasNext() {
129             return (cursor != size());
130         }
131         public T next() {
132             try {
133                 T next = get(cursor);
134                 lastRet = cursor++;
135                 return next;
136             } catch(IndexOutOfBoundsException JavaDoc e) {
137                 System.out.println("UnsupportedList.Itr.next: cursor=" +cursor);
138                 System.out.println("UnsupportedList.Itr.next: size=" +size());
139                 throw new NoSuchElementException JavaDoc();
140             }
141         }
142         public void remove() {
143             throw new UnsupportedOperationException JavaDoc(getClass().getName()+".remove");
144         }
145     }
146
147     protected class ListItr extends Itr implements ListIterator JavaDoc<T> {
148         public ListItr(int index) {
149             this.cursor = index;
150         }
151
152         public boolean hasPrevious() {
153             return cursor != 0;
154         }
155         public T previous() {
156             try {
157                 int i = cursor - 1;
158                 T previous = get(i);
159                 lastRet = cursor = i;
160                 return previous;
161             } catch(IndexOutOfBoundsException JavaDoc e) {
162                 throw new NoSuchElementException JavaDoc();
163             }
164         }
165
166         public int nextIndex() {
167             return cursor;
168         }
169
170         public int previousIndex() {
171             return cursor-1;
172         }
173
174         public void set(T o) {
175 /*
176             if (lastRet == -1)
177                 throw new IllegalStateException();
178             try {
179                 MemberList.this.set(lastRet, o);
180             } catch(IndexOutOfBoundsException e) {
181                 throw new ConcurrentModificationException();
182             }
183 */

184             throw new UnsupportedOperationException JavaDoc(getClass().getName()+".set");
185         }
186
187         public void add(T o) {
188             throw new UnsupportedOperationException JavaDoc(getClass().getName()+".add");
189         }
190     }
191 }
192
Popular Tags