]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/styledtext/CharBufferIterator.java
go
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / styledtext / CharBufferIterator.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.styledtext;\r
14 \r
15 import java.text.CharacterIterator;\r
16 \r
17 final class CharBufferIterator implements CharacterIterator,\r
18                                           Cloneable\r
19 {\r
20     static final String COPYRIGHT =\r
21                 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
22     private int fRangeStart;\r
23     private int fRangeLimit;\r
24     private int fCurrentIndex;\r
25     private char fStorage[];\r
26     private int fGap;\r
27     private int fGapLength;\r
28     private Validation fValidation;\r
29 \r
30     CharBufferIterator(int start,\r
31                        int limit,\r
32                        char[] storage,\r
33                        int length,\r
34                        int gap,\r
35                        Validation validation) {\r
36 \r
37         if (start > limit) {\r
38             throw new IllegalArgumentException("start > limit");\r
39         }\r
40         fRangeStart = start;\r
41         fRangeLimit = limit;\r
42         fCurrentIndex = fRangeStart;\r
43         fStorage = storage;\r
44         fGap = gap;\r
45         fGapLength = (storage==null? 0 : storage.length) - length;\r
46         fValidation = validation;\r
47     }\r
48 \r
49     private void checkValidation() {\r
50 \r
51         if (!fValidation.isValid()) {\r
52             throw new Error("Iterator is no longer valid");\r
53         }\r
54     }\r
55 \r
56     public char first()\r
57     {\r
58         return setIndex(fRangeStart);\r
59     }\r
60 \r
61     public char last()\r
62     {\r
63         return setIndex(fRangeLimit - 1);\r
64     }\r
65 \r
66     public char current()\r
67     {\r
68         checkValidation();\r
69         if (fCurrentIndex < fRangeStart || fCurrentIndex >= fRangeLimit)\r
70             return DONE;\r
71         int i = (fCurrentIndex < fGap) ? fCurrentIndex : (fCurrentIndex + fGapLength);\r
72         return fStorage[i];\r
73     }\r
74 \r
75     public char next()\r
76     {\r
77         checkValidation();\r
78         fCurrentIndex++;\r
79         if (fCurrentIndex >= fRangeLimit)\r
80         {\r
81             fCurrentIndex = fRangeLimit;\r
82             return DONE;\r
83         }\r
84         int i = (fCurrentIndex < fGap) ? fCurrentIndex : (fCurrentIndex + fGapLength);\r
85         return fStorage[i];\r
86     }\r
87 \r
88     public char previous()\r
89     {\r
90         fCurrentIndex--;\r
91         if (fCurrentIndex >= fRangeStart)\r
92             return current();\r
93         fCurrentIndex = fRangeStart;\r
94         return DONE;\r
95     }\r
96 \r
97     public char setIndex(int i)\r
98     {\r
99         if (i < fRangeStart || i > fRangeLimit)\r
100             throw new IllegalArgumentException("Invalid position");\r
101         fCurrentIndex = i;\r
102         return current();\r
103     }\r
104 \r
105     public int getBeginIndex()\r
106     {\r
107         return fRangeStart;\r
108     }\r
109 \r
110     public int getEndIndex()\r
111     {\r
112         return fRangeLimit;\r
113     }\r
114 \r
115     public int getIndex()\r
116     {\r
117         return fCurrentIndex;\r
118     }\r
119 \r
120     public Object clone()\r
121     {\r
122         try {\r
123             return super.clone();\r
124         }\r
125         catch (CloneNotSupportedException e) {\r
126             return null;\r
127         }\r
128     }\r
129 }\r