pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / analyzers / common / src / java / org / apache / lucene / analysis / compound / hyphenation / CharVector.java
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package org.apache.lucene.analysis.compound.hyphenation;
19
20 import java.io.Serializable;
21
22 /**
23  * This class implements a simple char vector with access to the underlying
24  * array.
25  * 
26  * This class has been taken from the Apache FOP project (http://xmlgraphics.apache.org/fop/). They have been slightly modified. 
27  */
28 public class CharVector implements Cloneable, Serializable {
29
30   /**
31    * Capacity increment size
32    */
33   private static final int DEFAULT_BLOCK_SIZE = 2048;
34
35   private int blockSize;
36
37   /**
38    * The encapsulated array
39    */
40   private char[] array;
41
42   /**
43    * Points to next free item
44    */
45   private int n;
46
47   public CharVector() {
48     this(DEFAULT_BLOCK_SIZE);
49   }
50
51   public CharVector(int capacity) {
52     if (capacity > 0) {
53       blockSize = capacity;
54     } else {
55       blockSize = DEFAULT_BLOCK_SIZE;
56     }
57     array = new char[blockSize];
58     n = 0;
59   }
60
61   public CharVector(char[] a) {
62     blockSize = DEFAULT_BLOCK_SIZE;
63     array = a;
64     n = a.length;
65   }
66
67   public CharVector(char[] a, int capacity) {
68     if (capacity > 0) {
69       blockSize = capacity;
70     } else {
71       blockSize = DEFAULT_BLOCK_SIZE;
72     }
73     array = a;
74     n = a.length;
75   }
76
77   /**
78    * Reset Vector but don't resize or clear elements
79    */
80   public void clear() {
81     n = 0;
82   }
83
84   @Override
85   public Object clone() {
86     CharVector cv = new CharVector(array.clone(), blockSize);
87     cv.n = this.n;
88     return cv;
89   }
90
91   public char[] getArray() {
92     return array;
93   }
94
95   /**
96    * return number of items in array
97    */
98   public int length() {
99     return n;
100   }
101
102   /**
103    * returns current capacity of array
104    */
105   public int capacity() {
106     return array.length;
107   }
108
109   public void put(int index, char val) {
110     array[index] = val;
111   }
112
113   public char get(int index) {
114     return array[index];
115   }
116
117   public int alloc(int size) {
118     int index = n;
119     int len = array.length;
120     if (n + size >= len) {
121       char[] aux = new char[len + blockSize];
122       System.arraycopy(array, 0, aux, 0, len);
123       array = aux;
124     }
125     n += size;
126     return index;
127   }
128
129   public void trimToSize() {
130     if (n < array.length) {
131       char[] aux = new char[n];
132       System.arraycopy(array, 0, aux, 0, n);
133       array = aux;
134     }
135   }
136
137 }