001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: CopyOnWriteArrayList.java,v 1.3 2005/12/04 13:46:04 jesper Exp $
023: package net.infonode.util.collection;
024:
025: import java.util.Collection;
026: import java.util.Iterator;
027:
028: /**
029: * @author $Author: jesper $
030: * @version $Revision: 1.3 $
031: */
032: public final class CopyOnWriteArrayList {
033: private static class IteratorImpl implements Iterator {
034: private Object[] e;
035: private int size;
036: private int index;
037:
038: IteratorImpl(Object[] e, int size, int index) {
039: this .e = e;
040: this .size = size;
041: this .index = index;
042: }
043:
044: public void remove() {
045: throw new UnsupportedOperationException();
046: }
047:
048: public boolean hasNext() {
049: return index < size;
050: }
051:
052: public Object next() {
053: return e[index++];
054: }
055: }
056:
057: private Object[] elements;
058: private int size;
059:
060: public CopyOnWriteArrayList(int initialCapacity) {
061: elements = new Object[initialCapacity];
062: }
063:
064: public void removeAll(Collection toRemove) {
065: Object[] ne = new Object[size - toRemove.size()];
066: int j = 0;
067:
068: for (int i = 0; i < size; i++) {
069: if (!toRemove.contains(elements[i])) {
070: ne[j++] = elements[i];
071: }
072: }
073:
074: size = j;
075: elements = ne;
076: }
077:
078: public void add(Object element) {
079: if (size >= elements.length) {
080: Object[] newElements = new Object[getPreferredSize(size)];
081: System.arraycopy(elements, 0, newElements, 0, size);
082: elements = newElements;
083: }
084:
085: elements[size++] = element;
086: }
087:
088: public boolean remove(Object element) {
089: int index = indexOf(element);
090:
091: if (index == -1)
092: return false;
093:
094: remove(index);
095: return true;
096: }
097:
098: public void remove(int index) {
099: size--;
100: Object[] newElements = new Object[getPreferredSize(size)];
101: System.arraycopy(elements, 0, newElements, 0, index);
102: System.arraycopy(elements, index + 1, newElements, index, size
103: - index);
104: elements = newElements;
105: }
106:
107: public int indexOf(Object element) {
108: for (int i = 0; i < size; i++)
109: if (elements[i] == element)
110: return i;
111:
112: return -1;
113: }
114:
115: public void each(Closure closure) {
116: Object[] l = elements;
117: int s = size;
118:
119: for (int i = 0; i < s; i++)
120: closure.apply(l[i]);
121: }
122:
123: public Iterator iterator() {
124: return new IteratorImpl(elements, size, 0);
125: }
126:
127: private static int getPreferredSize(int size) {
128: return size * 3 / 2 + 1;
129: }
130:
131: public int size() {
132: return size;
133: }
134:
135: public Object get(int index) {
136: return elements[index];
137: }
138:
139: public Object[] getElements() {
140: return elements;
141: }
142:
143: /* private static class Iterator implements java.util.Iterator {
144: private Object[] elements;
145: private int size;
146: private int index;
147:
148: Iterator(Object[] elements, int size) {
149: this.elements = elements;
150: this.size = size;
151: }
152:
153: public void remove() {
154: throw new UnsupportedOperationException();
155: }
156:
157: public boolean hasNext() {
158: return index < size;
159: }
160:
161: public Object next() {
162: return elements[index++];
163: }
164: }*/
165:
166: }
|