Path: blob/master/test/jdk/java/text/BreakIterator/MirroredBreakIterator.java
41149 views
/*1* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.text.BreakIterator;24import java.text.CharacterIterator;25import java.util.ArrayList;26import java.util.Collections;27import java.util.Iterator;28import java.util.List;29import java.util.NoSuchElementException;3031public class MirroredBreakIterator extends BreakIterator {32private final List<Integer> boundaries;33private int charIndex;34private int boundaryIndex;3536MirroredBreakIterator(BreakIterator bi) {37List<Integer> b = new ArrayList<Integer>();38int i = bi.first();39charIndex = i;40for (; i != DONE; i = bi.next()) {41b.add(i);42}43boundaries = Collections.unmodifiableList(b);44}4546@Override47public Object clone() {48try {49return super.clone();50} catch (Exception e) {51throw new RuntimeException("clone failed", e);52}53}5455@Override56public int first() {57return changeIndices(0);58}5960@Override61public int last() {62return changeIndices(boundaries.size() - 1);63}6465@Override66public int next(int n) {67if (n == 0) {68return current();69}70int newBoundary = boundaryIndex + n;71if (newBoundary < 0) {72first();73return DONE;74}75if (newBoundary > lastBoundary()) {76last();77return DONE;78}79return changeIndices(newBoundary);80}8182@Override83public int next() {84if (boundaryIndex == lastBoundary()) {85return DONE;86}87return changeIndices(boundaryIndex + 1);88}8990@Override91public int previous() {92if (boundaryIndex == 0) {93return DONE;94}95return changeIndices(boundaryIndex - 1);96}9798@Override99public int following(int offset) {100validateOffset(offset);101for (int b = 0; b <= lastBoundary(); b++) {102int i = boundaries.get(b);103if (i > offset) {104return changeIndices(i, b);105}106}107return DONE;108}109110@Override111public int preceding(int offset) {112validateOffset(offset);113for (int b = lastBoundary(); b >= 0; b--) {114int i = boundaries.get(b);115if (i < offset) {116return changeIndices(i, b);117}118}119return DONE;120}121122@Override123public boolean isBoundary(int offset) {124// Call the default impelementation in BreakIterator125return super.isBoundary(offset);126}127128@Override129public int current() {130return charIndex;131}132133@Override134public CharacterIterator getText() {135throw new UnsupportedOperationException();136}137138@Override139public void setText(CharacterIterator newText) {140throw new UnsupportedOperationException();141}142143private int lastBoundary() {144return boundaries.size() - 1;145}146147private int changeIndices(int newCharIndex, int newBoundary) {148boundaryIndex = newBoundary;149return charIndex = newCharIndex;150}151152private int changeIndices(int newBoundary) {153try {154return changeIndices(boundaries.get(newBoundary), newBoundary);155} catch (IndexOutOfBoundsException e) {156throw new IllegalArgumentException(e);157}158}159160private void validateOffset(int offset) {161if (offset < boundaries.get(0) || offset > boundaries.get(lastBoundary())) {162throw new IllegalArgumentException();163}164}165}166167168