1 package java.util;
2 
3 /**
4  * Title:
5  * Description:
6  * Copyright:    Copyright (c) 2001
7  * Company:
8  * @version 1.0
9  */
10 
11 public abstract class AbstractList extends AbstractCollection implements List
12 
13 {
14   protected AbstractList al = this;
15 
16 
AbstractList()17   protected AbstractList()
18         {
19     }
20 
add(Object o)21     public boolean add(Object o) throws UnsupportedOperationException, ClassCastException, IllegalArgumentException
22         {
23             try
24                 {
25                     add(size(),o);
26                   return true;
27                 }
28             catch(UnsupportedOperationException ue)
29                 {
30                     throw ue;
31                 }
32         }
33 
get(int index)34     public abstract Object get(int index) throws IndexOutOfBoundsException;
35 
set(int index,Object element)36     public Object set(int index,Object element) throws UnsupportedOperationException, ClassCastException, IllegalArgumentException, IndexOutOfBoundsException
37         {
38             throw new UnsupportedOperationException();
39         }
40 
add(int index,Object element)41     public void add(int index,Object element)  throws UnsupportedOperationException, ClassCastException, IllegalArgumentException, IndexOutOfBoundsException
42         {
43             throw new UnsupportedOperationException();
44         }
45 
remove(int index)46     public Object remove(int index)  throws UnsupportedOperationException, IndexOutOfBoundsException
47         {
48             Object o = get(index);
49 
50             removeRange(index,index+1);
51             return o;
52         }
53 
indexOf(Object o)54     public int indexOf(Object o)
55         {
56              ListIterator li = listIterator();
57       Object e;
58           while(li.hasNext())
59                 {
60           int index=li.nextIndex();
61                     e=li.next();
62 
63                     if(o==null)
64                         {
65                             if(e==null)
66                                 return index;
67                         }
68                     else
69                         {
70                             if(o.equals(e))
71                                 return index;
72                         }
73                 }
74             return -1;
75         }
76 
lastIndexOf(Object o)77     public int lastIndexOf(Object o)
78         {
79             ListIterator li=listIterator(size());
80           while(li.hasPrevious())
81                 {
82                     int index=li.previousIndex();
83                     Object e=li.previous();
84                     if(o==null)
85                         {
86                             if(e==null)
87                                 return index;
88                         }
89                     else
90                         {
91                             if(o.equals(e))
92                                 return index;
93                         }
94                 }
95             return -1;
96         }
97 
clear()98     public void clear() throws UnsupportedOperationException
99         {
100             try
101                 {
102                     removeRange(0,size());
103                 }
104             catch(UnsupportedOperationException ue)
105                 {
106                     throw ue;
107                 }
108         }
109 
addAll(int index,Collection c)110     public boolean addAll(int index,Collection c) throws UnsupportedOperationException, ClassCastException, IllegalArgumentException, IndexOutOfBoundsException
111         {
112             Iterator it=c.iterator();
113             boolean ret=false;
114             while(it.hasNext())
115                 {
116                     try
117                         {
118                             add(index++,it.next());
119                             ret=true;
120                         }
121                     catch(UnsupportedOperationException ue)
122                         {
123                             throw ue;
124                         }
125                 }
126             return ret;
127         }
128 
iterator()129     public Iterator iterator()
130         {
131             return new AbstractListIterator(this,0);
132         }
133 
listIterator()134     public ListIterator listIterator()
135         {
136             return listIterator(0);
137         }
138 
listIterator(int index)139     public ListIterator listIterator(int index) throws IndexOutOfBoundsException
140         {
141              if(index<0||index>size())    throw new IndexOutOfBoundsException();
142             return new AbstractListListIterator(this,index);
143         }
144 
subList(int fromIndex,int toIndex)145     public List subList(int fromIndex,int toIndex) throws IndexOutOfBoundsException,IllegalArgumentException
146         {
147           if(fromIndex < 0 || toIndex > size())
148                 throw new IndexOutOfBoundsException();
149             if(fromIndex>toIndex)
150                 throw new IllegalArgumentException();
151               return (List) new Sublist(this,fromIndex,toIndex);
152         }
153 
equals(Object o)154     public boolean equals(Object o)
155         {
156             if(o==this)
157                 return true;
158             if(!(o instanceof List))
159                 return false;
160             Iterator it1=iterator();
161             Iterator it2=((List)o).iterator();
162             while(it1.hasNext())
163                 {
164                     if(!it2.hasNext())
165                         return false;
166                     Object e1=it1.next();
167                     Object e2=it2.next();
168                     if(e1==null)
169                         {
170                             if(e2!=null)
171                                 return false;
172                         }
173                     if(!e1.equals(e2))
174                         return false;
175                 }
176             return true;
177         }
178 
hashCode()179     public int hashCode()
180         {
181           int hashCode = 1;
182             Iterator it = iterator();
183             while (it.hasNext())
184                 {
185                     Object o = it.next();
186                     hashCode = 31*hashCode + (o==null ? 0 : o.hashCode());
187                 }
188             return hashCode;
189     }
190 
removeRange(int fromIndex,int toIndex)191     protected void removeRange(int fromIndex,int toIndex)
192         {
193             if(fromIndex==toIndex) return;
194 
195              ListIterator li=listIterator(fromIndex);
196 
197             int i=fromIndex;
198             do
199               {
200                     li.next();
201                     li.remove();
202                     i++;
203                 }while(li.hasNext()&&i<toIndex);
204         }
205 
206 ////////////////////////////////////////////////////////////
207 /////////////innere Klasse AbstractIterator/////////////////
208 ////////////////////////////////////////////////////////////
209 
210 private class AbstractListIterator implements Iterator
211     {
212         AbstractList m_al=null;
213         int m_nextIndex=0;
AbstractListIterator(AbstractList al,int index)214         public AbstractListIterator(AbstractList al,int index)
215             {
216                 m_al=al;
217                 m_nextIndex=index;
218             }
219 
hasNext()220         public boolean hasNext()
221             {
222                 return m_nextIndex < m_al.size();
223             }
224 
next()225     public Object next()
226             {
227         return m_al.get(m_nextIndex++);
228             }
229 
remove()230         public void remove()
231           {
232               m_al.remove(m_nextIndex - 1);
233             }
234     }
235 
236 ///////////////////////////////////////////////////////////////////////////////
237 //////////// innere Klasse AbstraktListListIterator
238 ///////////////////////////////////////////////////////////////////////////////
239 
240 
241 private class AbstractListListIterator extends AbstractListIterator implements ListIterator
242     {
AbstractListListIterator(AbstractList al,int index)243         public AbstractListListIterator(AbstractList al,int index)
244             {
245                 super(al,index);
246             }
247 
hasPrevious()248         public boolean hasPrevious()
249             {
250                 return m_nextIndex>0;
251             }
252 
previous()253         public Object previous()// throws NoSuchElementException;
254           {
255                 return m_al.get(--m_nextIndex);
256             }
257 
nextIndex()258         public int nextIndex()
259             {
260                 return m_nextIndex;
261             }
262 
previousIndex()263         public int previousIndex()
264             {
265                 return m_nextIndex-1;
266             }
267 
set(Object o)268         public void set(Object o) //throws UnsupportedOperationException, ClassCastException, IllegalArgumentException,IllegalStateException;
269           {
270                 m_al.set(m_nextIndex-1,o);
271             }
272 
add(Object o)273         public void add(Object o)// throws UnsupportedOperationException, ClassCastException, IllegalArgumentException;
274           {
275                 m_al.add(m_nextIndex-1,o);
276             }
277     }
278 
279 
280 }
281