pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / icu / src / java / org / apache / lucene / analysis / icu / segmentation / CharArrayIterator.java
1 package org.apache.lucene.analysis.icu.segmentation;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.text.CharacterIterator;
21
22 /**
23  * Wraps a char[] as CharacterIterator for processing with a BreakIterator
24  * @lucene.experimental
25  */
26 final class CharArrayIterator implements CharacterIterator {
27   private char array[];
28   private int start;
29   private int index;
30   private int length;
31   private int limit;
32
33   public char [] getText() {
34     return array;
35   }
36   
37   public int getStart() {
38     return start;
39   }
40   
41   public int getLength() {
42     return length;
43   }
44   
45   /**
46    * Set a new region of text to be examined by this iterator
47    * 
48    * @param array text buffer to examine
49    * @param start offset into buffer
50    * @param length maximum length to examine
51    */
52   void setText(final char array[], int start, int length) {
53     this.array = array;
54     this.start = start;
55     this.index = start;
56     this.length = length;
57     this.limit = start + length;
58   }
59
60   public char current() {
61     return (index == limit) ? DONE : array[index];
62   }
63
64   public char first() {
65     index = start;
66     return current();
67   }
68
69   public int getBeginIndex() {
70     return 0;
71   }
72
73   public int getEndIndex() {
74     return length;
75   }
76
77   public int getIndex() {
78     return index - start;
79   }
80
81   public char last() {
82     index = (limit == start) ? limit : limit - 1;
83     return current();
84   }
85
86   public char next() {
87     if (++index >= limit) {
88       index = limit;
89       return DONE;
90     } else {
91       return current();
92     }
93   }
94
95   public char previous() {
96     if (--index < start) {
97       index = start;
98       return DONE;
99     } else {
100       return current();
101     }
102   }
103
104   public char setIndex(int position) {
105     if (position < getBeginIndex() || position > getEndIndex())
106       throw new IllegalArgumentException("Illegal Position: " + position);
107     index = start + position;
108     return current();
109   }
110
111   @Override
112   public Object clone() {
113     CharArrayIterator clone = new CharArrayIterator();
114     clone.setText(array, start, length);
115     clone.index = index;
116     return clone;
117   }
118 }