1 21 22 package org.armedbear.j; 23 24 public final class History 25 { 26 private final int limit; 27 28 private final String name; 29 private final String [] strings; 30 private int count; 31 private int index; 33 public History(String name) 34 { 35 this(name, 10); 36 } 37 38 public History(String name, int limit) 39 { 40 this.name = name; 41 this.limit = limit; 42 strings = new String [limit]; 43 if (name != null) { 44 SessionProperties sessionProperties = Editor.getSessionProperties(); 45 int i; 46 for (i = 0; i < limit; i++) { 47 String key = "history." + name + "." + String.valueOf(i); 48 String value = sessionProperties.getStringProperty(key, null); 49 if (value == null) 50 break; 51 strings[i] = value; 52 } 53 count = i; 54 } 55 reset(); 56 } 57 58 public int size() 59 { 60 return count; 61 } 62 63 public void save() 64 { 65 if (name != null) { 66 SessionProperties sessionProperties = Editor.getSessionProperties(); 67 for (int i = 0; i < count; i++) { 68 if (strings[i] == null) 69 break; 70 String key = "history." + name + "." + String.valueOf(i); 71 sessionProperties.setStringProperty(key, strings[i]); 72 } 73 } 74 } 75 76 public void append(String s) 77 { 78 if (s.length() == 0) 79 return; 80 for (int i = 0; i < count; i++) { 81 if (s.equals(strings[i])) { 82 for (int j = i+1; j < count; j++) 83 strings[j-1] = strings[j]; 84 --count; 85 break; 86 } 87 } 88 if (count == limit) { 89 for (int i = 0; i < limit - 1; i++) 90 strings[i] = strings[i+1]; 91 --count; 92 } 93 strings[count++] = s; 94 reset(); 95 } 96 97 public String get(int i) 98 { 99 if (i < 0) 100 return null; 101 if (i > count - 1) 102 return null; 103 return strings[i]; 104 } 105 106 public String getPrevious() 107 { 108 if (index > 0) 109 return get(--index); 110 return null; 111 } 112 113 public String getNext() 114 { 115 if (index < count - 1) 116 return get(++index); 117 return null; 118 } 119 120 public final void reset() 121 { 122 index = count; 123 } 124 } 125 | Popular Tags |