1 38 39 40 package com.sun.xml.fastinfoset.util; 41 import com.sun.xml.fastinfoset.CommonResourceBundle; 42 43 public class StringArray extends ValueArray { 44 45 public String [] _array; 46 47 private StringArray _readOnlyArray; 48 49 public StringArray(int initialCapacity, int maximumCapacity) { 50 _array = new String [initialCapacity]; 51 _maximumCapacity = maximumCapacity; 52 } 53 54 public StringArray() { 55 this(DEFAULT_CAPACITY, MAXIMUM_CAPACITY); 56 } 57 58 public final void clear() { 59 for (int i = _readOnlyArraySize; i < _size; i++) { 60 _array[i] = null; 61 } 62 _size = _readOnlyArraySize; 63 } 64 65 public final String [] getArray() { 66 return _array; 67 } 68 69 public final void setReadOnlyArray(ValueArray readOnlyArray, boolean clear) { 70 if (!(readOnlyArray instanceof StringArray)) { 71 throw new IllegalArgumentException (CommonResourceBundle.getInstance(). 72 getString("message.illegalClass", new Object []{readOnlyArray})); 73 } 74 75 setReadOnlyArray((StringArray)readOnlyArray, clear); 76 } 77 78 public final void setReadOnlyArray(StringArray readOnlyArray, boolean clear) { 79 if (readOnlyArray != null) { 80 _readOnlyArray = readOnlyArray; 81 _readOnlyArraySize = readOnlyArray.getSize(); 82 83 if (clear) { 84 clear(); 85 } 86 87 _array = getCompleteArray(); 88 _size = _readOnlyArraySize; 89 } 90 } 91 92 public final String [] getCompleteArray() { 93 if (_readOnlyArray == null) { 94 return _array; 95 } else { 96 final String [] ra = _readOnlyArray.getCompleteArray(); 97 final String [] a = new String [_readOnlyArraySize + _array.length]; 98 System.arraycopy(ra, 0, a, 0, _readOnlyArraySize); 99 return a; 100 } 101 } 102 103 public final String get(int i) { 104 return _array[i]; 105 } 106 107 public final int add(String s) { 108 if (_size == _array.length) { 109 resize(); 110 } 111 112 _array[_size++] = s; 113 return _size; 114 } 115 116 protected final void resize() { 117 if (_size == _maximumCapacity) { 118 throw new ValueArrayResourceException(CommonResourceBundle.getInstance().getString("message.arrayMaxCapacity")); 119 } 120 121 int newSize = _size * 3 / 2 + 1; 122 if (newSize > _maximumCapacity) { 123 newSize = _maximumCapacity; 124 } 125 126 final String [] newArray = new String [newSize]; 127 System.arraycopy(_array, 0, newArray, 0, _size); 128 _array = newArray; 129 } 130 } 131 | Popular Tags |