jdk/test/java/text/BreakIterator/MirroredBreakIterator.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright (c) 2007 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
import java.text.BreakIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.text.CharacterIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.Collections;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.List;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.NoSuchElementException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public class MirroredBreakIterator extends BreakIterator {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    private final List<Integer> boundaries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private int charIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private int boundaryIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    MirroredBreakIterator(BreakIterator bi) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        List<Integer> b = new ArrayList<Integer>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        int i = bi.first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        charIndex = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        for (; i != DONE; i = bi.next()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            b.add(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        boundaries = Collections.unmodifiableList(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    public Object clone() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            return super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            throw new RuntimeException("clone failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public int first() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return changeIndices(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public int last() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        return changeIndices(boundaries.size() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public int next(int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            return current();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        int newBoundary = boundaryIndex + n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (newBoundary < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            first();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (newBoundary > lastBoundary()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            last();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return changeIndices(newBoundary);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public int next() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (boundaryIndex == lastBoundary()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return changeIndices(boundaryIndex + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public int previous() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (boundaryIndex == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        return changeIndices(boundaryIndex - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public int following(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        validateOffset(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        for (int b = 0; b <= lastBoundary(); b++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            int i = boundaries.get(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (i > offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                return changeIndices(i, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public int preceding(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        validateOffset(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        for (int b = lastBoundary(); b >= 0; b--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            int i = boundaries.get(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (i < offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                return changeIndices(i, b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return DONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public boolean isBoundary(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // Call the default impelementation in BreakIterator
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        return super.isBoundary(offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public int current() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return charIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public CharacterIterator getText() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    @Override
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public void setText(CharacterIterator newText) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    private int lastBoundary() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        return boundaries.size() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private int changeIndices(int newCharIndex, int newBoundary) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        boundaryIndex = newBoundary;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return charIndex = newCharIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private int changeIndices(int newBoundary) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return changeIndices(boundaries.get(newBoundary), newBoundary);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } catch (IndexOutOfBoundsException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            throw new IllegalArgumentException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private void validateOffset(int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (offset < boundaries.get(0) || offset > boundaries.get(lastBoundary())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
}