001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package java.util;
017:
018: import java.io.Serializable;
019:
020: /**
021: * To keep performance characteristics in line with Java community expectations,
022: * <code>Vector</code> is a wrapper around <code>ArrayList</code>. <a
023: * href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/Vector.html">[Sun
024: * docs]</a>
025: *
026: * @param <E> element type.
027: */
028: public class Vector<E> extends AbstractList<E> implements List<E>,
029: RandomAccess, Cloneable, Serializable {
030:
031: private transient ArrayList<E> arrayList;
032:
033: public Vector() {
034: arrayList = new ArrayList<E>();
035: }
036:
037: public Vector(Collection<? extends E> c) {
038: arrayList = new ArrayList<E>();
039: addAll(c);
040: }
041:
042: /**
043: * There is no speed advantage to pre-allocating array sizes in JavaScript, so
044: * the <code>intialCapacity</code> parameter is ignored. This constructor is
045: * only present for compatibility with JDK 1.5's API.
046: */
047: public Vector(int initialCapacity) {
048: arrayList = new ArrayList<E>(initialCapacity);
049: }
050:
051: @Override
052: public boolean add(E o) {
053: return arrayList.add(o);
054: }
055:
056: @Override
057: public void add(int index, E o) {
058: arrayList.add(index, o);
059: }
060:
061: @Override
062: public boolean addAll(Collection<? extends E> c) {
063: return arrayList.addAll(c);
064: }
065:
066: @Override
067: public boolean addAll(int index, Collection<? extends E> c) {
068: return arrayList.addAll(index, c);
069: }
070:
071: public void addElement(E o) {
072: add(o);
073: }
074:
075: @Override
076: public void clear() {
077: arrayList.clear();
078: }
079:
080: public Object clone() {
081: return new Vector<E>(this );
082: }
083:
084: @Override
085: public boolean contains(Object elem) {
086: return arrayList.contains(elem);
087: }
088:
089: @Override
090: public boolean containsAll(Collection<?> c) {
091: // TODO(jat): implement
092: throw new UnsupportedOperationException(
093: "containsAll not implemented");
094: }
095:
096: public void copyInto(Object[] objs) {
097: int i = -1;
098: int n = size();
099: while (++i < n) {
100: objs[i] = get(i);
101: }
102: }
103:
104: public E elementAt(int index) {
105: return get(index);
106: }
107:
108: public Enumeration<E> elements() {
109: // TODO(jat): implement
110: return null;
111: }
112:
113: /**
114: * There is no speed advantage to pre-allocating array sizes in JavaScript.
115: * This method is only present for compatibility with the JRE.
116: */
117: public void ensureCapacity(int ignoredCapacity) {
118: }
119:
120: public E firstElement() {
121: return get(0);
122: }
123:
124: @Override
125: public E get(int index) {
126: return arrayList.get(index);
127: }
128:
129: @Override
130: public int indexOf(Object elem) {
131: return arrayList.indexOf(elem);
132: }
133:
134: public int indexOf(Object elem, int index) {
135: return arrayList.indexOf(elem, index);
136: }
137:
138: public void insertElementAt(E o, int index) {
139: add(index, o);
140: }
141:
142: @Override
143: public boolean isEmpty() {
144: return (arrayList.size() == 0);
145: }
146:
147: @Override
148: public Iterator<E> iterator() {
149: return arrayList.iterator();
150: }
151:
152: public E lastElement() {
153: if (isEmpty()) {
154: throw new IndexOutOfBoundsException("last");
155: } else {
156: return get(size() - 1);
157: }
158: }
159:
160: @Override
161: public int lastIndexOf(Object o) {
162: return arrayList.lastIndexOf(o);
163: }
164:
165: public int lastIndexOf(Object o, int index) {
166: return arrayList.lastIndexOf(o, index);
167: }
168:
169: @Override
170: public E remove(int index) {
171: return arrayList.remove(index);
172: }
173:
174: @Override
175: public boolean removeAll(Collection<?> c) {
176: // TODO(jat): implement
177: throw new UnsupportedOperationException(
178: "removeAll not implemented");
179: }
180:
181: public void removeAllElements() {
182: clear();
183: }
184:
185: public boolean removeElement(Object o) {
186: return remove(o);
187: }
188:
189: public void removeElementAt(int index) {
190: remove(index);
191: }
192:
193: @Override
194: public E set(int index, E elem) {
195: return arrayList.set(index, elem);
196: }
197:
198: public void setElementAt(E o, int index) {
199: set(index, o);
200: }
201:
202: public void setSize(int size) {
203: arrayList.setSize(size);
204: }
205:
206: @Override
207: public int size() {
208: return arrayList.size();
209: }
210:
211: @Override
212: public List<E> subList(int fromIndex, int toIndex) {
213: return arrayList.subList(fromIndex, toIndex);
214: }
215:
216: @Override
217: public Object[] toArray() {
218: return arrayList.toArray();
219: }
220:
221: @Override
222: public <T> T[] toArray(T[] a) {
223: return arrayList.toArray(a);
224: }
225:
226: @Override
227: public String toString() {
228: return arrayList.toString();
229: }
230:
231: /**
232: * Currently ignored.
233: */
234: public void trimToSize() {
235: arrayList.trimToSize();
236: }
237:
238: @Override
239: protected void removeRange(int fromIndex, int endIndex) {
240: arrayList.removeRange(fromIndex, endIndex);
241: }
242: }
|