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