001: /*
002: * Copyright 1999-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package com.lowagie.text.pdf.hyphenation;
018:
019: import java.io.Serializable;
020:
021: /**
022: * This class implements a simple char vector with access to the
023: * underlying array.
024: *
025: * @author Carlos Villegas <cav@uniscope.co.jp>
026: */
027: public class CharVector implements Cloneable, Serializable {
028:
029: private static final long serialVersionUID = -4875768298308363544L;
030: /**
031: * Capacity increment size
032: */
033: private static final int DEFAULT_BLOCK_SIZE = 2048;
034: private int blockSize;
035:
036: /**
037: * The encapsulated array
038: */
039: private char[] array;
040:
041: /**
042: * Points to next free item
043: */
044: private int n;
045:
046: public CharVector() {
047: this (DEFAULT_BLOCK_SIZE);
048: }
049:
050: public CharVector(int capacity) {
051: if (capacity > 0) {
052: blockSize = capacity;
053: } else {
054: blockSize = DEFAULT_BLOCK_SIZE;
055: }
056: array = new char[blockSize];
057: n = 0;
058: }
059:
060: public CharVector(char[] a) {
061: blockSize = DEFAULT_BLOCK_SIZE;
062: array = a;
063: n = a.length;
064: }
065:
066: public CharVector(char[] a, int capacity) {
067: if (capacity > 0) {
068: blockSize = capacity;
069: } else {
070: blockSize = DEFAULT_BLOCK_SIZE;
071: }
072: array = a;
073: n = a.length;
074: }
075:
076: /**
077: * Reset Vector but don't resize or clear elements
078: */
079: public void clear() {
080: n = 0;
081: }
082:
083: public Object clone() {
084: CharVector cv = new CharVector((char[]) array.clone(),
085: blockSize);
086: cv.n = this .n;
087: return cv;
088: }
089:
090: public char[] getArray() {
091: return array;
092: }
093:
094: /**
095: * return number of items in array
096: */
097: public int length() {
098: return n;
099: }
100:
101: /**
102: * returns current capacity of array
103: */
104: public int capacity() {
105: return array.length;
106: }
107:
108: public void put(int index, char val) {
109: array[index] = val;
110: }
111:
112: public char get(int index) {
113: return array[index];
114: }
115:
116: public int alloc(int size) {
117: int index = n;
118: int len = array.length;
119: if (n + size >= len) {
120: char[] aux = new char[len + blockSize];
121: System.arraycopy(array, 0, aux, 0, len);
122: array = aux;
123: }
124: n += size;
125: return index;
126: }
127:
128: public void trimToSize() {
129: if (n < array.length) {
130: char[] aux = new char[n];
131: System.arraycopy(array, 0, aux, 0, n);
132: array = aux;
133: }
134: }
135:
136: }
|