001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: *
025: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
026: */
027:
028: /*
029: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
030: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
031: *
032: * This code is free software; you can redistribute it and/or modify it
033: * under the terms of the GNU General Public License version 2 only, as
034: * published by the Free Software Foundation. Sun designates this
035: * particular file as subject to the "Classpath" exception as provided
036: * by Sun in the LICENSE file that accompanied this code.
037: *
038: * This code is distributed in the hope that it will be useful, but WITHOUT
039: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
040: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
041: * version 2 for more details (a copy is included in the LICENSE file that
042: * accompanied this code).
043: *
044: * You should have received a copy of the GNU General Public License version
045: * 2 along with this work; if not, write to the Free Software Foundation,
046: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
047: *
048: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
049: * CA 95054 USA or visit www.sun.com if you need additional information or
050: * have any questions.
051: *
052: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
053: *
054: */
055:
056: package com.sun.xml.internal.fastinfoset.util;
057:
058: import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
059:
060: public class StringArray extends ValueArray {
061:
062: public String[] _array;
063:
064: private StringArray _readOnlyArray;
065:
066: public StringArray(int initialCapacity, int maximumCapacity) {
067: _array = new String[initialCapacity];
068: _maximumCapacity = maximumCapacity;
069: }
070:
071: public StringArray() {
072: this (DEFAULT_CAPACITY, MAXIMUM_CAPACITY);
073: }
074:
075: public final void clear() {
076: for (int i = _readOnlyArraySize; i < _size; i++) {
077: _array[i] = null;
078: }
079: _size = _readOnlyArraySize;
080: }
081:
082: public final String[] getArray() {
083: return _array;
084: }
085:
086: public final void setReadOnlyArray(ValueArray readOnlyArray,
087: boolean clear) {
088: if (!(readOnlyArray instanceof StringArray)) {
089: throw new IllegalArgumentException(CommonResourceBundle
090: .getInstance().getString("message.illegalClass",
091: new Object[] { readOnlyArray }));
092: }
093:
094: setReadOnlyArray((StringArray) readOnlyArray, clear);
095: }
096:
097: public final void setReadOnlyArray(StringArray readOnlyArray,
098: boolean clear) {
099: if (readOnlyArray != null) {
100: _readOnlyArray = readOnlyArray;
101: _readOnlyArraySize = readOnlyArray.getSize();
102:
103: if (clear) {
104: clear();
105: }
106:
107: _array = getCompleteArray();
108: _size = _readOnlyArraySize;
109: }
110: }
111:
112: public final String[] getCompleteArray() {
113: if (_readOnlyArray == null) {
114: return _array;
115: } else {
116: final String[] ra = _readOnlyArray.getCompleteArray();
117: final String[] a = new String[_readOnlyArraySize
118: + _array.length];
119: System.arraycopy(ra, 0, a, 0, _readOnlyArraySize);
120: return a;
121: }
122: }
123:
124: public final String get(int i) {
125: return _array[i];
126: }
127:
128: public final int add(String s) {
129: if (_size == _array.length) {
130: resize();
131: }
132:
133: _array[_size++] = s;
134: return _size;
135: }
136:
137: protected final void resize() {
138: if (_size == _maximumCapacity) {
139: throw new ValueArrayResourceException(CommonResourceBundle
140: .getInstance()
141: .getString("message.arrayMaxCapacity"));
142: }
143:
144: int newSize = _size * 3 / 2 + 1;
145: if (newSize > _maximumCapacity) {
146: newSize = _maximumCapacity;
147: }
148:
149: final String[] newArray = new String[newSize];
150: System.arraycopy(_array, 0, newArray, 0, _size);
151: _array = newArray;
152: }
153: }
|