changeset 355:1bce334a816b

AbstractLuanTable now implements LuanRepr
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 14 Apr 2015 09:33:34 -0600
parents 705e4d6c3dbb
children 5e34702423a0
files core/src/luan/AbstractLuanTable.java core/src/luan/LuanPropertyTable.java core/src/luan/LuanTableImpl.java
diffstat 3 files changed, 68 insertions(+), 63 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/AbstractLuanTable.java	Tue Apr 14 05:44:06 2015 -0600
+++ b/core/src/luan/AbstractLuanTable.java	Tue Apr 14 09:33:34 2015 -0600
@@ -14,7 +14,7 @@
 import java.util.regex.Pattern;
 
 
-public abstract class AbstractLuanTable implements LuanTable {
+public abstract class AbstractLuanTable implements LuanTable, LuanRepr {
 
 	protected final Map<Object,Object> newMap() {
 		return new LinkedHashMap<Object,Object>();
@@ -44,6 +44,11 @@
 		return map;
 	}
 
+	// shouldn't include list
+	protected Map<Object,Object> map() {
+		return asMap();
+	}
+
 	protected abstract String type();
 
 	@Override public final String toString() {
@@ -96,4 +101,59 @@
 	@Override public void setJava() {
 		throw new UnsupportedOperationException();
 	}
+
+	@Override public String repr() {
+		return repr( Collections.newSetFromMap(new IdentityHashMap<AbstractLuanTable,Boolean>()) );
+	}
+
+	private String repr(Set<AbstractLuanTable> set) {
+		if( !set.add(this) ) {
+			return "\"<circular reference>\"";
+		}
+		StringBuilder sb = new StringBuilder();
+		sb.append('{');
+		boolean isFirst = true;
+		for( Object obj : asList() ) {
+			if( isFirst ) {
+				isFirst = false;
+			} else {
+				sb.append(", ");
+			}
+			sb.append(repr(set,obj));
+		}
+		for( Map.Entry<Object,Object> entry : map().entrySet() ) {
+			if( isFirst ) {
+				isFirst = false;
+			} else {
+				sb.append(", ");
+			}
+			sb.append(reprKey(set,entry.getKey())).append('=').append(repr(set,entry.getValue()));
+		}
+		sb.append('}');
+		return sb.toString();
+	}
+
+	private static final Pattern namePtn = Pattern.compile("[a-zA-Z_][a-zA-Z_0-9]*");
+
+	private String reprKey(Set<AbstractLuanTable> set,Object obj) {
+		if( obj instanceof String ) {
+			String s = (String)obj;
+			if( namePtn.matcher(s).matches() )
+				return s;
+		}
+		return "[" + repr(set,obj) + "]";
+	}
+
+	String repr(Set<AbstractLuanTable> set,Object obj) {
+		if( obj instanceof AbstractLuanTable ) {
+			AbstractLuanTable t = (AbstractLuanTable)obj;
+			return t.repr(set);
+		} else {
+			String s = Luan.repr(obj);
+			if( s == null )
+				s = "<couldn't repr: " + Luan.stringEncode(Luan.toString(obj)) + ">";
+			return s;
+		}
+	}
+
 }
--- a/core/src/luan/LuanPropertyTable.java	Tue Apr 14 05:44:06 2015 -0600
+++ b/core/src/luan/LuanPropertyTable.java	Tue Apr 14 09:33:34 2015 -0600
@@ -16,7 +16,7 @@
 		return obj instanceof LuanProperty ? ((LuanProperty)obj).get() : obj;
 	}
 
-	@Override String repr(Set<LuanTableImpl> set,Object obj) {
+	@Override String repr(Set<AbstractLuanTable> set,Object obj) {
 		return super.repr(set,fixValue(obj));
 	}
 
--- a/core/src/luan/LuanTableImpl.java	Tue Apr 14 05:44:06 2015 -0600
+++ b/core/src/luan/LuanTableImpl.java	Tue Apr 14 09:33:34 2015 -0600
@@ -14,7 +14,7 @@
 import java.util.regex.Pattern;
 
 
-class LuanTableImpl extends AbstractLuanTable implements LuanTable, DeepCloneable<LuanTableImpl>, LuanRepr {
+class LuanTableImpl extends AbstractLuanTable implements LuanTable, DeepCloneable<LuanTableImpl> {
 	private Map<Object,Object> map = null;
 	private List<Object> list = null;
 	private LuanTable metatable = null;
@@ -90,7 +90,7 @@
 
 	@Override public Map<Object,Object> asMap() {
 		if( list == null || list.isEmpty() )
-			return map!=null ? map : Collections.emptyMap();
+			return map();
 		Map<Object,Object> rtn = map!=null ? newMap(map) : newMap();
 		for( ListIterator iter = list.listIterator(); iter.hasNext(); ) {
 			int i = iter.nextIndex();
@@ -99,6 +99,10 @@
 		return rtn;
 	}
 
+	@Override protected Map<Object,Object> map() {
+		return map!=null ? map : Collections.emptyMap();
+	}
+
 	public boolean isSet() {
 		if( list != null ) {
 			for( Object obj : list ) {
@@ -129,65 +133,6 @@
 		return "table";
 	}
 
-	@Override public String repr() {
-		return repr( Collections.newSetFromMap(new IdentityHashMap<LuanTableImpl,Boolean>()) );
-	}
-
-	private String repr(Set<LuanTableImpl> set) {
-		if( !set.add(this) ) {
-			return "\"<circular reference>\"";
-		}
-		StringBuilder sb = new StringBuilder();
-		sb.append('{');
-		boolean isFirst = true;
-		if( list != null ) {
-			for( int i=0; i<list.size(); i++ ) {
-				Object obj = list.get(i);
-				if( isFirst ) {
-					isFirst = false;
-				} else {
-					sb.append(", ");
-				}
-				sb.append(repr(set,obj));
-			}
-		}
-		if( map != null ) {
-			for( Map.Entry<Object,Object> entry : map.entrySet() ) {
-				if( isFirst ) {
-					isFirst = false;
-				} else {
-					sb.append(", ");
-				}
-				sb.append(reprKey(set,entry.getKey())).append('=').append(repr(set,entry.getValue()));
-			}
-		}
-		sb.append('}');
-		return sb.toString();
-	}
-
-	private static final Pattern namePtn = Pattern.compile("[a-zA-Z_][a-zA-Z_0-9]*");
-
-	private String reprKey(Set<LuanTableImpl> set,Object obj) {
-		if( obj instanceof String ) {
-			String s = (String)obj;
-			if( namePtn.matcher(s).matches() )
-				return s;
-		}
-		return "[" + repr(set,obj) + "]";
-	}
-
-	String repr(Set<LuanTableImpl> set,Object obj) {
-		if( obj instanceof LuanTableImpl ) {
-			LuanTableImpl t = (LuanTableImpl)obj;
-			return t.repr(set);
-		} else {
-			String s = Luan.repr(obj);
-			if( s == null )
-				s = "<couldn't repr: " + Luan.stringEncode(Luan.toString(obj)) + ">";
-			return s;
-		}
-	}
-
 	@Override public Object get(Object key) {
 		if( list != null ) {
 			Integer iT = Luan.asInteger(key);