diff --git a/com.ibm.wala.core/src/com/ibm/wala/util/collections/Iterator2Collection.java b/com.ibm.wala.core/src/com/ibm/wala/util/collections/Iterator2Collection.java index bbc9f1fed..245372a07 100644 --- a/com.ibm.wala.core/src/com/ibm/wala/util/collections/Iterator2Collection.java +++ b/com.ibm.wala.core/src/com/ibm/wala/util/collections/Iterator2Collection.java @@ -23,17 +23,9 @@ import java.util.Set; * * @see Iterator2Iterable */ -public class Iterator2Collection implements Collection { - - private final Collection delegate; - - private Iterator2Collection(Iterator i, Collection delegate) { - this.delegate = delegate; - while (i.hasNext()) { - delegate.add(i.next()); - } - } +public abstract class Iterator2Collection implements Collection { + protected abstract Collection getDelegate(); /** * Just calls {@link #toSet(Iterator)} * @@ -47,124 +39,124 @@ public class Iterator2Collection implements Collection { /** * Returns a {@link Set} containing all elements in i. Note that duplicates will be removed. */ - public static Iterator2Collection toSet(Iterator i) throws IllegalArgumentException { + public static Iterator2Set toSet(Iterator i) throws IllegalArgumentException { if (i == null) { throw new IllegalArgumentException("i == null"); } - return new Iterator2Collection(i, new LinkedHashSet(5)); + return new Iterator2Set(i, new LinkedHashSet(5)); } /** * Returns a {@link List} containing all elements in i, preserving duplicates. */ - public static Iterator2Collection toList(Iterator i) throws IllegalArgumentException { + public static Iterator2List toList(Iterator i) throws IllegalArgumentException { if (i == null) { throw new IllegalArgumentException("i == null"); } - return new Iterator2Collection(i, new ArrayList(5)); + return new Iterator2List(i, new ArrayList(5)); } @Override public String toString() { - return delegate.toString(); + return getDelegate().toString(); } /* * @see java.util.Collection#size() */ public int size() { - return delegate.size(); + return getDelegate().size(); } /* * @see java.util.Collection#clear() */ public void clear() { - delegate.clear(); + getDelegate().clear(); } /* * @see java.util.Collection#isEmpty() */ public boolean isEmpty() { - return delegate.isEmpty(); + return getDelegate().isEmpty(); } /* * @see java.util.Collection#toArray() */ public Object[] toArray() { - return delegate.toArray(); + return getDelegate().toArray(); } /* * @see java.util.Collection#add(java.lang.Object) */ public boolean add(T arg0) { - return delegate.add(arg0); + return getDelegate().add(arg0); } /* * @see java.util.Collection#contains(java.lang.Object) */ public boolean contains(Object arg0) { - return delegate.contains(arg0); + return getDelegate().contains(arg0); } /* * @see java.util.Collection#remove(java.lang.Object) */ public boolean remove(Object arg0) { - return delegate.remove(arg0); + return getDelegate().remove(arg0); } /* * @see java.util.Collection#addAll(java.util.Collection) */ public boolean addAll(Collection arg0) { - return delegate.addAll(arg0); + return getDelegate().addAll(arg0); } /* * @see java.util.Collection#containsAll(java.util.Collection) */ public boolean containsAll(Collection arg0) { - return delegate.containsAll(arg0); + return getDelegate().containsAll(arg0); } /* * @see java.util.Collection#removeAll(java.util.Collection) */ public boolean removeAll(Collection arg0) { - return delegate.removeAll(arg0); + return getDelegate().removeAll(arg0); } /* * @see java.util.Collection#retainAll(java.util.Collection) */ public boolean retainAll(Collection arg0) { - return delegate.retainAll(arg0); + return getDelegate().retainAll(arg0); } /* * @see java.util.Collection#iterator() */ public Iterator iterator() { - return delegate.iterator(); + return getDelegate().iterator(); } @SuppressWarnings("hiding") public T[] toArray(T[] a) { - return delegate.toArray(a); + return getDelegate().toArray(a); } @Override public boolean equals(Object o) { - return delegate.equals(o); + return getDelegate().equals(o); } @Override public int hashCode() { - return delegate.hashCode(); + return getDelegate().hashCode(); } } diff --git a/com.ibm.wala.core/src/com/ibm/wala/util/collections/Iterator2List.java b/com.ibm.wala.core/src/com/ibm/wala/util/collections/Iterator2List.java new file mode 100644 index 000000000..48d21d068 --- /dev/null +++ b/com.ibm.wala.core/src/com/ibm/wala/util/collections/Iterator2List.java @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright (c) 2007 IBM Corporation. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package com.ibm.wala.util.collections; + +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; + +public class Iterator2List extends Iterator2Collection implements List { + + private final List delegate; + + public Iterator2List(Iterator i, List delegate) { + this.delegate = delegate; + while (i.hasNext()) { + delegate.add(i.next()); + } + } + + public void add(int index, T element) { + delegate.add(index, element); + } + + public boolean addAll(int index, Collection c) { + return delegate.addAll(index, c); + } + + public T get(int index) { + return delegate.get(index); + } + + public int indexOf(Object o) { + return delegate.indexOf(o); + } + + public int lastIndexOf(Object o) { + return delegate.lastIndexOf(o); + } + + public ListIterator listIterator() { + return delegate.listIterator(); + } + + public ListIterator listIterator(int index) { + return delegate.listIterator(index); + } + + public T remove(int index) { + return delegate.remove(index); + } + + public T set(int index, T element) { + return delegate.set(index, element); + } + + public List subList(int fromIndex, int toIndex) { + return delegate.subList(fromIndex, toIndex); + } + + @Override + protected Collection getDelegate() { + return delegate; + } + +} diff --git a/com.ibm.wala.core/src/com/ibm/wala/util/collections/Iterator2Set.java b/com.ibm.wala.core/src/com/ibm/wala/util/collections/Iterator2Set.java new file mode 100644 index 000000000..142492f6f --- /dev/null +++ b/com.ibm.wala.core/src/com/ibm/wala/util/collections/Iterator2Set.java @@ -0,0 +1,34 @@ +/******************************************************************************* + * Copyright (c) 2007 IBM Corporation. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package com.ibm.wala.util.collections; + +import java.util.Collection; +import java.util.Iterator; +import java.util.Set; + +public class Iterator2Set extends Iterator2Collection implements Set { + + private final Set delegate; + + protected Iterator2Set(Iterator i, Set delegate) { + this.delegate = delegate; + while (i.hasNext()) { + delegate.add(i.next()); + } + } + + @Override + protected Collection getDelegate() { + return delegate; + } + + +}