Attempt to clean up Tape.toString method by creating ListFormatter class
authorMichael Welch <michaelgwelch@gmail.com>
Mon, 27 Feb 2012 18:06:10 +0000 (12:06 -0600)
committerMichael Welch <michaelgwelch@gmail.com>
Mon, 27 Feb 2012 18:06:10 +0000 (12:06 -0600)
java/src/welch/brainmess/ListFormatter.java [new file with mode: 0644]
java/src/welch/brainmess/Tape.java

diff --git a/java/src/welch/brainmess/ListFormatter.java b/java/src/welch/brainmess/ListFormatter.java
new file mode 100644 (file)
index 0000000..7c9a58e
--- /dev/null
@@ -0,0 +1,57 @@
+package welch.brainmess;
+
+import java.util.LinkedList;
+import java.util.List;
+
+public class ListFormatter<T> {
+       private final ElementFormatter<T> _elementFormatter;
+       private final String _prefix;
+       private final String _suffix;
+       private final String _separator;
+       
+       public ListFormatter(String prefix, String suffix, String separator, 
+                       ElementFormatter<T> elementFormatter) {
+               if (prefix == null) throw new NullPointerException("prefix");
+               if (suffix == null) throw new NullPointerException("suffix");
+               if (separator == null) throw new NullPointerException("separator");
+               if (elementFormatter == null) throw new NullPointerException("elementFormatter");
+               
+               _prefix = prefix;
+               _suffix = suffix;
+               _separator = separator;
+               _elementFormatter = elementFormatter;
+       }
+
+       
+       public String format(List<T> list) {
+               StringBuilder builder = new StringBuilder(_prefix);
+               boolean first = true;
+               int index = 0;
+               for(T t : list) {
+                       if (first) {
+                               first = false;
+                       } else {
+                               builder.append(_separator);
+                       }
+                       builder.append(_elementFormatter.format(t, index));
+                       index++;
+               }
+               builder.append(_suffix);
+               return builder.toString();
+       }
+       
+       public interface ElementFormatter<E> {
+               String format(E t, int index);
+       }
+       
+       public class DefaultElementFormatter<E> implements ElementFormatter<E> {
+               @Override
+               public String format(E t, int index) {
+                       return t.toString();
+               }
+       }
+       
+       public static void main(String[] args) {
+               new LinkedList<Integer>(null);
+       }
+}
index 295ceb4..a618961 100644 (file)
@@ -2,6 +2,7 @@ package welch.brainmess;
 
 import java.util.LinkedList;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicReference;
 
 /**
  * Represents the tape of a Brainmess machine.
@@ -10,6 +11,14 @@ import java.util.List;
  */
 public class Tape
 {
+
+       
+       // Do not access this directly. Use tapeFormatter() getter.
+       // It sets this instance only when needed. It is also thread-safe in setting
+       // this value.
+       private final AtomicReference<ListFormatter<Integer>> TAPE_FORMATTER = new AtomicReference<ListFormatter<Integer>>();
+       
+       
        /**
         * The list used to store the tape values. This is 
         * only stored so that we can do a toString on it.
@@ -90,35 +99,30 @@ public class Tape
                return traveler.currentIndex();
        }
        
+       private ListFormatter<Integer> tapeFormatter() {
+               if (TAPE_FORMATTER.get() == null) {
+                       TAPE_FORMATTER.compareAndSet(null, new ListFormatter<Integer>("[", "]", ", ", new TapeElementFormatter()));
+               }
+               return TAPE_FORMATTER.get();
+       }
+       
        /**
         * Converts this instance into its string representation.
         */
        public String toString()
        {
-               StringBuilder builder = new StringBuilder();
-               int current = currentIndex();
-               
-               builder.append("[");
-               int i = 0;
-               boolean first = true;
-               for (int val : list)
-               {
-                       if (!first)
-                       {
-                               builder.append(", ");
-                       }
-                       else 
-                       {
-                               first = false;
-                       }
-                       if (i == current) builder.append("*");
-                       builder.append(val);
-                       if (i == current) builder.append("*");
-                       i++;
-               }
-               builder.append("]");
-               return builder.toString();
+               return tapeFormatter().format(list);
        }
        
+       private class TapeElementFormatter implements ListFormatter.ElementFormatter<Integer> {
+
+               @Override
+               public String format(Integer value, int index) {
+                       if (value == null) throw new NullPointerException("Attempted to format a null integer");
+                   final int currentIndex = currentIndex();
+                   return (currentIndex == index) ? String.format("*%d*", value) : value.toString();
+               }
+               
+       }
        
 }