001    package railo.runtime.type.wrap;
002    
003    import java.util.ArrayList;
004    import java.util.Collection;
005    import java.util.Iterator;
006    import java.util.List;
007    import java.util.ListIterator;
008    
009    import railo.runtime.exp.PageException;
010    import railo.runtime.exp.PageRuntimeException;
011    import railo.runtime.type.Array;
012    
013    public class ArrayAsArrayList extends ArrayList {
014    
015            Array array;
016            
017            private ArrayAsArrayList(Array array) {
018                    this.array=array;
019            }
020            
021            public static ArrayList toArrayList(Array array) {
022                    return new ArrayAsArrayList(array);
023            }
024            
025            
026            /**
027             * @see java.util.List#addEntry(E)
028             */
029            public boolean add(Object o) {
030                    try {
031                            array.append(o);
032                    } 
033                    catch (PageException e) {
034                            return false;
035                    }
036                    return true;
037            }
038    
039            public void add(int index, Object element) {
040                    try {
041                            array.insert(index+1, element);
042                    } catch (PageException e) {
043                            throw new IndexOutOfBoundsException(e.getMessage());
044                    }
045            }
046    
047            /**
048             * @see java.util.List#addAll(java.util.Collection)
049             */
050            public boolean addAll(Collection c) {
051                    Iterator it = c.iterator();
052                    while(it.hasNext()) {
053                            add(it.next());
054                    }
055                    return !c.isEmpty();
056            }
057    
058            /**
059             * @see java.util.List#addAll(int, java.util.Collection)
060             */
061            public boolean addAll(int index, Collection c) {
062                    Iterator it = c.iterator();
063                    while(it.hasNext()) {
064                            add(index++,it.next());
065                    }
066                    return !c.isEmpty();
067            }
068    
069            /**
070             * @see java.util.List#clear()
071             */
072            public void clear() {
073                    array.clear();
074            }
075    
076            /**
077             * @see java.util.List#contains(java.lang.Object)
078             */
079            public boolean contains(Object o) {
080                    return indexOf(o)!=-1;
081            }
082    
083            /**
084             * @see java.util.List#containsAll(java.util.Collection)
085             */
086            public boolean containsAll(Collection c) {
087                    Iterator it = c.iterator();
088                    while(it.hasNext()) {
089                            if(!contains(it.next()))return false;
090                    }
091                    return true;
092            }
093    
094            /**
095             * @see java.util.List#get(int)
096             */
097            public Object get(int index) {
098                    try {
099                            return array.getE(index+1);
100                    } catch (PageException e) {
101                            throw new IndexOutOfBoundsException(e.getMessage());
102                    }
103            }
104    
105            public int indexOf(Object o) {
106                    Iterator it=array.iterator();
107                    int index=0;
108                    while(it.hasNext()) {
109                            if(it.next().equals(o))return index;
110                            index++;
111                    }
112                    return -1;
113            }
114    
115            /**
116             * @see java.util.List#isEmpty()
117             */
118            public boolean isEmpty() {
119                    return array.size()==0;
120            }
121    
122            /**
123             * @see java.util.List#iterator()
124             */
125            public Iterator iterator() {
126                    return array.iterator();
127            }
128    
129            public int lastIndexOf(Object o) {
130                    Iterator it=array.iterator();
131                    int index=0;
132                    int rtn=-1;
133                    while(it.hasNext()) {
134                            if(it.next().equals(o))rtn=index;
135                            index++;
136                    }
137                    return rtn;
138            }
139    
140            /**
141             * @see java.util.List#listIterator()
142             */
143            public ListIterator listIterator() {
144                    return listIterator(0);
145            }
146    
147            /**
148             * @see java.util.List#listIterator(int)
149             */
150            public ListIterator listIterator(int index) {
151                    return array.toList().listIterator(index);
152            }
153            
154    
155            /**
156             * @see java.util.List#remove(java.lang.Object)
157             */
158            public boolean remove(Object o) {
159                    int index = indexOf(o);
160                    if(index==-1) return false;
161                    
162                    try {
163                            array.removeE(index+1);
164                    } catch (PageException e) {
165                            return false;
166                    }
167                    return true;
168            }
169    
170            public Object remove(int index) {
171                    try {
172                            return array.removeE(index+1);
173                    } catch (PageException e) {
174                            throw new IndexOutOfBoundsException(e.getMessage());
175                    }
176            }
177    
178            public boolean removeAll(Collection c) {
179                    Iterator it = c.iterator();
180                    boolean rtn=false;
181                    while(it.hasNext()) {
182                            if(remove(it.next()))rtn=true;
183                    }
184                    return rtn;
185            }
186    
187            public boolean retainAll(Collection c) {new ArrayList().retainAll(c);
188                    boolean modified = false;
189                    Iterator it = iterator();
190                    while (it.hasNext()) {
191                        if(!c.contains(it.next())) {
192                            it.remove();
193                            modified = true;
194                        }
195                    }
196                    return modified;
197            }
198    
199            public Object set(int index, Object element) {
200                    try {
201                            if(!array.containsKey(index+1)) throw new IndexOutOfBoundsException("Index: "+(index+1)+", Size: "+size());
202                            return array.setE(index+1,element);
203                    } catch (PageException e) {
204                            throw new PageRuntimeException(e);
205                    }
206            }
207    
208            /**
209             * @see java.util.List#size()
210             */
211            public int size() {
212                    return array.size();
213            }
214    
215            /**
216             * @see java.util.List#subList(int, int)
217             */
218            public List subList(int fromIndex, int toIndex) {
219                    return array.toList().subList(fromIndex, toIndex);
220            }
221    
222            /**
223             * @see java.util.List#toArray()
224             */
225            public Object[] toArray() {
226                    return array.toArray();
227            }
228    
229            /**
230             * @see java.util.List#toArray(T[])
231             */
232            public Object[] toArray(Object[] a) {
233                    return array.toArray();
234            }
235    
236            /**
237             *
238             * @see java.util.ArrayList#clone()
239             */
240            public Object clone() {
241                    return toArrayList((Array) array.duplicate(true));
242            }
243    
244            /**
245             *
246             * @see java.util.ArrayList#ensureCapacity(int)
247             */
248            public void ensureCapacity(int minCapacity) {
249                    throw new PageRuntimeException("not supported");
250            }
251    
252            /**
253             *
254             * @see java.util.ArrayList#trimToSize()
255             */
256            public void trimToSize() {
257                    throw new PageRuntimeException("not supported");
258            }
259    }