001: /**
002: * MVEL (The MVFLEX Expression Language)
003: *
004: * Copyright (C) 2007 Christopher Brock, MVFLEX/Valhalla Project and the Codehaus
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may 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,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: */package org.mvel.util;
019:
020: import org.mvel.ImmutableElementException;
021:
022: import java.util.*;
023:
024: public class FastList extends AbstractList {
025: private Object[] elements;
026: private int size = 0;
027:
028: // private float threshold = 1.0f;
029: private boolean updated = false;
030:
031: public FastList(int size) {
032: elements = new Object[size];
033: }
034:
035: public FastList(Object[] elements) {
036: this .size = (this .elements = elements).length;
037: }
038:
039: public Object get(int index) {
040: return elements[index];
041: }
042:
043: public int size() {
044: return size;
045: }
046:
047: public boolean add(Object o) {
048: if (size == elements.length) {
049: increaseSize(elements.length * 2);
050: }
051:
052: elements[size++] = o;
053: return true;
054: }
055:
056: public Object set(int i, Object o) {
057: if (!updated)
058: copyArray();
059: Object old = elements[i];
060: elements[i] = o;
061: return old;
062: }
063:
064: public void add(int i, Object o) {
065: if (size == elements.length) {
066: increaseSize(elements.length * 2);
067: }
068:
069: for (int c = size; c != i; c--) {
070: elements[c] = elements[c - 1];
071: }
072: elements[i] = o;
073: size++;
074: }
075:
076: public Object remove(int i) {
077: Object old = elements[i];
078: for (int c = i + 1; c != size; c--) {
079: elements[c - 1] = elements[c];
080: }
081: size--;
082: return old;
083: }
084:
085: public int indexOf(Object o) {
086: if (o == null)
087: return -1;
088: for (int i = 0; i < elements.length; i++) {
089: if (o.equals(elements[i]))
090: return i;
091: }
092: return -1;
093: }
094:
095: public int lastIndexOf(Object o) {
096: if (o == null)
097: return -1;
098: for (int i = elements.length - 1; i != -1; i--) {
099: if (o.equals(elements[i]))
100: return i;
101: }
102: return -1;
103: }
104:
105: public void clear() {
106: elements = new Object[0];
107: }
108:
109: public boolean addAll(int i, Collection collection) {
110: int offset = collection.size();
111: ensureCapacity(offset);
112:
113: for (int c = i; c != (i + offset); c++) {
114: elements[c + offset + 1] = elements[c];
115: }
116:
117: int c = 0;
118: for (Object o : collection) {
119: elements[offset + c] = o;
120: }
121:
122: return true;
123: }
124:
125: public Iterator iterator() {
126: final int size = this .size;
127: return new Iterator() {
128: private int cursor = 0;
129:
130: public boolean hasNext() {
131: return cursor < size;
132: }
133:
134: public Object next() {
135: return elements[cursor++];
136: }
137:
138: public void remove() {
139: throw new ImmutableElementException(
140: "cannot change elements in immutable list");
141: }
142: };
143:
144: }
145:
146: public ListIterator listIterator() {
147: return super .listIterator(); //To change body of overridden methods use File | Settings | File Templates.
148: }
149:
150: public ListIterator listIterator(int i) {
151: return super .listIterator(i); //To change body of overridden methods use File | Settings | File Templates.
152: }
153:
154: public List subList(int i, int i1) {
155: return super .subList(i, i1); //To change body of overridden methods use File | Settings | File Templates.
156: }
157:
158: public boolean equals(Object o) {
159: return super .equals(o); //To change body of overridden methods use File | Settings | File Templates.
160: }
161:
162: public int hashCode() {
163: return super .hashCode(); //To change body of overridden methods use File | Settings | File Templates.
164: }
165:
166: protected void removeRange(int i, int i1) {
167: throw new RuntimeException("not implemented");
168: }
169:
170: public boolean isEmpty() {
171: return size == 0;
172: }
173:
174: public boolean contains(Object o) {
175: return indexOf(o) != -1;
176: }
177:
178: public Object[] toArray() {
179: if (!updated)
180: copyArray();
181: return elements;
182: }
183:
184: public Object[] toArray(Object[] objects) {
185: throw new RuntimeException("not implemented");
186: }
187:
188: public boolean remove(Object o) {
189: throw new RuntimeException("not implemented");
190: }
191:
192: public boolean containsAll(Collection collection) {
193: throw new RuntimeException("not implemented");
194: }
195:
196: public boolean addAll(Collection collection) {
197: return addAll(size, collection);
198: }
199:
200: public boolean removeAll(Collection collection) {
201: throw new RuntimeException("not implemented");
202: }
203:
204: public boolean retainAll(Collection collection) {
205: throw new RuntimeException("not implemented");
206: }
207:
208: private void ensureCapacity(int additional) {
209: if ((size + additional) > elements.length)
210: increaseSize((size + additional) * 2);
211: }
212:
213: private void copyArray() {
214: increaseSize(elements.length);
215: }
216:
217: private void increaseSize(int newSize) {
218: Object[] newElements = new Object[newSize];
219: for (int i = 0; i < elements.length; i++)
220: newElements[i] = elements[i];
221:
222: elements = newElements;
223:
224: updated = true;
225: }
226:
227: public String toString() {
228: return super .toString(); //To change body of overridden methods use File | Settings | File Templates.
229: }
230: }
|