1 9 package javolution.lang; 10 11 import j2me.lang.CharSequence; 12 import javolution.realtime.RealtimeObject; 13 import java.io.IOException; 14 15 50 public abstract class TextFormat { 51 52 55 protected TextFormat() { 56 } 57 58 64 public abstract Text format(Object obj); 65 66 78 public abstract Object parse(CharSequence csq, Cursor pos); 79 80 92 public final Object parse(CharSequence csq) { 93 Cursor cursor = Cursor.newInstance(); 94 try { 95 Object obj = parse(csq, cursor); 96 if (cursor.getIndex() == csq.length()) { 97 return obj; 98 } else { 99 throw new IllegalArgumentException("Parsing of " + csq 100 + " incomplete (terminated at index: " 101 + cursor.getIndex() + ")"); 102 } 103 } finally { 104 cursor.recycle(); 105 } 106 } 107 108 117 public final Appendable format(Object obj, Appendable dest) throws IOException { 118 Text txt = format(obj); 119 return dest.append(txt); 120 } 121 122 126 public static final class Cursor extends RealtimeObject { 127 128 131 private static final Factory FACTORY = new Factory() { 132 public Object create() { 133 return new Cursor(); 134 } 135 }; 136 137 140 private int _index; 141 142 150 public static Cursor newInstance() { 151 Cursor cursor = (Cursor) FACTORY.object(); 152 cursor._index = 0; 153 return cursor; 154 } 155 156 161 public int getIndex() { 162 return _index; 163 } 164 165 170 public void setIndex(int i) { 171 _index = i; 172 } 173 174 179 public void increment(int i) { 180 _index += i; 181 } 182 183 186 public void recycle() { 187 super.recycle(); 188 } 189 } 190 } | Popular Tags |