1 29 30 package com.caucho.quercus.env; 31 32 import com.caucho.quercus.program.JavaClassDef; 33 34 import com.caucho.quercus.QuercusRuntimeException; 35 36 import java.util.*; 37 import java.util.logging.*; 38 39 42 public class JavaListAdapter 43 extends JavaCollectionAdapter 44 { 45 private static final Logger log 46 = Logger.getLogger(JavaListAdapter.class.getName()); 47 48 private List _list; 50 51 private int _next = 0; 52 53 public JavaListAdapter(Env env, List list) 54 { 55 this(env, list, env.getJavaClassDefinition(list.getClass().getName())); 56 } 57 58 public JavaListAdapter(Env env, List list, JavaClassDef def) 59 { 60 super(env, list, def); 61 _list = list; 62 } 63 64 67 public Value putImpl(Value key, Value value) 68 { 69 int pos = key.toInt(); 70 int size = size(); 71 72 if (0 <= pos && pos <= size) { 73 if (pos < size) 74 _list.remove(pos); 75 76 _list.add(pos, value.toJavaObject()); 77 78 return value; 79 } 80 else { 81 getEnv().warning(L.l("index {0} is out of range", pos)); 82 log.log(Level.FINE, L.l("index {0} is out of range", pos)); 83 84 return UnsetValue.UNSET; 85 } 86 } 87 88 91 public Value get(Value key) 92 { 93 int pos = key.toInt(); 94 95 if (0 <= pos && pos < size()) 96 return wrapJava(_list.get(pos)); 97 else 98 return UnsetValue.UNSET; 99 } 100 101 104 public Value remove(Value key) 105 { 106 int pos = key.toInt(); 107 108 if (0 <= pos && pos < size()) 109 return wrapJava(_list.remove(pos)); 110 else 111 return UnsetValue.UNSET; 112 } 113 114 117 public Value pop() 118 { 119 if (size() == 0) 120 return BooleanValue.FALSE; 121 122 return wrapJava(_list.remove(0)); 123 } 124 125 134 public Value contains(Value value) 135 { 136 for (Map.Entry<Value,Value> entry : entrySet()) { 137 if (entry.getValue().equals(value)) 138 return entry.getKey(); 139 } 140 141 return NullValue.NULL; 142 } 143 144 147 public Value current() 148 { 149 if (_next < _list.size()) 150 return wrapJava(_list.get(_next)); 151 else 152 return BooleanValue.FALSE; 153 } 154 155 158 public Value key() 159 { 160 if (_next < _list.size()) 161 return LongValue.create(_next); 162 else 163 return NullValue.NULL; 164 } 165 166 169 public boolean hasCurrent() 170 { 171 return _next < _list.size(); 172 } 173 174 177 public Value next() 178 { 179 if (_next < _list.size()) 180 return wrapJava(_list.get(_next++)); 181 else 182 return BooleanValue.FALSE; 183 } 184 185 188 public Value prev() 189 { 190 if (_next > 0) 191 return wrapJava(_list.get(_next--)); 192 else 193 return BooleanValue.FALSE; 194 } 195 196 199 public Value each() 200 { 201 if (_next < _list.size()) 202 { 203 ArrayValue result = new ArrayValueImpl(); 204 205 result.put(LongValue.ZERO, key()); 206 result.put(KEY, key()); 207 208 result.put(LongValue.ONE, current()); 209 result.put(VALUE, current()); 210 211 _next++; 212 213 return result; 214 } 215 else 216 return NullValue.NULL; 217 } 218 219 222 public Value reset() 223 { 224 _next = 0; 225 226 return current(); 227 } 228 229 232 public Value end() 233 { 234 _next = _list.size(); 235 236 return current(); 237 } 238 239 242 @Override 243 public Value copy() 244 { 245 try { 246 Class cl = _list.getClass(); 247 248 List list = (List)cl.newInstance(); 249 250 list.addAll(_list); 251 252 return new JavaListAdapter(getEnv(), list, getClassDef()); 253 } 254 catch (Exception e) { 255 throw new QuercusRuntimeException(e); 256 } 257 } 258 } 259 | Popular Tags |