1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sourceforge.jeuclid.elements.support.text;
20
21 import java.text.AttributedCharacterIterator;
22 import java.text.CharacterIterator;
23 import java.util.ArrayList;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29
30
31
32
33
34 public class MultiAttributedCharacterIterator implements
35 AttributedCharacterIterator {
36
37 private final List<AttributedCharacterIterator> realIterators = new ArrayList<AttributedCharacterIterator>();
38
39 private int currentList;
40
41
42
43
44 public MultiAttributedCharacterIterator() {
45
46 }
47
48
49 @Override
50 public Object clone() {
51 throw new UnsupportedOperationException();
52 }
53
54
55
56
57
58
59
60
61
62 public void appendAttributedCharacterIterator(
63 final AttributedCharacterIterator aci) {
64 this.realIterators.add(aci);
65 this.first();
66 }
67
68 private int sumUpToNotIncluding(final int limit) {
69 int offset = 0;
70 for (int i = 0; i < limit; i++) {
71 final AttributedCharacterIterator ci = this.realIterators.get(i);
72 offset += ci.getEndIndex() - ci.getBeginIndex();
73 }
74 return offset;
75 }
76
77 private int currentOffset() {
78 int offset = this.sumUpToNotIncluding(this.currentList);
79 offset -= this.realIterators.get(this.currentList).getBeginIndex();
80 return offset;
81 }
82
83
84 public Set<Attribute> getAllAttributeKeys() {
85 final Set<Attribute> retVal = new HashSet<Attribute>();
86 for (final AttributedCharacterIterator ri : this.realIterators) {
87 retVal.addAll(ri.getAllAttributeKeys());
88 }
89 return retVal;
90 }
91
92
93 public Object getAttribute(final Attribute attribute) {
94 return this.realIterators.get(this.currentList).getAttribute(attribute);
95 }
96
97
98 public Map<Attribute, Object> getAttributes() {
99 return this.realIterators.get(this.currentList).getAttributes();
100 }
101
102
103 public int getRunLimit() {
104 return this.realIterators.get(this.currentList).getRunLimit()
105 + this.currentOffset();
106 }
107
108
109 public int getRunLimit(final Attribute attribute) {
110 return this.realIterators.get(this.currentList).getRunLimit(attribute)
111 + this.currentOffset();
112 }
113
114
115 public int getRunLimit(final Set<? extends Attribute> attributes) {
116 return this.realIterators.get(this.currentList).getRunLimit(attributes)
117 + this.currentOffset();
118 }
119
120
121 public int getRunStart() {
122 return this.realIterators.get(this.currentList).getRunStart()
123 + this.currentOffset();
124 }
125
126
127 public int getRunStart(final Attribute attribute) {
128 return this.realIterators.get(this.currentList).getRunStart(attribute)
129 + this.currentOffset();
130 }
131
132
133 public int getRunStart(final Set<? extends Attribute> attributes) {
134 return this.realIterators.get(this.currentList).getRunStart(attributes)
135 + this.currentOffset();
136 }
137
138
139 public char current() {
140 return this.realIterators.get(this.currentList).current();
141 }
142
143
144 public char first() {
145 this.currentList = 0;
146 return this.realIterators.get(this.currentList).first();
147 }
148
149
150 public int getBeginIndex() {
151 return 0;
152 }
153
154
155 public int getEndIndex() {
156 return this.sumUpToNotIncluding(this.realIterators.size());
157 }
158
159
160 public int getIndex() {
161 return this.currentOffset()
162 + this.realIterators.get(this.currentList).getIndex();
163 }
164
165
166 public char last() {
167 this.currentList = this.realIterators.size() - 1;
168 return this.realIterators.get(this.currentList).last();
169 }
170
171
172 public char next() {
173 char c = this.realIterators.get(this.currentList).next();
174 while ((c == CharacterIterator.DONE)
175 && (this.currentList < this.realIterators.size() - 1)) {
176 this.currentList++;
177 c = this.realIterators.get(this.currentList).first();
178 }
179 return c;
180 }
181
182
183 public char previous() {
184 char c = this.realIterators.get(this.currentList).previous();
185 while ((c == CharacterIterator.DONE) && (this.currentList > 0)) {
186 this.currentList--;
187 c = this.realIterators.get(this.currentList).previous();
188 }
189 return c;
190 }
191
192
193 public char setIndex(final int position) {
194 int prev = 0;
195 int offset = 0;
196 for (int i = 0; i < this.realIterators.size(); i++) {
197 final AttributedCharacterIterator ci = this.realIterators.get(i);
198 prev = offset;
199 final int beginIndex = ci.getBeginIndex();
200 offset += ci.getEndIndex() - beginIndex;
201 if (((prev <= position) && (offset > position))
202 || ((offset == position) && (i == this.realIterators.size() - 1))) {
203 this.currentList = i;
204 return ci.setIndex(beginIndex + position - prev);
205 }
206 }
207 throw new IllegalArgumentException();
208 }
209 }