1 7 8 20 21 package java.text; 22 23 56 public class FieldPosition { 57 58 62 int field = 0; 63 64 68 int endIndex = 0; 69 70 74 int beginIndex = 0; 75 76 79 private Format.Field attribute; 80 81 91 public FieldPosition(int field) { 92 this.field = field; 93 } 94 95 104 public FieldPosition(Format.Field attribute) { 105 this(attribute, -1); 106 } 107 108 124 public FieldPosition(Format.Field attribute, int fieldID) { 125 this.attribute = attribute; 126 this.field = fieldID; 127 } 128 129 137 public Format.Field getFieldAttribute() { 138 return attribute; 139 } 140 141 144 public int getField() { 145 return field; 146 } 147 148 151 public int getBeginIndex() { 152 return beginIndex; 153 } 154 155 159 public int getEndIndex() { 160 return endIndex; 161 } 162 163 167 public void setBeginIndex(int bi) { 168 beginIndex = bi; 169 } 170 171 175 public void setEndIndex(int ei) { 176 endIndex = ei; 177 } 178 179 185 Format.FieldDelegate getFieldDelegate() { 186 return new Delegate(); 187 } 188 189 192 public boolean equals(Object obj) 193 { 194 if (obj == null) return false; 195 if (!(obj instanceof FieldPosition )) 196 return false; 197 FieldPosition other = (FieldPosition ) obj; 198 if (attribute == null) { 199 if (other.attribute != null) { 200 return false; 201 } 202 } 203 else if (!attribute.equals(other.attribute)) { 204 return false; 205 } 206 return (beginIndex == other.beginIndex 207 && endIndex == other.endIndex 208 && field == other.field); 209 } 210 211 215 public int hashCode() { 216 return (field << 24) | (beginIndex << 16) | endIndex; 217 } 218 219 223 public String toString() { 224 return getClass().getName() + 225 "[field=" + field + ",attribute=" + attribute + 226 ",beginIndex=" + beginIndex + 227 ",endIndex=" + endIndex + ']'; 228 } 229 230 231 235 private boolean matchesField(Format.Field attribute) { 236 if (this.attribute != null) { 237 return this.attribute.equals(attribute); 238 } 239 return false; 240 } 241 242 247 private boolean matchesField(Format.Field attribute, int field) { 248 if (this.attribute != null) { 249 return this.attribute.equals(attribute); 250 } 251 return (field == this.field); 252 } 253 254 255 260 private class Delegate implements Format.FieldDelegate { 261 266 private boolean encounteredField; 267 268 public void formatted(Format.Field attr, Object value, int start, 269 int end, StringBuffer buffer) { 270 if (!encounteredField && matchesField(attr)) { 271 setBeginIndex(start); 272 setEndIndex(end); 273 encounteredField = (start != end); 274 } 275 } 276 277 public void formatted(int fieldID, Format.Field attr, Object value, 278 int start, int end, StringBuffer buffer) { 279 if (!encounteredField && matchesField(attr, fieldID)) { 280 setBeginIndex(start); 281 setEndIndex(end); 282 encounteredField = (start != end); 283 } 284 } 285 } 286 } 287 | Popular Tags |