]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/textlayout/attributes/AttributeKey.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / textlayout / attributes / AttributeKey.java
1 /*\r
2  * (C) Copyright IBM Corp. 1998-2004.  All Rights Reserved.\r
3  *\r
4  * The program is provided "as is" without any warranty express or\r
5  * implied, including the warranty of non-infringement and the implied\r
6  * warranties of merchantibility and fitness for a particular purpose.\r
7  * IBM will not be liable for any damages suffered by you as a result\r
8  * of using the Program. In no event will IBM be liable for any\r
9  * special, indirect or consequential damages or lost profits even if\r
10  * IBM has been advised of the possibility of their occurrence. IBM\r
11  * will not be liable for any third party claims against you.\r
12  */\r
13 package com.ibm.richtext.textlayout.attributes;\r
14 \r
15 import java.io.Serializable;\r
16 \r
17 /**\r
18  * This class provides a cannonical mapping between fields in TextAttribute\r
19  * and instances of itself.  It is used by AttributeMap to serialize\r
20  * and deserialize TextAttribute to preserve uniqueness of TextAttribute\r
21  * instances (ie so that TextAttribute instances remain singletons),\r
22  * and to provide compatability between 1.1 and 1.2 versions of\r
23  * TextAttribute.\r
24  * <p>\r
25  * Example use - instead of doing this:\r
26  * <blockquote><pre>\r
27  *     out.writeObject(anAttribute);\r
28  * </pre></blockquote>\r
29  * do this:\r
30  * <blockquote><pre>\r
31  *     out.writeObject(AttributeKey.mapAttributeToKey(anAttribute));\r
32  * </pre></blockquote>\r
33  * Similarly, instead of this:\r
34  * <blockquote><pre>\r
35  *     anAttribute = in.readObject();\r
36  * </pre></blockquote>\r
37  * do this:\r
38  * <blockquote><pre>\r
39  *     anAttribute = AttributeKey.mapKeyToAttribute(in.readObject());\r
40  * </pre></blockquote>\r
41  * <p>\r
42  * If anAttribute is not a known TextAttribute, then <code>mapAttributeToKey</code>\r
43  * will just return its argument.  Similarly, <code>mapKeyToAttribute</code> will\r
44  * return its argument if the argument is not a known AttributeKey.\r
45  */\r
46 \r
47 /*public*/ final class AttributeKey implements Serializable {\r
48 \r
49 /*\r
50     In this implementation, two parallel Vectors are\r
51     maintained.  TextAttribute(i) maps to AttributeKey(i).\r
52     For compatability with existing data, this mapping must\r
53     be maintained in the future!  So, when new attributes\r
54     are added, add them to the end of the list.\r
55 */\r
56     static final String COPYRIGHT =\r
57                 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
58     private static final long serialVersionUID = 3772371253277107294L;\r
59 \r
60     private static Object[] fgTextAttributes;\r
61     private static Object[] fgAttributeKeys;\r
62 \r
63     static {\r
64         fgTextAttributes = new Object[] {\r
65             TextAttribute.FONT,\r
66             TextAttribute.FAMILY,\r
67             TextAttribute.WEIGHT,\r
68             TextAttribute.POSTURE,\r
69             TextAttribute.SIZE,\r
70             TextAttribute.SUPERSCRIPT,\r
71             TextAttribute.FOREGROUND,\r
72             TextAttribute.BACKGROUND,\r
73             TextAttribute.UNDERLINE,\r
74             TextAttribute.STRIKETHROUGH,\r
75             TextAttribute.CHAR_REPLACEMENT,\r
76             TextAttribute.EXTRA_LINE_SPACING,\r
77             TextAttribute.FIRST_LINE_INDENT,\r
78             TextAttribute.MIN_LINE_SPACING,\r
79             TextAttribute.LINE_FLUSH,\r
80             TextAttribute.LEADING_MARGIN,\r
81             TextAttribute.TRAILING_MARGIN,\r
82             TextAttribute.TAB_RULER,\r
83             TextAttribute.RUN_DIRECTION,\r
84             TextAttribute.BIDI_EMBEDDING,\r
85             TextAttribute.JUSTIFICATION,\r
86         };\r
87 \r
88         final int attrCount = fgTextAttributes.length;\r
89         fgAttributeKeys = new Object[attrCount];\r
90 \r
91         for (int i=0; i < attrCount; i += 1) {\r
92             fgAttributeKeys[i] = new AttributeKey(i);\r
93         }\r
94     }\r
95 \r
96     /**\r
97      * Return the TextAttribute corresponding to the given key.\r
98      * If key is an instance of AttributeKey it will be mapped to\r
99      * a TextAttribute.  Otherwise, the key is returned.\r
100      * @param key the key to map to a TextAttribute field\r
101      * @return the TextAttribute for <code>key</code> if <code>key</code>\r
102      *    is an AttributeKey; otherwise <code>key</code> is returned\r
103      */\r
104     /*public*/ static Object mapKeyToAttribute(Object key) {\r
105 \r
106         try {\r
107             AttributeKey aKey = (AttributeKey) key;\r
108             if (aKey.fId < fgTextAttributes.length) {\r
109                 return fgTextAttributes[aKey.fId];\r
110             }\r
111             else {\r
112                 return key;\r
113             }\r
114         }\r
115         catch(ClassCastException e) {\r
116             return key;\r
117         }\r
118     }\r
119 \r
120     /**\r
121      * If attribute is a known TextAttribute, return an AttributeKey\r
122      * for it.  Otherwise the object is returned.\r
123      * @param attribute the attribute to map to an AttributeKey\r
124      * @return an AttributeKey for <code>attribute</code>\r
125      *     if <code>attribute</code> is a known attribute; otherwise\r
126      *     <code>attribute</code> is returned\r
127      */\r
128     /*public*/ static Object mapAttributeToKey(Object attribute) {\r
129 \r
130         final int attrCount = fgTextAttributes.length;\r
131         \r
132         for (int index = 0; index < attrCount; index += 1) {\r
133             if (fgTextAttributes[index].equals(attribute)) {\r
134                 return fgAttributeKeys[index];\r
135             }\r
136         }\r
137         \r
138         return attribute;\r
139     }\r
140 \r
141 \r
142     private int fId;\r
143 \r
144     private AttributeKey(int id) {\r
145 \r
146         fId = id;\r
147     }\r
148 \r
149     public boolean equals(Object rhs) {\r
150 \r
151         try {\r
152             return ((AttributeKey)rhs).fId == fId;\r
153         }\r
154         catch(ClassCastException e) {\r
155             return false;\r
156         }\r
157     }\r
158 \r
159     public int hashCode() {\r
160 \r
161         return fId;\r
162     }\r
163 }