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