📄 attributedstring.java
字号:
updateRunInfo(); if (attributes != null) { relevantAttributes = (Attribute[]) attributes.clone(); } } // Object methods. See documentation in that class. public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof AttributedStringIterator)) { return false; } AttributedStringIterator that = (AttributedStringIterator) obj; if (AttributedString.this != that.getString()) return false; if (currentIndex != that.currentIndex || beginIndex != that.beginIndex || endIndex != that.endIndex) return false; return true; } public int hashCode() { return text.hashCode() ^ currentIndex ^ beginIndex ^ endIndex; } public Object clone() { try { AttributedStringIterator other = (AttributedStringIterator) super.clone(); return other; } catch (CloneNotSupportedException e) { throw new InternalError(); } } // CharacterIterator methods. See documentation in that interface. public char first() { return internalSetIndex(beginIndex); } public char last() { if (endIndex == beginIndex) { return internalSetIndex(endIndex); } else { return internalSetIndex(endIndex - 1); } } public char current() { if (currentIndex == endIndex) { return DONE; } else { return charAt(currentIndex); } } public char next() { if (currentIndex < endIndex) { return internalSetIndex(currentIndex + 1); } else { return DONE; } } public char previous() { if (currentIndex > beginIndex) { return internalSetIndex(currentIndex - 1); } else { return DONE; } } public char setIndex(int position) { if (position < beginIndex || position > endIndex) throw new IllegalArgumentException("Invalid index"); return internalSetIndex(position); } public int getBeginIndex() { return beginIndex; } public int getEndIndex() { return endIndex; } public int getIndex() { return currentIndex; } // AttributedCharacterIterator methods. See documentation in that interface. public int getRunStart() { return currentRunStart; } public int getRunStart(Attribute attribute) { if (currentRunStart == beginIndex || currentRunIndex == -1) { return currentRunStart; } else { Object value = getAttribute(attribute); int runStart = currentRunStart; int runIndex = currentRunIndex; while (runStart > beginIndex && valuesMatch(value, AttributedString.this.getAttribute(attribute, runIndex - 1))) { runIndex--; runStart = runStarts[runIndex]; } if (runStart < beginIndex) { runStart = beginIndex; } return runStart; } } public int getRunStart(Set attributes) { if (currentRunStart == beginIndex || currentRunIndex == -1) { return currentRunStart; } else { int runStart = currentRunStart; int runIndex = currentRunIndex; while (runStart > beginIndex && AttributedString.this.attributeValuesMatch(attributes, currentRunIndex, runIndex - 1)) { runIndex--; runStart = runStarts[runIndex]; } if (runStart < beginIndex) { runStart = beginIndex; } return runStart; } } public int getRunLimit() { return currentRunLimit; } public int getRunLimit(Attribute attribute) { if (currentRunLimit == endIndex || currentRunIndex == -1) { return currentRunLimit; } else { Object value = getAttribute(attribute); int runLimit = currentRunLimit; int runIndex = currentRunIndex; while (runLimit < endIndex && valuesMatch(value, AttributedString.this.getAttribute(attribute, runIndex + 1))) { runIndex++; runLimit = runIndex < runCount - 1 ? runStarts[runIndex + 1] : endIndex; } if (runLimit > endIndex) { runLimit = endIndex; } return runLimit; } } public int getRunLimit(Set attributes) { if (currentRunLimit == endIndex || currentRunIndex == -1) { return currentRunLimit; } else { int runLimit = currentRunLimit; int runIndex = currentRunIndex; while (runLimit < endIndex && AttributedString.this.attributeValuesMatch(attributes, currentRunIndex, runIndex + 1)) { runIndex++; runLimit = runIndex < runCount - 1 ? runStarts[runIndex + 1] : endIndex; } if (runLimit > endIndex) { runLimit = endIndex; } return runLimit; } } public Map getAttributes() { if (runAttributes == null || currentRunIndex == -1 || runAttributes[currentRunIndex] == null) { // ??? would be nice to return null, but current spec doesn't allow it // returning Hashtable saves AttributeMap from dealing with emptiness return new Hashtable(); } return new AttributeMap(currentRunIndex, beginIndex, endIndex); } public Set getAllAttributeKeys() { // ??? This should screen out attribute keys that aren't relevant to the client if (runAttributes == null) { // ??? would be nice to return null, but current spec doesn't allow it // returning HashSet saves us from dealing with emptiness return new HashSet(); } synchronized (AttributedString.this) { // ??? should try to create this only once, then update if necessary, // and give callers read-only view Set keys = new HashSet(); int i = 0; while (i < runCount) { if (runStarts[i] < endIndex && (i == runCount - 1 || runStarts[i + 1] > beginIndex)) { Vector currentRunAttributes = runAttributes[i]; if (currentRunAttributes != null) { int j = currentRunAttributes.size(); while (j-- > 0) { keys.add(currentRunAttributes.get(j)); } } } i++; } return keys; } } public Object getAttribute(Attribute attribute) { int runIndex = currentRunIndex; if (runIndex < 0) { return null; } return AttributedString.this.getAttributeCheckRange(attribute, runIndex, beginIndex, endIndex); } // internally used methods private AttributedString getString() { return AttributedString.this; } // set the current index, update information about the current run if necessary, // return the character at the current index private char internalSetIndex(int position) { currentIndex = position; if (position < currentRunStart || position >= currentRunLimit) { updateRunInfo(); } if (currentIndex == endIndex) { return DONE; } else { return charAt(position); } } // update the information about the current run private void updateRunInfo() { if (currentIndex == endIndex) { currentRunStart = currentRunLimit = endIndex; currentRunIndex = -1; } else { synchronized (AttributedString.this) { int runIndex = -1; while (runIndex < runCount - 1 && runStarts[runIndex + 1] <= currentIndex) runIndex++; currentRunIndex = runIndex; if (runIndex >= 0) { currentRunStart = runStarts[runIndex]; if (currentRunStart < beginIndex) currentRunStart = beginIndex; } else { currentRunStart = beginIndex; } if (runIndex < runCount - 1) { currentRunLimit = runStarts[runIndex + 1]; if (currentRunLimit > endIndex) currentRunLimit = endIndex; } else { currentRunLimit = endIndex; } } } } } // the map class associated with this string class, giving access to the attributes of one run final private class AttributeMap extends AbstractMap { int runIndex; int beginIndex; int endIndex; AttributeMap(int runIndex, int beginIndex, int endIndex) { this.runIndex = runIndex; this.beginIndex = beginIndex; this.endIndex = endIndex; } public Set entrySet() { HashSet set = new HashSet(); synchronized (AttributedString.this) { int size = runAttributes[runIndex].size(); for (int i = 0; i < size; i++) { Attribute key = (Attribute) runAttributes[runIndex].get(i); Object value = runAttributeValues[runIndex].get(i); if (value instanceof Annotation) { value = AttributedString.this.getAttributeCheckRange(key, runIndex, beginIndex, endIndex); if (value == null) { continue; } } Map.Entry entry = new AttributeEntry(key, value); set.add(entry); } } return set; } public Object get(Object key) { return AttributedString.this.getAttributeCheckRange((Attribute) key, runIndex, beginIndex, endIndex); } }}class AttributeEntry implements Map.Entry { private Attribute key; private Object value; AttributeEntry(Attribute key, Object value) { this.key = key; this.value = value; } public boolean equals(Object o) { if (!(o instanceof AttributeEntry)) { return false; } AttributeEntry other = (AttributeEntry) o; return other.key.equals(key) && (value == null ? other.value == null : other.value.equals(value)); } public Object getKey() { return key; } public Object getValue() { return value; } public Object setValue(Object newValue) { throw new UnsupportedOperationException(); } public int hashCode() { return key.hashCode() ^ (value==null ? 0 : value.hashCode()); } public String toString() { return key.toString()+"="+value.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -