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 017package com.itextpdf.text.pdf.hyphenation; 018 019import 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 */ 027public 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(), blockSize); 085 cv.n = this.n; 086 return cv; 087 } 088 089 public char[] getArray() { 090 return array; 091 } 092 093 /** 094 * return number of items in array 095 */ 096 public int length() { 097 return n; 098 } 099 100 /** 101 * returns current capacity of array 102 */ 103 public int capacity() { 104 return array.length; 105 } 106 107 public void put(int index, char val) { 108 array[index] = val; 109 } 110 111 public char get(int index) { 112 return array[index]; 113 } 114 115 public int alloc(int size) { 116 int index = n; 117 int len = array.length; 118 if (n + size >= len) { 119 char[] aux = new char[len + blockSize]; 120 System.arraycopy(array, 0, aux, 0, len); 121 array = aux; 122 } 123 n += size; 124 return index; 125 } 126 127 public void trimToSize() { 128 if (n < array.length) { 129 char[] aux = new char[n]; 130 System.arraycopy(array, 0, aux, 0, n); 131 array = aux; 132 } 133 } 134 135}