Mercurial Hosting > luan
comparison core/src/luan/LuanTableImpl.java @ 222:b76fcb72d97d
add AbstractLuanTable and improve HttpLuan
git-svn-id: https://luan-java.googlecode.com/svn/trunk@223 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Mon, 21 Jul 2014 02:23:05 +0000 |
parents | ec016471c6eb |
children | 05eb2837ddbf |
comparison
equal
deleted
inserted
replaced
221:ec016471c6eb | 222:b76fcb72d97d |
---|---|
1 package luan; | 1 package luan; |
2 | 2 |
3 import java.util.Iterator; | 3 import java.util.Iterator; |
4 import java.util.ListIterator; | 4 import java.util.ListIterator; |
5 import java.util.Map; | 5 import java.util.Map; |
6 import java.util.AbstractMap; | |
6 import java.util.HashMap; | 7 import java.util.HashMap; |
7 import java.util.List; | 8 import java.util.List; |
8 import java.util.ArrayList; | 9 import java.util.ArrayList; |
9 import java.util.Collections; | 10 import java.util.Collections; |
10 import java.util.Comparator; | 11 import java.util.Comparator; |
12 import java.util.HashSet; | 13 import java.util.HashSet; |
13 import java.util.IdentityHashMap; | 14 import java.util.IdentityHashMap; |
14 import java.util.regex.Pattern; | 15 import java.util.regex.Pattern; |
15 | 16 |
16 | 17 |
17 public final class LuanTableImpl implements LuanTable, DeepCloneable<LuanTableImpl>, LuanRepr { | 18 final class LuanTableImpl extends AbstractLuanTable implements LuanTable, DeepCloneable<LuanTableImpl>, LuanRepr { |
18 private Map<Object,Object> map = null; | 19 private Map<Object,Object> map = null; |
19 private List<Object> list = null; | 20 private List<Object> list = null; |
20 private LuanTable metatable = null; | 21 private LuanTable metatable = null; |
21 | 22 |
22 public LuanTableImpl() {} | 23 public LuanTableImpl() {} |
121 rtn.add(i); | 122 rtn.add(i); |
122 } | 123 } |
123 return rtn; | 124 return rtn; |
124 } | 125 } |
125 | 126 |
126 @Override public String toString() { | 127 @Override protected String type() { |
127 return "table: " + Integer.toHexString(hashCode()); | 128 return "table"; |
128 } | 129 } |
129 | 130 |
130 @Override public String repr() { | 131 @Override public String repr() { |
131 return repr( Collections.newSetFromMap(new IdentityHashMap<LuanTableImpl,Boolean>()) ); | 132 return repr( Collections.newSetFromMap(new IdentityHashMap<LuanTableImpl,Boolean>()) ); |
132 } | 133 } |
275 return list; | 276 return list; |
276 } | 277 } |
277 | 278 |
278 @Override public void insert(int pos,Object value) { | 279 @Override public void insert(int pos,Object value) { |
279 if( value==null ) | 280 if( value==null ) |
280 throw new UnsupportedOperationException(); | 281 throw new IllegalArgumentException("can't insert a nil value"); |
281 list().add(pos-1,value); | 282 list().add(pos-1,value); |
282 mapToList(); | 283 mapToList(); |
283 } | 284 } |
284 | 285 |
285 @Override public void add(Object value) { | 286 @Override public void add(Object value) { |
286 if( value==null ) | 287 if( value==null ) |
287 throw new UnsupportedOperationException(); | 288 throw new IllegalArgumentException("can't add a nil value"); |
288 list().add(value); | 289 list().add(value); |
289 mapToList(); | 290 mapToList(); |
290 } | 291 } |
291 | 292 |
292 @Override public Object remove(int pos) { | 293 @Override public Object remove(int pos) { |
338 public boolean hasNext() { | 339 public boolean hasNext() { |
339 return iter.hasNext(); | 340 return iter.hasNext(); |
340 } | 341 } |
341 public Map.Entry<Object,Object> next() { | 342 public Map.Entry<Object,Object> next() { |
342 Double key = Double.valueOf(iter.nextIndex()+1); | 343 Double key = Double.valueOf(iter.nextIndex()+1); |
343 return new MapEntry(key,iter.next()); | 344 return new AbstractMap.SimpleEntry<Object,Object>(key,iter.next()); |
344 } | 345 } |
345 public void remove() { | 346 public void remove() { |
346 throw new UnsupportedOperationException(); | 347 throw new UnsupportedOperationException(); |
347 } | 348 } |
348 }; | 349 }; |
360 | 361 |
361 @Override public void setMetatable(LuanTable metatable) { | 362 @Override public void setMetatable(LuanTable metatable) { |
362 this.metatable = metatable; | 363 this.metatable = metatable; |
363 } | 364 } |
364 | 365 |
365 private static final class MapEntry implements Map.Entry<Object,Object> { | |
366 private final Object key; | |
367 private final Object value; | |
368 | |
369 MapEntry(Object key,Object value) { | |
370 this.key = key; | |
371 this.value = value; | |
372 } | |
373 | |
374 @Override public Object getKey() { | |
375 return key; | |
376 } | |
377 | |
378 @Override public Object getValue() { | |
379 return value; | |
380 } | |
381 | |
382 @Override public Object setValue(Object value) { | |
383 throw new UnsupportedOperationException(); | |
384 } | |
385 } | |
386 | |
387 public boolean isEmpty() { | 366 public boolean isEmpty() { |
388 return (list==null || list.isEmpty()) && (map==null || map.isEmpty()); | 367 return (list==null || list.isEmpty()) && (map==null || map.isEmpty()); |
389 } | 368 } |
390 } | 369 } |