]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/textformat/TextOffset.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / textformat / TextOffset.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 // Revision: 03 1.16 richtext/TextOffset.java, richtext, richtext\r
14 \r
15 /*\r
16     9/5/96 {jbr} added set and equals methods\r
17 */\r
18 \r
19 package com.ibm.richtext.textformat;\r
20 \r
21 /**\r
22  * A TextOffset indicates both an integer offset into text and a placement\r
23  * on one of the characters adjacent to the offset.  An offset is a\r
24  * position between two characters;  offset n\r
25  * is between character n-1 and character n.  The placement specifies whether\r
26  * it is associated with the character\r
27  * after the offset\r
28  * (character n) or the character before the offset (character n-1).\r
29  * <p>\r
30  * Knowing which character the TextOffset is associated with is necessary\r
31  * when displaying carets.  In bidirectional text, a single offset may\r
32  * have two distinct carets.  Also, in multiline text, an offset at a line\r
33  * break has a possible caret on each line.\r
34  * <p>\r
35  * Most clients will not be interested in the placement, and will just use\r
36  * the offset.\r
37  */\r
38 public final class TextOffset\r
39 {\r
40     static final String COPYRIGHT =\r
41                 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
42     /**\r
43      * Indicates that the TextOffset is associated with character\r
44      * <code>fOffset - 1</code> - ie the character before its offset.\r
45      */\r
46     public final static boolean BEFORE_OFFSET = true;\r
47 \r
48     /**\r
49      * Indicates that the TextOffset is associated with character\r
50      * <code>fOffset</code> - ie the character after its offset.\r
51      */\r
52     public final static boolean AFTER_OFFSET = false;\r
53 \r
54     /**\r
55      * The offset into the text.\r
56      */\r
57     public int fOffset = 0;\r
58 \r
59     /**\r
60      * The placement - before or after.\r
61      */\r
62     public boolean fPlacement = AFTER_OFFSET;\r
63 \r
64     /**\r
65     * Constructs a new TextOffset\r
66     * @param offset the offset into the text to represent.  Placement is implicitly AFTER_OFFSET.\r
67     */\r
68     public TextOffset(int offset)\r
69     {\r
70         if (offset < 0)\r
71             throw new IllegalArgumentException("Offset is negative in TextOffset constructor.");\r
72 \r
73         fOffset = offset;\r
74 \r
75         fPlacement = AFTER_OFFSET;\r
76     }\r
77 \r
78     /**\r
79     * Constructs a new TextOffset at 0, with placement AFTER_OFFSET.\r
80     */\r
81     public TextOffset() {\r
82         this(0);\r
83     }\r
84 \r
85     /**\r
86     * Constructs a new TextOffset with the given offset and placement.\r
87     * @param offset the offset into the text\r
88     * @param placement indicates the position of the caret; one of BEFORE_OFFSET or AFTER_OFFSET\r
89     */\r
90     public TextOffset(int offset, boolean placement)\r
91     {\r
92         if (offset < 0)\r
93             throw new IllegalArgumentException("TextOffset constructor offset < 0: " + offset);\r
94 \r
95         fOffset = offset;\r
96         fPlacement = placement;\r
97     }\r
98 \r
99     /**\r
100     * Constructs a new TextOffset from an existing one.\r
101     * @param rhs the TextOffset to copy\r
102     */\r
103     public TextOffset(TextOffset rhs) {\r
104 \r
105         this(rhs.fOffset, rhs.fPlacement);\r
106     }\r
107 \r
108     /**\r
109     * Set the value of the TextOffset\r
110     * @param offset the offset into the text\r
111     * @param placement indicates the position of the caret; one of BEFORE_OFFSET or AFTER_OFFSET\r
112     */\r
113     public void setOffset(int offset, boolean placement)\r
114     {\r
115         if (offset < 0)\r
116             throw new IllegalArgumentException("TextOffset setOffset offset < 0: " + offset);\r
117 \r
118         fOffset = offset;\r
119         fPlacement = placement;\r
120     }\r
121 \r
122     /**\r
123      * Compare this to another Object.\r
124      */\r
125     public boolean equals(Object other) {\r
126 \r
127         try {\r
128             return equals((TextOffset)other);\r
129         }\r
130         catch(ClassCastException e) {\r
131             return false;\r
132         }\r
133     }\r
134 \r
135     /**\r
136     * Return true if offset and placement are the same.\r
137     *\r
138     * @param other offset to compare against\r
139     * @return true if both offsets are equal\r
140     */\r
141     public boolean equals(TextOffset other) {\r
142 \r
143         return fOffset == other.fOffset && fPlacement == other.fPlacement;\r
144     }\r
145 \r
146     /**\r
147      * Return the hashCode for this object.\r
148      */\r
149     public int hashCode() {\r
150 \r
151         return fPlacement==AFTER_OFFSET? fOffset : -fOffset;\r
152     }\r
153 \r
154     /**\r
155     * Return true if this offset is 'greaterThan' other.  If the fOffset fields are equal, the\r
156     * placement field is considered, and AFTER_OFFSET is considered 'greaterThan' BEFORE_OFFSET.\r
157     *\r
158     * @param other the other offset\r
159     * @return true if this offset appears after other\r
160     */\r
161     public boolean greaterThan(TextOffset other)\r
162     {\r
163         return fOffset > other.fOffset ||\r
164             (fOffset == other.fOffset && fPlacement == AFTER_OFFSET && other.fPlacement == BEFORE_OFFSET);\r
165     }\r
166 \r
167     /**\r
168     * Return true if this offset is 'lessThan' other.  If the fOffset fields are equal, the\r
169     * placement field is considered, and BEFORE_OFFSET is considered 'lessThan' AFTER_OFFSET.\r
170     *\r
171     * @param other the other offset\r
172     * @return true if this offset appears before other\r
173     */\r
174     public boolean lessThan(TextOffset other) {\r
175 \r
176         return fOffset < other.fOffset ||\r
177             (fOffset == other.fOffset && fPlacement == BEFORE_OFFSET && other.fPlacement == AFTER_OFFSET);\r
178     }\r
179 \r
180     /**\r
181     * Copy the value of another TextOffset into this\r
182     * @param other the TextOffset to copy\r
183     */\r
184     public void assign(TextOffset other) {\r
185         fOffset = other.fOffset;\r
186         fPlacement = other.fPlacement;\r
187     }\r
188 \r
189     /**\r
190     * Return a string representation of this object.\r
191     */\r
192     public String toString() {\r
193 \r
194         return "[" + (fPlacement ? "before " : "after ") + fOffset + "]";\r
195     }\r
196 }\r