1 19 package jline; 20 21 import java.io.*; 22 import java.util.*; 23 24 25 30 public class History 31 { 32 private List history = new ArrayList (); 33 private PrintWriter output = null; 34 private int maxSize = 500; 35 private int currentIndex = 0; 36 37 38 41 public History () 42 { 43 } 44 45 46 50 public History (File historyFile) 51 throws IOException 52 { 53 setHistoryFile (historyFile); 54 } 55 56 57 public void setHistoryFile (File historyFile) 58 throws IOException 59 { 60 if (historyFile.isFile ()) 61 load (new FileInputStream (historyFile)); 62 setOutput (new PrintWriter (new FileWriter (historyFile, false), true)); 63 flushBuffer (); 64 } 65 66 67 70 public void load (InputStream in) 71 throws IOException 72 { 73 load (new InputStreamReader (in)); 74 } 75 76 77 80 public void load (Reader reader) 81 throws IOException 82 { 83 BufferedReader breader = new BufferedReader (reader); 84 List lines = new ArrayList (); 85 String line; 86 while ((line = breader.readLine ()) != null) 87 { 88 lines.add (line); 89 } 90 91 for (Iterator i = lines.iterator (); i.hasNext (); ) 92 addToHistory ((String )i.next ()); 93 } 94 95 96 public int size () 97 { 98 return history.size (); 99 } 100 101 102 105 public void clear () 106 { 107 history.clear (); 108 currentIndex = 0; 109 } 110 111 112 116 public void addToHistory (String buffer) 117 { 118 if (history.size () != 0 && buffer.equals ( 120 history.get (history.size () - 1))) 121 return; 122 123 history.add (buffer); 124 while (history.size () > getMaxSize ()) 125 history.remove (0); 126 127 currentIndex = history.size (); 128 129 if (getOutput () != null) 130 { 131 getOutput ().println (buffer); 132 getOutput ().flush (); 133 } 134 } 135 136 137 140 public void flushBuffer () 141 throws IOException 142 { 143 if (getOutput () != null) 144 { 145 for (Iterator i = history.iterator (); i.hasNext (); 146 getOutput ().println ((String )i.next ())); 147 148 getOutput ().flush (); 149 } 150 } 151 152 153 156 public void moveToEnd () 157 { 158 currentIndex = history.size (); 159 } 160 161 162 165 public void setMaxSize (int maxSize) 166 { 167 this.maxSize = maxSize; 168 } 169 170 171 174 public int getMaxSize () 175 { 176 return this.maxSize; 177 } 178 179 180 184 public void setOutput (PrintWriter output) 185 { 186 this.output = output; 187 } 188 189 190 193 public PrintWriter getOutput () 194 { 195 return this.output; 196 } 197 198 199 202 public int getCurrentIndex () 203 { 204 return this.currentIndex; 205 } 206 207 208 211 public String current () 212 { 213 if (currentIndex >= history.size ()) 214 return ""; 215 216 return (String )history.get (currentIndex); 217 } 218 219 220 225 public boolean previous () 226 { 227 if (currentIndex <= 0) 228 return false; 229 230 currentIndex--; 231 return true; 232 } 233 234 235 240 public boolean next () 241 { 242 if (currentIndex >= history.size ()) 243 return false; 244 245 currentIndex++; 246 return true; 247 } 248 249 250 253 public List getHistoryList () 254 { 255 return Collections.unmodifiableList (history); 256 } 257 258 259 263 public String toString () 264 { 265 return history.toString (); 266 } 267 } 268 269 | Popular Tags |