|
1 /* |
|
2 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 * |
|
25 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. |
|
26 */ |
|
27 |
|
28 package com.sun.xml.internal.fastinfoset.util; |
|
29 import com.sun.xml.internal.fastinfoset.CommonResourceBundle; |
|
30 |
|
31 public class StringArray extends ValueArray { |
|
32 |
|
33 public String[] _array; |
|
34 |
|
35 private StringArray _readOnlyArray; |
|
36 |
|
37 private boolean _clear; |
|
38 |
|
39 public StringArray(int initialCapacity, int maximumCapacity, boolean clear) { |
|
40 _array = new String[initialCapacity]; |
|
41 _maximumCapacity = maximumCapacity; |
|
42 _clear = clear; |
|
43 } |
|
44 |
|
45 public StringArray() { |
|
46 this(DEFAULT_CAPACITY, MAXIMUM_CAPACITY, false); |
|
47 } |
|
48 |
|
49 public final void clear() { |
|
50 if (_clear) for (int i = _readOnlyArraySize; i < _size; i++) { |
|
51 _array[i] = null; |
|
52 } |
|
53 _size = _readOnlyArraySize; |
|
54 } |
|
55 |
|
56 /** |
|
57 * Returns cloned version of internal String[]. |
|
58 * @return cloned version of internal String[]. |
|
59 */ |
|
60 public final String[] getArray() { |
|
61 if (_array == null) return null; |
|
62 |
|
63 final String[] clonedArray = new String[_array.length]; |
|
64 System.arraycopy(_array, 0, clonedArray, 0, _array.length); |
|
65 return clonedArray; |
|
66 } |
|
67 |
|
68 public final void setReadOnlyArray(ValueArray readOnlyArray, boolean clear) { |
|
69 if (!(readOnlyArray instanceof StringArray)) { |
|
70 throw new IllegalArgumentException(CommonResourceBundle.getInstance(). |
|
71 getString("message.illegalClass", new Object[]{readOnlyArray})); |
|
72 } |
|
73 |
|
74 setReadOnlyArray((StringArray)readOnlyArray, clear); |
|
75 } |
|
76 |
|
77 public final void setReadOnlyArray(StringArray readOnlyArray, boolean clear) { |
|
78 if (readOnlyArray != null) { |
|
79 _readOnlyArray = readOnlyArray; |
|
80 _readOnlyArraySize = readOnlyArray.getSize(); |
|
81 |
|
82 if (clear) { |
|
83 clear(); |
|
84 } |
|
85 |
|
86 _array = getCompleteArray(); |
|
87 _size = _readOnlyArraySize; |
|
88 } |
|
89 } |
|
90 |
|
91 public final String[] getCompleteArray() { |
|
92 if (_readOnlyArray == null) { |
|
93 // Return cloned version of internal _array |
|
94 return getArray(); |
|
95 // return _array; |
|
96 } else { |
|
97 final String[] ra = _readOnlyArray.getCompleteArray(); |
|
98 final String[] a = new String[_readOnlyArraySize + _array.length]; |
|
99 System.arraycopy(ra, 0, a, 0, _readOnlyArraySize); |
|
100 return a; |
|
101 } |
|
102 } |
|
103 |
|
104 public final String get(int i) { |
|
105 return _array[i]; |
|
106 } |
|
107 |
|
108 public final int add(String s) { |
|
109 if (_size == _array.length) { |
|
110 resize(); |
|
111 } |
|
112 |
|
113 _array[_size++] = s; |
|
114 return _size; |
|
115 } |
|
116 |
|
117 protected final void resize() { |
|
118 if (_size == _maximumCapacity) { |
|
119 throw new ValueArrayResourceException(CommonResourceBundle.getInstance().getString("message.arrayMaxCapacity")); |
|
120 } |
|
121 |
|
122 int newSize = _size * 3 / 2 + 1; |
|
123 if (newSize > _maximumCapacity) { |
|
124 newSize = _maximumCapacity; |
|
125 } |
|
126 |
|
127 final String[] newArray = new String[newSize]; |
|
128 System.arraycopy(_array, 0, newArray, 0, _size); |
|
129 _array = newArray; |
|
130 } |
|
131 } |