1. /*
  2. * @(#)AttributedCharacterIterator.java 1.30 03/01/23
  3. *
  4. * Copyright 2003 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 1.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. * @since 1.2
  58. */
  59. public static class Attribute implements Serializable {
  60. /**
  61. * The name of this Attribute. The name is used primarily by readResolve
  62. * to look up the corresponding predefined instance when deserializing
  63. * an instance.
  64. * @serial
  65. */
  66. private String name;
  67. // table of all instances in this class, used by readResolve
  68. private static final Map instanceMap = new HashMap(7);
  69. /**
  70. * Constructs an Attribute with the given name.
  71. */
  72. protected Attribute(String name) {
  73. this.name = name;
  74. if (this.getClass() == Attribute.class) {
  75. instanceMap.put(name, this);
  76. }
  77. }
  78. /**
  79. * Compares two objects for equality. This version only returns true
  80. * for <code>x.equals(y)</code> if <code>x</code> and <code>y</code> refer
  81. * to the same object, and guarantees this for all subclasses.
  82. */
  83. public final boolean equals(Object obj) {
  84. return super.equals(obj);
  85. }
  86. /**
  87. * Returns a hash code value for the object. This version is identical to
  88. * the one in Object, but is also final.
  89. */
  90. public final int hashCode() {
  91. return super.hashCode();
  92. }
  93. /**
  94. * Returns a string representation of the object. This version returns the
  95. * concatenation of class name, "(", a name identifying the attribute and ")".
  96. */
  97. public String toString() {
  98. return getClass().getName() + "(" + name + ")";
  99. }
  100. /**
  101. * Returns the name of the attribute.
  102. */
  103. protected String getName() {
  104. return name;
  105. }
  106. /**
  107. * Resolves instances being deserialized to the predefined constants.
  108. */
  109. protected Object readResolve() throws InvalidObjectException {
  110. if (this.getClass() != Attribute.class) {
  111. throw new InvalidObjectException("subclass didn't correctly implement readResolve");
  112. }
  113. Attribute instance = (Attribute) instanceMap.get(getName());
  114. if (instance != null) {
  115. return instance;
  116. } else {
  117. throw new InvalidObjectException("unknown attribute name");
  118. }
  119. }
  120. /**
  121. * Attribute key for the language of some text.
  122. * <p> Values are instances of Locale.
  123. * @see java.util.Locale
  124. */
  125. public static final Attribute LANGUAGE = new Attribute("language");
  126. /**
  127. * Attribute key for the reading of some text. In languages where the written form
  128. * and the pronunciation of a word are only loosely related (such as Japanese),
  129. * it is often necessary to store the reading (pronunciation) along with the
  130. * written form.
  131. * <p>Values are instances of Annotation holding instances of String.
  132. * @see Annotation
  133. * @see java.lang.String
  134. */
  135. public static final Attribute READING = new Attribute("reading");
  136. /**
  137. * Attribute key for input method segments. Input methods often break
  138. * up text into segments, which usually correspond to words.
  139. * <p>Values are instances of Annotation holding a null reference.
  140. * @see Annotation
  141. */
  142. public static final Attribute INPUT_METHOD_SEGMENT = new Attribute("input_method_segment");
  143. // make sure the serial version doesn't change between compiler versions
  144. private static final long serialVersionUID = -9142742483513960612L;
  145. };
  146. /**
  147. * Returns the index of the first character of the run
  148. * with respect to all attributes containing the current character.
  149. */
  150. public int getRunStart();
  151. /**
  152. * Returns the index of the first character of the run
  153. * with respect to the given attribute containing the current character.
  154. */
  155. public int getRunStart(Attribute attribute);
  156. /**
  157. * Returns the index of the first character of the run
  158. * with respect to the given attributes containing the current character.
  159. */
  160. public int getRunStart(Set attributes);
  161. /**
  162. * Returns the index of the first character following the run
  163. * with respect to all attributes containing the current character.
  164. */
  165. public int getRunLimit();
  166. /**
  167. * Returns the index of the first character following the run
  168. * with respect to the given attribute containing the current character.
  169. */
  170. public int getRunLimit(Attribute attribute);
  171. /**
  172. * Returns the index of the first character following the run
  173. * with respect to the given attributes containing the current character.
  174. */
  175. public int getRunLimit(Set attributes);
  176. /**
  177. * Returns a map with the attributes defined on the current
  178. * character.
  179. */
  180. public Map getAttributes();
  181. /**
  182. * Returns the value of the named attribute for the current character.
  183. * Returns null if the attribute is not defined.
  184. * @param attribute the key of the attribute whose value is requested.
  185. */
  186. public Object getAttribute(Attribute attribute);
  187. /**
  188. * Returns the keys of all attributes defined on the
  189. * iterator's text range. The set is empty if no
  190. * attributes are defined.
  191. */
  192. public Set getAllAttributeKeys();
  193. };