001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.type.wrap; 020 021import java.util.ArrayList; 022import java.util.Collection; 023import java.util.Iterator; 024import java.util.List; 025import java.util.ListIterator; 026 027import lucee.runtime.exp.PageException; 028import lucee.runtime.exp.PageRuntimeException; 029import lucee.runtime.op.Duplicator; 030import lucee.runtime.type.Array; 031 032public class ArrayAsArrayList extends ArrayList { 033 034 Array array; 035 036 private ArrayAsArrayList(Array array) { 037 this.array=array; 038 } 039 040 public static ArrayList toArrayList(Array array) { 041 return new ArrayAsArrayList(array); 042 } 043 044 045 @Override 046 public boolean add(Object o) { 047 try { 048 array.append(o); 049 } 050 catch (PageException e) { 051 return false; 052 } 053 return true; 054 } 055 056 public void add(int index, Object element) { 057 try { 058 array.insert(index+1, element); 059 } catch (PageException e) { 060 throw new IndexOutOfBoundsException(e.getMessage()); 061 } 062 } 063 064 @Override 065 public boolean addAll(Collection c) { 066 Iterator it = c.iterator(); 067 while(it.hasNext()) { 068 add(it.next()); 069 } 070 return !c.isEmpty(); 071 } 072 073 @Override 074 public boolean addAll(int index, Collection c) { 075 Iterator it = c.iterator(); 076 while(it.hasNext()) { 077 add(index++,it.next()); 078 } 079 return !c.isEmpty(); 080 } 081 082 @Override 083 public void clear() { 084 array.clear(); 085 } 086 087 @Override 088 public boolean contains(Object o) { 089 return indexOf(o)!=-1; 090 } 091 092 @Override 093 public boolean containsAll(Collection c) { 094 Iterator it = c.iterator(); 095 while(it.hasNext()) { 096 if(!contains(it.next()))return false; 097 } 098 return true; 099 } 100 101 @Override 102 public Object get(int index) { 103 try { 104 return array.getE(index+1); 105 } catch (PageException e) { 106 throw new IndexOutOfBoundsException(e.getMessage()); 107 } 108 } 109 110 public int indexOf(Object o) { 111 Iterator<Object> it=array.valueIterator(); 112 int index=0; 113 while(it.hasNext()) { 114 if(it.next().equals(o))return index; 115 index++; 116 } 117 return -1; 118 } 119 120 @Override 121 public boolean isEmpty() { 122 return array.size()==0; 123 } 124 125 @Override 126 public Iterator iterator() { 127 return array.valueIterator(); 128 } 129 130 public int lastIndexOf(Object o) { 131 Iterator<Object> it=array.valueIterator(); 132 int index=0; 133 int rtn=-1; 134 while(it.hasNext()) { 135 if(it.next().equals(o))rtn=index; 136 index++; 137 } 138 return rtn; 139 } 140 141 @Override 142 public ListIterator listIterator() { 143 return listIterator(0); 144 } 145 146 @Override 147 public ListIterator listIterator(int index) { 148 return array.toList().listIterator(index); 149 } 150 151 152 @Override 153 public boolean remove(Object o) { 154 int index = indexOf(o); 155 if(index==-1) return false; 156 157 try { 158 array.removeE(index+1); 159 } catch (PageException e) { 160 return false; 161 } 162 return true; 163 } 164 165 public Object remove(int index) { 166 try { 167 return array.removeE(index+1); 168 } catch (PageException e) { 169 throw new IndexOutOfBoundsException(e.getMessage()); 170 } 171 } 172 173 public boolean removeAll(Collection c) { 174 Iterator it = c.iterator(); 175 boolean rtn=false; 176 while(it.hasNext()) { 177 if(remove(it.next()))rtn=true; 178 } 179 return rtn; 180 } 181 182 public boolean retainAll(Collection c) {new ArrayList().retainAll(c); 183 boolean modified = false; 184 Iterator it = iterator(); 185 while (it.hasNext()) { 186 if(!c.contains(it.next())) { 187 it.remove(); 188 modified = true; 189 } 190 } 191 return modified; 192 } 193 194 public Object set(int index, Object element) { 195 try { 196 if(!array.containsKey(index+1)) throw new IndexOutOfBoundsException("Index: "+(index+1)+", Size: "+size()); 197 return array.setE(index+1,element); 198 } catch (PageException e) { 199 throw new PageRuntimeException(e); 200 } 201 } 202 203 @Override 204 public int size() { 205 return array.size(); 206 } 207 208 @Override 209 public List subList(int fromIndex, int toIndex) { 210 return array.toList().subList(fromIndex, toIndex); 211 } 212 213 @Override 214 public Object[] toArray() { 215 return array.toArray(); 216 } 217 218 @Override 219 public Object[] toArray(Object[] a) { 220 return array.toArray(); 221 } 222 223 @Override 224 public Object clone() { 225 return toArrayList((Array) Duplicator.duplicate(array,true)); 226 } 227 228 @Override 229 public void ensureCapacity(int minCapacity) { 230 throw new PageRuntimeException("not supported"); 231 } 232 233 @Override 234 public void trimToSize() { 235 throw new PageRuntimeException("not supported"); 236 } 237}