allow Iterator2Collection.toSet() to return a Set, and similarly for toList()
git-svn-id: https://wala.svn.sourceforge.net/svnroot/wala/trunk@3422 f5eafffb-2e1d-0410-98e4-8ec43c5233c4
This commit is contained in:
parent
e08ed2663f
commit
ae57e6079c
|
@ -23,17 +23,9 @@ import java.util.Set;
|
|||
*
|
||||
* @see Iterator2Iterable
|
||||
*/
|
||||
public class Iterator2Collection<T> implements Collection<T> {
|
||||
|
||||
private final Collection<T> delegate;
|
||||
|
||||
private Iterator2Collection(Iterator<? extends T> i, Collection<T> delegate) {
|
||||
this.delegate = delegate;
|
||||
while (i.hasNext()) {
|
||||
delegate.add(i.next());
|
||||
}
|
||||
}
|
||||
public abstract class Iterator2Collection<T> implements Collection<T> {
|
||||
|
||||
protected abstract Collection<T> getDelegate();
|
||||
/**
|
||||
* Just calls {@link #toSet(Iterator)}
|
||||
*
|
||||
|
@ -47,124 +39,124 @@ public class Iterator2Collection<T> implements Collection<T> {
|
|||
/**
|
||||
* Returns a {@link Set} containing all elements in i. Note that duplicates will be removed.
|
||||
*/
|
||||
public static <T> Iterator2Collection<T> toSet(Iterator<? extends T> i) throws IllegalArgumentException {
|
||||
public static <T> Iterator2Set<T> toSet(Iterator<? extends T> i) throws IllegalArgumentException {
|
||||
if (i == null) {
|
||||
throw new IllegalArgumentException("i == null");
|
||||
}
|
||||
return new Iterator2Collection<T>(i, new LinkedHashSet<T>(5));
|
||||
return new Iterator2Set<T>(i, new LinkedHashSet<T>(5));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link List} containing all elements in i, preserving duplicates.
|
||||
*/
|
||||
public static <T> Iterator2Collection<T> toList(Iterator<? extends T> i) throws IllegalArgumentException {
|
||||
public static <T> Iterator2List<T> toList(Iterator<? extends T> i) throws IllegalArgumentException {
|
||||
if (i == null) {
|
||||
throw new IllegalArgumentException("i == null");
|
||||
}
|
||||
return new Iterator2Collection<T>(i, new ArrayList<T>(5));
|
||||
return new Iterator2List<T>(i, new ArrayList<T>(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<? extends T> 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<T> iterator() {
|
||||
return delegate.iterator();
|
||||
return getDelegate().iterator();
|
||||
}
|
||||
|
||||
@SuppressWarnings("hiding")
|
||||
public <T> 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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<T> extends Iterator2Collection<T> implements List<T> {
|
||||
|
||||
private final List<T> delegate;
|
||||
|
||||
public Iterator2List(Iterator<? extends T> i, List<T> 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<? extends T> 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<T> listIterator() {
|
||||
return delegate.listIterator();
|
||||
}
|
||||
|
||||
public ListIterator<T> 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<T> subList(int fromIndex, int toIndex) {
|
||||
return delegate.subList(fromIndex, toIndex);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Collection<T> getDelegate() {
|
||||
return delegate;
|
||||
}
|
||||
|
||||
}
|
|
@ -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<T> extends Iterator2Collection<T> implements Set<T> {
|
||||
|
||||
private final Set<T> delegate;
|
||||
|
||||
protected Iterator2Set(Iterator<? extends T> i, Set<T> delegate) {
|
||||
this.delegate = delegate;
|
||||
while (i.hasNext()) {
|
||||
delegate.add(i.next());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Collection<T> getDelegate() {
|
||||
return delegate;
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue