001: /*******************************************************************************
002: * Portions created by Sebastian Thomschke are copyright (c) 2005-2007 Sebastian
003: * Thomschke.
004: *
005: * All Rights Reserved. This program and the accompanying materials
006: * are made available under the terms of the Eclipse Public License v1.0
007: * which accompanies this distribution, and is available at
008: * http://www.eclipse.org/legal/epl-v10.html
009: *
010: * Contributors:
011: * Sebastian Thomschke - initial implementation.
012: *******************************************************************************/package net.sf.oval.internal.util;
013:
014: import java.util.Collection;
015: import java.util.Iterator;
016: import java.util.List;
017: import java.util.ListIterator;
018: import java.util.Set;
019:
020: import net.sf.oval.internal.CollectionFactoryHolder;
021:
022: /**
023: * @author Sebastian Thomschke
024: */
025: public class ListOrderedSet<E> implements Cloneable, Set<E>, List<E> {
026: private final List<E> list;
027:
028: /**
029: * Constructs a new, empty collection with an initial capacity (16).
030: */
031: public ListOrderedSet() {
032: list = CollectionFactoryHolder.getFactory().createList(16);
033: }
034:
035: /**
036: * Constructs a new, empty collection with the given initial capacity.
037: *
038: * @param initialCapacity the initial capacity
039: */
040: public ListOrderedSet(final int initialCapacity) {
041: list = CollectionFactoryHolder.getFactory().createList(
042: initialCapacity);
043: }
044:
045: public boolean add(final E e) {
046: return list.add(e);
047: }
048:
049: public void add(final int index, final E e) {
050: if (!list.contains(e))
051: list.add(index, e);
052: }
053:
054: public boolean addAll(final Collection<? extends E> c) {
055: boolean itemAdded = false;
056: for (final E o : c) {
057: if (!list.contains(o)) {
058: add(o);
059: itemAdded = true;
060: }
061: }
062: return itemAdded;
063: }
064:
065: public boolean addAll(final int index,
066: final Collection<? extends E> c) {
067: final List<E> tmp = CollectionFactoryHolder.getFactory()
068: .createList(c.size());
069: for (final E o : c) {
070: if (!list.contains(o)) {
071: tmp.add(o);
072: }
073: }
074: return list.addAll(index, tmp);
075: }
076:
077: public void clear() {
078: list.clear();
079: }
080:
081: @SuppressWarnings("unchecked")
082: @Override
083: public Object clone() throws CloneNotSupportedException {
084: final ListOrderedSet<E> os = (ListOrderedSet<E>) super .clone();
085: os.list.addAll(list);
086: return os;
087: }
088:
089: public boolean contains(final Object o) {
090: return list.contains(o);
091: }
092:
093: public boolean containsAll(final Collection<?> c) {
094: return list.containsAll(c);
095: }
096:
097: @Override
098: public boolean equals(final Object obj) {
099: if (this == obj)
100: return true;
101: if (obj == null)
102: return false;
103: if (getClass() != obj.getClass())
104: return false;
105: final ListOrderedSet other = (ListOrderedSet) obj;
106: if (list == null) {
107: if (other.list != null)
108: return false;
109: } else if (!list.equals(other.list))
110: return false;
111: return true;
112: }
113:
114: public E get(final int index) {
115: return list.get(index);
116: }
117:
118: @Override
119: public int hashCode() {
120: final int PRIME = 31;
121: int result = 1;
122: result = PRIME * result
123: + ((list == null) ? 0 : list.hashCode());
124: return result;
125: }
126:
127: public int indexOf(final Object o) {
128: return list.indexOf(o);
129: }
130:
131: public boolean isEmpty() {
132: return list.isEmpty();
133: }
134:
135: public Iterator<E> iterator() {
136: return list.iterator();
137: }
138:
139: public int lastIndexOf(final Object o) {
140: return list.lastIndexOf(o);
141: }
142:
143: public ListIterator<E> listIterator() {
144: return list.listIterator();
145: }
146:
147: public ListIterator<E> listIterator(final int index) {
148: return list.listIterator(index);
149: }
150:
151: public E remove(final int index) {
152: return list.remove(index);
153: }
154:
155: public boolean remove(final Object o) {
156: return list.remove(o);
157: }
158:
159: public boolean removeAll(final Collection<?> c) {
160: return list.removeAll(c);
161: }
162:
163: public boolean retainAll(final Collection<?> c) {
164: return list.retainAll(c);
165: }
166:
167: public E set(final int index, final E element) {
168: final int elementIndex = list.indexOf(element);
169:
170: if (elementIndex == index)
171: return element;
172:
173: if (elementIndex == -1) {
174: return list.set(index, element);
175: }
176: if (elementIndex > index) {
177: list.remove(element);
178: return list.set(index, element);
179: }
180:
181: // if (elementIndex < index)
182: list.remove(element);
183: return list.set(index - 1, element);
184: }
185:
186: public int size() {
187: return list.size();
188: }
189:
190: public List<E> subList(final int fromIndex, final int toIndex) {
191: return list.subList(fromIndex, toIndex);
192: }
193:
194: public Object[] toArray() {
195: return list.toArray();
196: }
197:
198: public <T> T[] toArray(final T[] a) {
199: return list.toArray(a);
200: }
201:
202: @Override
203: public String toString() {
204: return list.toString();
205: }
206: }
|