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 @Override 030 public boolean add(Object o) { 031 try { 032 array.append(o); 033 } 034 catch (PageException e) { 035 return false; 036 } 037 return true; 038 } 039 040 public void add(int index, Object element) { 041 try { 042 array.insert(index+1, element); 043 } catch (PageException e) { 044 throw new IndexOutOfBoundsException(e.getMessage()); 045 } 046 } 047 048 @Override 049 public boolean addAll(Collection c) { 050 Iterator it = c.iterator(); 051 while(it.hasNext()) { 052 add(it.next()); 053 } 054 return !c.isEmpty(); 055 } 056 057 @Override 058 public boolean addAll(int index, Collection c) { 059 Iterator it = c.iterator(); 060 while(it.hasNext()) { 061 add(index++,it.next()); 062 } 063 return !c.isEmpty(); 064 } 065 066 @Override 067 public void clear() { 068 array.clear(); 069 } 070 071 @Override 072 public boolean contains(Object o) { 073 return indexOf(o)!=-1; 074 } 075 076 @Override 077 public boolean containsAll(Collection c) { 078 Iterator it = c.iterator(); 079 while(it.hasNext()) { 080 if(!contains(it.next()))return false; 081 } 082 return true; 083 } 084 085 @Override 086 public Object get(int index) { 087 try { 088 return array.getE(index+1); 089 } catch (PageException e) { 090 throw new IndexOutOfBoundsException(e.getMessage()); 091 } 092 } 093 094 public int indexOf(Object o) { 095 Iterator<Object> it=array.valueIterator(); 096 int index=0; 097 while(it.hasNext()) { 098 if(it.next().equals(o))return index; 099 index++; 100 } 101 return -1; 102 } 103 104 @Override 105 public boolean isEmpty() { 106 return array.size()==0; 107 } 108 109 @Override 110 public Iterator iterator() { 111 return array.valueIterator(); 112 } 113 114 public int lastIndexOf(Object o) { 115 Iterator<Object> it=array.valueIterator(); 116 int index=0; 117 int rtn=-1; 118 while(it.hasNext()) { 119 if(it.next().equals(o))rtn=index; 120 index++; 121 } 122 return rtn; 123 } 124 125 @Override 126 public ListIterator listIterator() { 127 return listIterator(0); 128 } 129 130 @Override 131 public ListIterator listIterator(int index) { 132 return new ArrayListIteratorImpl(array,index); 133 //return array.toList().listIterator(index); 134 } 135 136 137 @Override 138 public boolean remove(Object o) { 139 int index = indexOf(o); 140 if(index==-1) return false; 141 142 try { 143 array.removeE(index+1); 144 } catch (PageException e) { 145 return false; 146 } 147 return true; 148 } 149 150 public Object remove(int index) { 151 try { 152 return array.removeE(index+1); 153 } catch (PageException e) { 154 throw new IndexOutOfBoundsException(e.getMessage()); 155 } 156 } 157 158 public boolean removeAll(Collection c) { 159 Iterator it = c.iterator(); 160 boolean rtn=false; 161 while(it.hasNext()) { 162 if(remove(it.next()))rtn=true; 163 } 164 return rtn; 165 } 166 167 public boolean retainAll(Collection c) {new ArrayList().retainAll(c); 168 boolean modified = false; 169 Iterator it = iterator(); 170 while (it.hasNext()) { 171 if(!c.contains(it.next())) { 172 it.remove(); 173 modified = true; 174 } 175 } 176 return modified; 177 } 178 179 public Object set(int index, Object element) { 180 try { 181 if(!array.containsKey(index+1)) throw new IndexOutOfBoundsException("Index: "+(index+1)+", Size: "+size()); 182 return array.setE(index+1,element); 183 } catch (PageException e) { 184 throw new PageRuntimeException(e); 185 } 186 } 187 188 @Override 189 public int size() { 190 return array.size(); 191 } 192 193 @Override 194 public List subList(int fromIndex, int toIndex) { 195 return array.toList().subList(fromIndex, toIndex); 196 } 197 198 @Override 199 public Object[] toArray() { 200 return array.toArray(); 201 } 202 203 @Override 204 public Object[] toArray(Object[] a) { 205 return array.toArray(); 206 } 207 }