changeset 284:8870840251ea

use LinkedHashMap in LuanTable to preserve order git-svn-id: https://luan-java.googlecode.com/svn/trunk@285 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Tue, 02 Dec 2014 03:55:33 +0000
parents b669cdaf54b7
children 582e8db4cdb6
files core/src/luan/AbstractLuanTable.java core/src/luan/LuanTableImpl.java
diffstat 2 files changed, 18 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
diff -r b669cdaf54b7 -r 8870840251ea core/src/luan/AbstractLuanTable.java
--- a/core/src/luan/AbstractLuanTable.java	Tue Dec 02 03:34:04 2014 +0000
+++ b/core/src/luan/AbstractLuanTable.java	Tue Dec 02 03:55:33 2014 +0000
@@ -3,7 +3,7 @@
 import java.util.Iterator;
 import java.util.ListIterator;
 import java.util.Map;
-import java.util.HashMap;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -16,6 +16,14 @@
 
 public abstract class AbstractLuanTable implements LuanTable {
 
+	protected final Map<Object,Object> newMap() {
+		return new LinkedHashMap<Object,Object>();
+	}
+
+	protected final Map<Object,Object> newMap(Map<Object,Object> map) {
+		return new LinkedHashMap<Object,Object>(map);
+	}
+
 	@Override public boolean isEmpty() {
 		return isList() && length()==0;
 	}
@@ -29,7 +37,7 @@
 	}
 
 	@Override public Map<Object,Object> asMap() {
-		Map<Object,Object> map = new HashMap<Object,Object>();
+		Map<Object,Object> map = newMap();
 		for( Map.Entry<Object,Object> entry : this ) {
 			map.put(entry.getKey(),entry.getValue());
 		}
@@ -78,6 +86,6 @@
 	}
 
 	@Override public LuanTable cloneTable() {
-		return isList() ? new LuanTableImpl(new ArrayList<Object>(asList())) : new LuanTableImpl(new HashMap<Object,Object>(asMap()));
+		return isList() ? new LuanTableImpl(new ArrayList<Object>(asList())) : new LuanTableImpl(newMap(asMap()));
 	}
 }
diff -r b669cdaf54b7 -r 8870840251ea core/src/luan/LuanTableImpl.java
--- a/core/src/luan/LuanTableImpl.java	Tue Dec 02 03:34:04 2014 +0000
+++ b/core/src/luan/LuanTableImpl.java	Tue Dec 02 03:55:33 2014 +0000
@@ -4,7 +4,6 @@
 import java.util.ListIterator;
 import java.util.Map;
 import java.util.AbstractMap;
-import java.util.HashMap;
 import java.util.List;
 import java.util.ArrayList;
 import java.util.Collections;
@@ -24,14 +23,14 @@
 /*
 	public LuanTableImpl(LuanTableImpl tbl) {
 		if( tbl.map != null )
-			this.map = new HashMap<Object,Object>(tbl.map);
+			this.map = newMap(tbl.map);
 		if( tbl.list != null )
 			this.list = new ArrayList<Object>(tbl.list);
 	}
 */
 	LuanTableImpl(List<Object> list) {
 		this.list = list;
-		this.map = new HashMap<Object,Object>();
+		this.map = newMap();
 		map.put("n",list.size());
 		for( int i=0; i<list.size(); i++ ) {
 			if( list.get(i) == null ) {
@@ -51,7 +50,7 @@
 	}
 
 	LuanTableImpl(Set<Object> set) {
-		map = new HashMap<Object,Object>();
+		map = newMap();
 		for( Object obj : set ) {
 			if( obj != null )
 				map.put(obj,Boolean.TRUE);
@@ -64,7 +63,7 @@
 
 	@Override public void deepenClone(LuanTableImpl clone,DeepCloner cloner) {
 		if( map != null ) {
-			clone.map = new HashMap<Object,Object>();
+			clone.map = newMap();
 			for( Map.Entry<Object,Object> entry : map.entrySet() ) {
 				clone.map.put( cloner.get(entry.getKey()), cloner.get(entry.getValue()) );
 			}
@@ -90,7 +89,7 @@
 	@Override public Map<Object,Object> asMap() {
 		if( list == null || list.isEmpty() )
 			return map!=null ? map : Collections.emptyMap();
-		Map<Object,Object> rtn = map!=null ? new HashMap<Object,Object>(map) : new HashMap<Object,Object>();
+		Map<Object,Object> rtn = map!=null ? newMap(map) : newMap();
 		for( ListIterator iter = list.listIterator(); iter.hasNext(); ) {
 			int i = iter.nextIndex();
 			rtn.put(i+1,iter.next());
@@ -222,7 +221,7 @@
 			}
 		}
 		if( map==null ) {
-			map = new HashMap<Object,Object>();
+			map = newMap();
 		}
 		if( key instanceof Number && !(key instanceof Double) ) {
 			Number n = (Number)key;
@@ -253,7 +252,7 @@
 				Object v = list.remove(i);
 				if( v != null ) {
 					if( map==null )
-						map = new HashMap<Object,Object>();
+						map = newMap();
 					map.put(i+1,v);
 				}
 			}