1 17 18 19 20 package org.apache.fop.hyphenation; 21 22 import java.io.Serializable ; 23 24 30 public class CharVector implements Cloneable , Serializable { 31 32 35 private static final int DEFAULT_BLOCK_SIZE = 2048; 36 private int blockSize; 37 38 41 private char[] array; 42 43 46 private int n; 47 48 public CharVector() { 49 this(DEFAULT_BLOCK_SIZE); 50 } 51 52 public CharVector(int capacity) { 53 if (capacity > 0) { 54 blockSize = capacity; 55 } else { 56 blockSize = DEFAULT_BLOCK_SIZE; 57 } 58 array = new char[blockSize]; 59 n = 0; 60 } 61 62 public CharVector(char[] a) { 63 blockSize = DEFAULT_BLOCK_SIZE; 64 array = a; 65 n = a.length; 66 } 67 68 public CharVector(char[] a, int capacity) { 69 if (capacity > 0) { 70 blockSize = capacity; 71 } else { 72 blockSize = DEFAULT_BLOCK_SIZE; 73 } 74 array = a; 75 n = a.length; 76 } 77 78 81 public void clear() { 82 n = 0; 83 } 84 85 public Object clone() { 86 CharVector cv = new CharVector((char[])array.clone(), blockSize); 87 cv.n = this.n; 88 return cv; 89 } 90 91 public char[] getArray() { 92 return array; 93 } 94 95 98 public int length() { 99 return n; 100 } 101 102 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 } 138 | Popular Tags |