AttributedCharacterIterator.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* AttributedCharacterIterator.java -- Iterate over attributes
  2. Copyright (C) 1998, 1999, 2004, 2006, Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.text;
  32. import java.io.InvalidObjectException;
  33. import java.io.Serializable;
  34. import java.util.Map;
  35. import java.util.Set;
  36. /**
  37. * This interface extends the <code>CharacterIterator</code> interface
  38. * in order to support iteration over character attributes as well as
  39. * over the characters themselves.
  40. * <p>
  41. * In addition to attributes of specific characters, this interface
  42. * supports the concept of the "attribute run", which is an attribute
  43. * that is defined for a particular value across an entire range of
  44. * characters or which is undefined over a range of characters.
  45. *
  46. * @since 1.2
  47. *
  48. * @author Aaron M. Renn (arenn@urbanophile.com)
  49. * @since 1.2
  50. */
  51. public interface AttributedCharacterIterator extends CharacterIterator
  52. {
  53. /**
  54. * Defines attribute keys that are used as text attributes.
  55. */
  56. public static class Attribute implements Serializable
  57. {
  58. private static final long serialVersionUID = -9142742483513960612L;
  59. /**
  60. * This is the attribute for the language of the text. The value of
  61. * attributes of this key type are instances of <code>Locale</code>.
  62. */
  63. public static final Attribute LANGUAGE = new Attribute("language");
  64. /**
  65. * This is the attribute for the reading form of text. This is used
  66. * for storing pronunciation along with the written text for languages
  67. * which need it. The value of attributes of this key type are
  68. * instances of <code>Annotation</code> which wrappers a
  69. * <code>String</code>.
  70. */
  71. public static final Attribute READING = new Attribute("reading");
  72. /**
  73. * This is the attribute for input method segments. The value of attributes
  74. * of this key type are instances of <code>Annotation</code> which wrapper
  75. * a <code>String</code>.
  76. */
  77. public static final Attribute INPUT_METHOD_SEGMENT =
  78. new Attribute("input_method_segment");
  79. /**
  80. * The name of the attribute key
  81. * @serial
  82. */
  83. private String name;
  84. /**
  85. * Initializes a new instance of this class with the specified name.
  86. *
  87. * @param name The name of this attribute key.
  88. */
  89. protected Attribute(String name)
  90. {
  91. this.name = name;
  92. }
  93. /**
  94. * Returns the name of this attribute.
  95. *
  96. * @return The attribute name
  97. */
  98. protected String getName()
  99. {
  100. return name;
  101. }
  102. /**
  103. * Resolves an instance of
  104. * <code>AttributedCharacterIterator.Attribute</code>
  105. * that is being deserialized to one of the three pre-defined attribute
  106. * constants. It does this by comparing the names of the attributes. The
  107. * constant that the deserialized object resolves to is returned.
  108. *
  109. * @return The resolved contant value
  110. *
  111. * @exception InvalidObjectException If the object being deserialized
  112. * cannot be resolved.
  113. */
  114. protected Object readResolve() throws InvalidObjectException
  115. {
  116. if (getName().equals(READING.getName()))
  117. return READING;
  118. if (getName().equals(LANGUAGE.getName()))
  119. return LANGUAGE;
  120. if (getName().equals(INPUT_METHOD_SEGMENT.getName()))
  121. return INPUT_METHOD_SEGMENT;
  122. throw new InvalidObjectException ("Can't resolve Attribute: "
  123. + getName());
  124. }
  125. /**
  126. * Tests this object for equality against the specified object.
  127. * The two objects will be considered equal if and only if:
  128. * <ul>
  129. * <li>The specified object is not <code>null</code>.
  130. * <li>The specified object is an instance of
  131. * <code>AttributedCharacterIterator.Attribute</code>.
  132. * <li>The specified object has the same attribute name as this object.
  133. * </ul>
  134. *
  135. * @param obj the <code>Object</code> to test for equality against this
  136. * object.
  137. *
  138. * @return <code>true</code> if the specified object is equal to this one,
  139. * <code>false</code> otherwise.
  140. */
  141. public final boolean equals(Object obj)
  142. {
  143. if (obj == this)
  144. return true;
  145. else
  146. return false;
  147. }
  148. /**
  149. * Returns a hash value for this object.
  150. *
  151. * @return A hash value for this object.
  152. */
  153. public final int hashCode()
  154. {
  155. return super.hashCode();
  156. }
  157. /**
  158. * Returns a <code>String</code> representation of this object.
  159. *
  160. * @return A <code>String</code> representation of this object.
  161. */
  162. public String toString()
  163. {
  164. return getClass().getName() + "(" + getName() + ")";
  165. }
  166. } // Inner class Attribute
  167. /**
  168. * Returns a list of all keys that are defined for the
  169. * text range. This can be an empty list if no attributes are defined.
  170. *
  171. * @return A list of keys
  172. */
  173. Set<Attribute> getAllAttributeKeys();
  174. /**
  175. * Returns a <code>Map</code> of the attributes defined for the current
  176. * character.
  177. *
  178. * @return A <code>Map</code> of the attributes for the current character.
  179. */
  180. Map<Attribute, Object> getAttributes();
  181. /**
  182. * Returns the value of the specified attribute for the
  183. * current character. If the attribute is not defined for the current
  184. * character, <code>null</code> is returned.
  185. *
  186. * @param attrib The attribute to retrieve the value of.
  187. *
  188. * @return The value of the specified attribute
  189. */
  190. Object getAttribute(AttributedCharacterIterator.Attribute attrib);
  191. /**
  192. * Returns the index of the first character in the run that
  193. * contains all attributes defined for the current character.
  194. *
  195. * @return The start index of the run
  196. */
  197. int getRunStart();
  198. /**
  199. * Returns the index of the first character in the run that
  200. * contains all attributes in the specified <code>Set</code> defined for
  201. * the current character.
  202. *
  203. * @param attribs The <code>Set</code> of attributes.
  204. *
  205. * @return The start index of the run.
  206. */
  207. int getRunStart(Set<? extends Attribute> attribs);
  208. /**
  209. * Returns the index of the first character in the run that
  210. * contains the specified attribute defined for the current character.
  211. *
  212. * @param attrib The attribute.
  213. *
  214. * @return The start index of the run.
  215. */
  216. int getRunStart(AttributedCharacterIterator.Attribute attrib);
  217. /**
  218. * Returns the index of the character after the end of the run
  219. * that contains all attributes defined for the current character.
  220. *
  221. * @return The end index of the run.
  222. */
  223. int getRunLimit();
  224. /**
  225. * Returns the index of the character after the end of the run
  226. * that contains all attributes in the specified <code>Set</code> defined
  227. * for the current character.
  228. *
  229. * @param attribs The <code>Set</code> of attributes.
  230. *
  231. * @return The end index of the run.
  232. */
  233. int getRunLimit(Set<? extends Attribute> attribs);
  234. /**
  235. * Returns the index of the character after the end of the run
  236. * that contains the specified attribute defined for the current character.
  237. *
  238. * @param attrib The attribute.
  239. *
  240. * @return The end index of the run.
  241. */
  242. int getRunLimit(AttributedCharacterIterator.Attribute attrib);
  243. } // interface AttributedCharacterIterator