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 CharArrayArray extends ValueArray {
044:
045: private CharArray[] _array;
046:
047: private CharArrayArray _readOnlyArray;
048:
049: public CharArrayArray(int initialCapacity, int maximumCapacity) {
050: _array = new CharArray[initialCapacity];
051: _maximumCapacity = maximumCapacity;
052: }
053:
054: public CharArrayArray() {
055: this (DEFAULT_CAPACITY, MAXIMUM_CAPACITY);
056: }
057:
058: public final void clear() {
059: for (int i = 0; i < _size; i++) {
060: _array[i] = null;
061: }
062: _size = 0;
063: }
064:
065: public final CharArray[] getArray() {
066: return _array;
067: }
068:
069: public final void setReadOnlyArray(ValueArray readOnlyArray,
070: boolean clear) {
071: if (!(readOnlyArray instanceof CharArrayArray)) {
072: throw new IllegalArgumentException(CommonResourceBundle
073: .getInstance().getString("message.illegalClass",
074: new Object[] { readOnlyArray }));
075: }
076:
077: setReadOnlyArray((CharArrayArray) readOnlyArray, clear);
078: }
079:
080: public final void setReadOnlyArray(CharArrayArray readOnlyArray,
081: boolean clear) {
082: if (readOnlyArray != null) {
083: _readOnlyArray = readOnlyArray;
084: _readOnlyArraySize = readOnlyArray.getSize();
085:
086: if (clear) {
087: clear();
088: }
089: }
090: }
091:
092: public final CharArray get(int i) {
093: if (_readOnlyArray == null) {
094: return _array[i];
095: } else {
096: if (i < _readOnlyArraySize) {
097: return _readOnlyArray.get(i);
098: } else {
099: return _array[i - _readOnlyArraySize];
100: }
101: }
102: }
103:
104: public final void add(CharArray s) {
105: if (_size == _array.length) {
106: resize();
107: }
108:
109: _array[_size++] = s;
110: }
111:
112: protected final void resize() {
113: if (_size == _maximumCapacity) {
114: throw new ValueArrayResourceException(CommonResourceBundle
115: .getInstance()
116: .getString("message.arrayMaxCapacity"));
117: }
118:
119: int newSize = _size * 3 / 2 + 1;
120: if (newSize > _maximumCapacity) {
121: newSize = _maximumCapacity;
122: }
123:
124: final CharArray[] newArray = new CharArray[newSize];
125: System.arraycopy(_array, 0, newArray, 0, _size);
126: _array = newArray;
127: }
128: }
|