]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/classes/core/src/com/ibm/icu/text/DictionaryBreakEngine.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / main / classes / core / src / com / ibm / icu / text / DictionaryBreakEngine.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2012, International Business Machines Corporation and         *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.text;
8
9 import java.text.CharacterIterator;
10 import java.util.Stack;
11
12 abstract class DictionaryBreakEngine implements LanguageBreakEngine {
13     protected UnicodeSet fSet = new UnicodeSet();
14     private final int fTypes;
15
16     /**
17      * @param breakTypes A mask of the break iterators that can use this engine.
18      *  For example, (1 << KIND_WORD) | (1 << KIND_LINE) could be used by 
19      *  word iterators and line iterators, but not any other kind.
20      */
21     public DictionaryBreakEngine(int breakTypes) {
22         // TODO: consider using a java.util.BitSet with nbits <= 32
23         fTypes = breakTypes;
24     }
25
26     public boolean handles(int c, int breakType) {
27         return (breakType >= 0 && breakType < 32) && // breakType is in range
28                 ((1 << breakType) & fTypes) != 0 && // this type can use us
29                 fSet.contains(c); // we recognize the character
30     }
31
32     public int findBreaks(CharacterIterator text_, int startPos, int endPos, 
33             boolean reverse, int breakType, Stack<Integer> foundBreaks) {
34         if (breakType < 0 || breakType >= 32 ||
35                 ((1 << breakType) & fTypes) == 0) {
36             return 0;
37         }
38
39         int result = 0;
40         UCharacterIterator text = UCharacterIterator.getInstance(text_);
41         int start = text.getIndex();
42         int current, rangeStart, rangeEnd;
43         int c = text.current();
44         if (reverse) {
45             boolean isDict = fSet.contains(c);
46             while ((current = text.getIndex()) > startPos && isDict) {
47                 c = text.previous();
48                 isDict = fSet.contains(c);
49             }
50             rangeStart = (current < startPos) ? startPos :
51                 current + (isDict ? 0 : 1);
52             rangeEnd = start + 1;
53         } else {
54             while ((current = text.getIndex()) < endPos && fSet.contains(c)) {
55                 c = text.next();
56             }
57             rangeStart = start;
58             rangeEnd = current;
59         }
60
61         result = divideUpDictionaryRange(text, rangeStart, rangeEnd, foundBreaks);
62         text.setIndex(current);
63
64         return result;
65     }
66
67     protected abstract int divideUpDictionaryRange(UCharacterIterator text, 
68             int rangeStart, int rangeEnd, Stack<Integer> foundBreaks);
69 }