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