1 7 8 package javax.swing.text; 9 10 import java.io.Serializable ; 11 12 29 public class TabSet implements Serializable 30 { 31 32 private TabStop [] tabs; 33 38 private int hashCode = Integer.MAX_VALUE; 39 40 44 public TabSet(TabStop [] tabs) { 45 if(tabs != null) { 47 int tabCount = tabs.length; 48 49 this.tabs = new TabStop [tabCount]; 50 System.arraycopy(tabs, 0, this.tabs, 0, tabCount); 51 } 52 else 53 this.tabs = null; 54 } 55 56 59 public int getTabCount() { 60 return (tabs == null) ? 0 : tabs.length; 61 } 62 63 68 public TabStop getTab(int index) { 69 int numTabs = getTabCount(); 70 71 if(index < 0 || index >= numTabs) 72 throw new IllegalArgumentException (index + 73 " is outside the range of tabs"); 74 return tabs[index]; 75 } 76 77 81 public TabStop getTabAfter(float location) { 82 int index = getTabIndexAfter(location); 83 84 return (index == -1) ? null : tabs[index]; 85 } 86 87 91 public int getTabIndex(TabStop tab) { 92 for(int counter = getTabCount() - 1; counter >= 0; counter--) 93 if(getTab(counter) == tab) 95 return counter; 96 return -1; 97 } 98 99 103 public int getTabIndexAfter(float location) { 104 int current, min, max; 105 106 min = 0; 107 max = getTabCount(); 108 while(min != max) { 109 current = (max - min) / 2 + min; 110 if(location > tabs[current].getPosition()) { 111 if(min == current) 112 min = max; 113 else 114 min = current; 115 } 116 else { 117 if(current == 0 || location > tabs[current - 1].getPosition()) 118 return current; 119 max = current; 120 } 121 } 122 return -1; 124 } 125 126 136 public boolean equals(Object o) { 137 if (o == this) { 138 return true; 139 } 140 if (o instanceof TabSet ) { 141 TabSet ts = (TabSet ) o; 142 int count = getTabCount(); 143 if (ts.getTabCount() != count) { 144 return false; 145 } 146 for (int i=0; i < count; i++) { 147 TabStop ts1 = getTab(i); 148 TabStop ts2 = ts.getTab(i); 149 if ((ts1 == null && ts2 != null) || 150 (ts1 != null && !getTab(i).equals(ts.getTab(i)))) { 151 return false; 152 } 153 } 154 return true; 155 } 156 return false; 157 } 158 159 165 public int hashCode() { 166 if (hashCode == Integer.MAX_VALUE) { 167 hashCode = 0; 168 int len = getTabCount(); 169 for (int i = 0; i < len; i++) { 170 TabStop ts = getTab(i); 171 hashCode ^= ts != null ? getTab(i).hashCode() : 0; 172 } 173 if (hashCode == Integer.MAX_VALUE) { 174 hashCode -= 1; 175 } 176 } 177 return hashCode; 178 } 179 180 183 public String toString() { 184 int tabCount = getTabCount(); 185 StringBuffer buffer = new StringBuffer ("[ "); 186 187 for(int counter = 0; counter < tabCount; counter++) { 188 if(counter > 0) 189 buffer.append(" - "); 190 buffer.append(getTab(counter).toString()); 191 } 192 buffer.append(" ]"); 193 return buffer.toString(); 194 } 195 } 196 | Popular Tags |