checkpoint
[sbp.git] / src / edu / berkeley / sbp / util / PrintableTree.java
index 73b7f02..180d2ea 100644 (file)
@@ -9,7 +9,9 @@ public abstract class PrintableTree<T extends PrintableTree> implements Iterable
 
     protected abstract String headToString();
     protected abstract String headToJava();    
-    protected abstract int    numChildren();
+    protected abstract String left();
+    protected abstract String right();
+    protected abstract boolean ignoreSingleton();
 
     private static final int MAXCHARS=40;
 
@@ -20,8 +22,14 @@ public abstract class PrintableTree<T extends PrintableTree> implements Iterable
         if (str.length() < MAXCHARS) return str;
         String head = headToString();
         StringBuffer ret = new StringBuffer();
-        if (numChildren()==0) return head==null ? "{}" : head;
-        ret.append(head==null?"{ ":(head+":"+nl));
+
+        Iterator<T> iterator = iterator();
+        if (!iterator.hasNext()) return head==null ? (left()+right()) : head;
+        PrintableTree t0 = iterator.next();
+        if (!iterator.hasNext() && ignoreSingleton())
+            return t0.toPrettyString(nl);
+
+        ret.append(head==null?(left()+" "):(head+":"+nl));
         boolean first = true;
         int len = 0;
         for(T t : this) {
@@ -35,20 +43,23 @@ public abstract class PrintableTree<T extends PrintableTree> implements Iterable
             ret.append(s);
             len += s.length();
         }
-        if (head==null) ret.append(" }");
+        if (head==null) ret.append(" "+right());
         return ret.toString();
     }
 
     public String toString() {
         StringBuffer ret = new StringBuffer();
+        int count=0;
         for(T t : this) {
+            count++;
             String q = t==null ? "null" : t.toString();
             if (q.length() > 0) { ret.append(q); ret.append(" "); }
         }
+        if (count==1 && ignoreSingleton()) return ret.toString().trim();
         String tail = ret.toString().trim();
         String head = headToString();
         String h = (head!=null && !head.toString().equals("")) ? (tail.length() > 0 ? head+":" : head+"") : "";
-        if (tail.length() > 0) tail = "{" + tail + "}";
+        if (tail.length() > 0) tail = left() + tail + right();
         return h + tail;
     }