1. /*
  2. * @(#)AttributedCharacterIterator.java 1.22 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.text;
  8. import java.io.InvalidObjectException;
  9. import java.io.Serializable;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. import java.util.Set;
  13. /**
  14. * An AttributedCharacterIterator allows iteration through both text and
  15. * related attribute information.
  16. *
  17. * <p>
  18. * An attribute is a key/value pair, identified by the key. No two
  19. * attributes on a given character can have the same key.
  20. *
  21. * <p>The values for an attribute are immutable, or must not be mutated
  22. * by clients or storage. They are always passed by reference, and not
  23. * cloned.
  24. *
  25. * <p>A <em>run with respect to an attribute</em> is a maximum text range for
  26. * which:
  27. * <ul>
  28. * <li>the attribute is undefined or null for the entire range, or
  29. * <li>the attribute value is defined and has the same non-null value for the
  30. * entire range.
  31. * </ul>
  32. *
  33. * <p>A <em>run with respect to a set of attributes</em> is a maximum text range for
  34. * which this condition is met for each member attribute.
  35. *
  36. * <p>The returned indexes are limited to the range of the iterator.
  37. *
  38. * <p>The returned attribute information is limited to runs that contain
  39. * the current character.
  40. *
  41. * <p>
  42. * Attribute keys are instances of AttributedCharacterIterator.Attribute and its
  43. * subclasses, such as java.awt.font.TextAttribute.
  44. *
  45. * @see AttributedCharacterIterator.Attribute
  46. * @see java.awt.font.TextAttribute
  47. * @see AttributedString
  48. * @see Annotation
  49. * @since JDK1.2
  50. */
  51. public interface AttributedCharacterIterator extends CharacterIterator {
  52. /**
  53. * Defines attribute keys that are used to identify text attributes. These
  54. * keys are used in AttributedCharacterIterator and AttributedString.
  55. * @see AttributedCharacterIterator
  56. * @see AttributedString
  57. */
  58. public static class Attribute implements Serializable {
  59. /**
  60. * The name of this Attribute. The name is used primarily by readResolve
  61. * to look up the corresponding predefined instance when deserializing
  62. * an instance.
  63. * @serial
  64. */
  65. private String name;
  66. // table of all instances in this class, used by readResolve
  67. private static final Map instanceMap = new HashMap(7);
  68. /**
  69. * Constructs an Attribute with the given name.
  70. */
  71. protected Attribute(String name) {
  72. this.name = name;
  73. if (this.getClass() == Attribute.class) {
  74. instanceMap.put(name, this);
  75. }
  76. }
  77. /**
  78. * Compares two objects for equality. This version only returns true
  79. * for <code>x.equals(y)</code> if <code>x</code> and <code>y</code> refer
  80. * to the same object, and guarantees this for all subclasses.
  81. */
  82. public final boolean equals(Object obj) {
  83. return super.equals(obj);
  84. }
  85. /**
  86. * Returns a hash code value for the object. This version is identical to
  87. * the one in Object, but is also final.
  88. */
  89. public final int hashCode() {
  90. return super.hashCode();
  91. }
  92. /**
  93. * Returns a string representation of the object. This version returns the
  94. * concatenation of class name, "(", a name identifying the attribute and ")".
  95. */
  96. public String toString() {
  97. return getClass().getName() + "(" + name + ")";
  98. }
  99. /**
  100. * Returns the name of the attribute.
  101. */
  102. protected String getName() {
  103. return name;
  104. }
  105. /**
  106. * Resolves instances being deserialized to the predefined constants.
  107. */
  108. protected Object readResolve() throws InvalidObjectException {
  109. if (this.getClass() != Attribute.class) {
  110. throw new InvalidObjectException("subclass didn't correctly implement readResolve");
  111. }
  112. Attribute instance = (Attribute) instanceMap.get(getName());
  113. if (instance != null) {
  114. return instance;
  115. } else {
  116. throw new InvalidObjectException("unknown attribute name");
  117. };
  118. }
  119. /**
  120. * Attribute key for the language of some text.
  121. * <p> Values are instances of Locale.
  122. * @see java.util.Locale
  123. */
  124. public static final Attribute LANGUAGE = new Attribute("language");
  125. /**
  126. * Attribute key for the reading of some text. In languages where the written form
  127. * and the pronunciation of a word are only loosely related (such as Japanese),
  128. * it is often necessary to store the reading (pronunciation) along with the
  129. * written form.
  130. * <p>Values are instances of Annotation holding instances of String.
  131. * @see Annotation
  132. * @see java.lang.String
  133. */
  134. public static final Attribute READING = new Attribute("reading");
  135. /**
  136. * Attribute key for input method segments. Input methods often break
  137. * up text into segments, which usually correspond to words.
  138. * <p>Values are instances of Annotation holding a null reference.
  139. * @see Annotation
  140. */
  141. public static final Attribute INPUT_METHOD_SEGMENT = new Attribute("input_method_segment");
  142. };
  143. /**
  144. * Returns the index of the first character of the run
  145. * with respect to all attributes containing the current character.
  146. */
  147. public int getRunStart();
  148. /**
  149. * Returns the index of the first character of the run
  150. * with respect to the given attribute containing the current character.
  151. */
  152. public int getRunStart(Attribute attribute);
  153. /**
  154. * Returns the index of the first character of the run
  155. * with respect to the given attributes containing the current character.
  156. */
  157. public int getRunStart(Set attributes);
  158. /**
  159. * Returns the index of the first character following the run
  160. * with respect to all attributes containing the current character.
  161. */
  162. public int getRunLimit();
  163. /**
  164. * Returns the index of the first character following the run
  165. * with respect to the given attribute containing the current character.
  166. */
  167. public int getRunLimit(Attribute attribute);
  168. /**
  169. * Returns the index of the first character following the run
  170. * with respect to the given attributes containing the current character.
  171. */
  172. public int getRunLimit(Set attributes);
  173. /**
  174. * Returns a map with the attributes defined on the current
  175. * character.
  176. */
  177. public Map getAttributes();
  178. /**
  179. * Returns the value of the named attribute for the current character.
  180. * Returns null if the attribute is not defined.
  181. * @param attribute the key of the attribute whose value is requested.
  182. */
  183. public Object getAttribute(Attribute attribute);
  184. /**
  185. * Returns the keys of all attributes defined on the
  186. * iterator's text range. The set is empty if no
  187. * attributes are defined.
  188. */
  189. public Set getAllAttributeKeys();
  190. };