001: /*
002: * Fast Infoset ver. 0.1 software ("Software")
003: *
004: * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * Software is licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License. You may
008: * obtain a copy of the License at:
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations.
016: *
017: * Sun supports and benefits from the global community of open source
018: * developers, and thanks the community for its important contributions and
019: * open standards-based technology, which Sun has adopted into many of its
020: * products.
021: *
022: * Please note that portions of Software may be provided with notices and
023: * open source licenses from such communities and third parties that govern the
024: * use of those portions, and any licenses granted hereunder do not alter any
025: * rights and obligations you may have under such open source licenses,
026: * however, the disclaimer of warranty and limitation of liability provisions
027: * in this License will apply to all Software in this distribution.
028: *
029: * You acknowledge that the Software is not designed, licensed or intended
030: * for use in the design, construction, operation or maintenance of any nuclear
031: * facility.
032: *
033: * Apache License
034: * Version 2.0, January 2004
035: * http://www.apache.org/licenses/
036: *
037: */
038:
039: package com.sun.xml.fastinfoset.util;
040:
041: import com.sun.xml.fastinfoset.CommonResourceBundle;
042:
043: public class StringArray extends ValueArray {
044:
045: public String[] _array;
046:
047: private StringArray _readOnlyArray;
048:
049: private boolean _clear;
050:
051: public StringArray(int initialCapacity, int maximumCapacity,
052: boolean clear) {
053: _array = new String[initialCapacity];
054: _maximumCapacity = maximumCapacity;
055: _clear = clear;
056: }
057:
058: public StringArray() {
059: this (DEFAULT_CAPACITY, MAXIMUM_CAPACITY, false);
060: }
061:
062: public final void clear() {
063: if (_clear)
064: for (int i = _readOnlyArraySize; i < _size; i++) {
065: _array[i] = null;
066: }
067: _size = _readOnlyArraySize;
068: }
069:
070: public final String[] getArray() {
071: return _array;
072: }
073:
074: public final void setReadOnlyArray(ValueArray readOnlyArray,
075: boolean clear) {
076: if (!(readOnlyArray instanceof StringArray)) {
077: throw new IllegalArgumentException(CommonResourceBundle
078: .getInstance().getString("message.illegalClass",
079: new Object[] { readOnlyArray }));
080: }
081:
082: setReadOnlyArray((StringArray) readOnlyArray, clear);
083: }
084:
085: public final void setReadOnlyArray(StringArray readOnlyArray,
086: boolean clear) {
087: if (readOnlyArray != null) {
088: _readOnlyArray = readOnlyArray;
089: _readOnlyArraySize = readOnlyArray.getSize();
090:
091: if (clear) {
092: clear();
093: }
094:
095: _array = getCompleteArray();
096: _size = _readOnlyArraySize;
097: }
098: }
099:
100: public final String[] getCompleteArray() {
101: if (_readOnlyArray == null) {
102: return _array;
103: } else {
104: final String[] ra = _readOnlyArray.getCompleteArray();
105: final String[] a = new String[_readOnlyArraySize
106: + _array.length];
107: System.arraycopy(ra, 0, a, 0, _readOnlyArraySize);
108: return a;
109: }
110: }
111:
112: public final String get(int i) {
113: return _array[i];
114: }
115:
116: public final int add(String s) {
117: if (_size == _array.length) {
118: resize();
119: }
120:
121: _array[_size++] = s;
122: return _size;
123: }
124:
125: protected final void resize() {
126: if (_size == _maximumCapacity) {
127: throw new ValueArrayResourceException(CommonResourceBundle
128: .getInstance()
129: .getString("message.arrayMaxCapacity"));
130: }
131:
132: int newSize = _size * 3 / 2 + 1;
133: if (newSize > _maximumCapacity) {
134: newSize = _maximumCapacity;
135: }
136:
137: final String[] newArray = new String[newSize];
138: System.arraycopy(_array, 0, newArray, 0, _size);
139: _array = newArray;
140: }
141: }
|