fixed tibdoc
[sbp.git] / src / edu / berkeley / sbp / util / PrintableTree.java
index 9d37a05..a6c2f6e 100644 (file)
@@ -48,7 +48,9 @@ public abstract class PrintableTree<T extends PrintableTree> implements Iterable
         return ret.toString();
     }
 
+    private String cached = null;
     public String toString() {
+        if (cached!=null) return cached;
         StringBuffer ret = new StringBuffer();
         int count=0;
         for(T t : this) {
@@ -56,12 +58,12 @@ public abstract class PrintableTree<T extends PrintableTree> implements Iterable
             String q = t==null ? "null" : t.toString();
             if (q.length() > 0) { ret.append(q); ret.append(" "); }
         }
-        if (count==1 && ignoreSingleton()) return ret.toString().trim();
+        if (count==1 && ignoreSingleton()) { return cached = 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 = left() + tail + right();
-        return h + tail;
+        return cached = h + tail;
     }
 
     /** append Java code to <tt>sb</tt> which evaluates to this instance */