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