blob: 571cdeb7bbf458a1a6d5006d92e4c7c8c5c6ea83 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package javax.swing.text;
27
28import java.text.BreakIterator;
29import java.text.CharacterIterator;
30import java.text.StringCharacterIterator;
31import java.util.Arrays;
32
33/**
34 * A simple whitespace-based BreakIterator implementation.
35 *
36 * @author Sergey Groznyh
37 */
38class WhitespaceBasedBreakIterator extends BreakIterator {
39 private char[] text = new char[0];
40 private int[] breaks = new int[] { 0 } ;
41 private int pos = 0;
42
43 /**
44 * Calculate break positions eagerly parallel to reading text.
45 */
46 public void setText(CharacterIterator ci) {
47 int begin = ci.getBeginIndex();
48 text = new char[ci.getEndIndex() - begin];
49 int[] breaks0 = new int[text.length + 1];
50 int brIx = 0;
51 breaks0[brIx++] = begin;
52
53 int charIx = 0;
54 boolean inWs = false;
55 for (char c = ci.first(); c != CharacterIterator.DONE; c = ci.next()) {
56 text[charIx] = c;
57 boolean ws = Character.isWhitespace(c);
58 if (inWs && !ws) {
59 breaks0[brIx++] = charIx + begin;
60 }
61 inWs = ws;
62 charIx++;
63 }
64 if (text.length > 0) {
65 breaks0[brIx++] = text.length + begin;
66 }
67 System.arraycopy(breaks0, 0, breaks = new int[brIx], 0, brIx);
68 }
69
70 public CharacterIterator getText() {
71 return new StringCharacterIterator(new String(text));
72 }
73
74 public int first() {
75 return breaks[pos = 0];
76 }
77
78 public int last() {
79 return breaks[pos = breaks.length - 1];
80 }
81
82 public int current() {
83 return breaks[pos];
84 }
85
86 public int next() {
87 return (pos == breaks.length - 1 ? DONE : breaks[++pos]);
88 }
89
90 public int previous() {
91 return (pos == 0 ? DONE : breaks[--pos]);
92 }
93
94 public int next(int n) {
95 return checkhit(pos + n);
96 }
97
98 public int following(int n) {
99 return adjacent(n, 1);
100 }
101
102 public int preceding(int n) {
103 return adjacent(n, -1);
104 }
105
106 private int checkhit(int hit) {
107 if ((hit < 0) || (hit >= breaks.length)) {
108 return DONE;
109 } else {
110 return breaks[pos = hit];
111 }
112 }
113
114 private int adjacent(int n, int bias) {
115 int hit = Arrays.binarySearch(breaks, n);
116 int offset = (hit < 0 ? (bias < 0 ? -1 : -2) : 0);
117 return checkhit(Math.abs(hit) + bias + offset);
118 }
119}