1 16 17 package swingwtx.swing.text; 18 19 import java.text.CharacterIterator ; 20 21 public class Segment implements Cloneable , CharacterIterator { 22 23 public char[] array; 24 public int offset; 25 public int count; 26 27 private boolean partialReturn; 28 29 public Segment() { 30 this(null, 0, 0); 31 } 32 public Segment(char[] array, int offset, int count) { 33 this.array = array; 34 this.offset = offset; 35 this.count = count; 36 partialReturn = false; 37 } 38 public void setPartialReturn(boolean p) { 39 partialReturn = p; 40 } 41 public boolean isPartialReturn() { 42 return partialReturn; 43 } 44 public String toString() { 45 if (array != null) { 46 return new String (array, offset, count); 47 } 48 return new String (); 49 } 50 51 public char first() { 52 pos = offset; 53 if (count != 0) { 54 return array[pos]; 55 } 56 return DONE; 57 } 58 public char last() { 59 pos = offset + count; 60 if (count != 0) { 61 pos -= 1; 62 return array[pos]; 63 } 64 return DONE; 65 } 66 public char current() { 67 if (count != 0 && pos < offset + count) { 68 return array[pos]; 69 } 70 return DONE; 71 } 72 public char next() { 73 pos += 1; 74 int end = offset + count; 75 if (pos >= end) { 76 pos = end; 77 return DONE; 78 } 79 return current(); 80 } 81 public char previous() { 82 if (pos == offset) { 83 return DONE; 84 } 85 pos -= 1; 86 return current(); 87 } 88 public char setIndex(int position) { 89 int end = offset + count; 90 if ((position < offset) || (position > end)) { 91 throw new IllegalArgumentException ("bad position: " + position); 92 } 93 pos = position; 94 if ((pos != end) && (count != 0)) { 95 return array[pos]; 96 } 97 return DONE; 98 } 99 public int getBeginIndex() { 100 return offset; 101 } 102 public int getEndIndex() { 103 return offset + count; 104 } 105 public int getIndex() { 106 return pos; 107 } 108 public Object clone() { 109 Object o; 110 try { 111 o = super.clone(); 112 } catch (CloneNotSupportedException cnse) { 113 o = null; 114 } 115 return o; 116 } 117 118 private int pos; 119 } | Popular Tags |