From e972ebc72b2bcf0f6d68550b11499b9bcd10f2da Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Wed, 24 Feb 2010 22:01:25 +0000 Subject: [PATCH] Added toSet(). --- .../com/samskivert/depot/util/Sequence.java | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/java/com/samskivert/depot/util/Sequence.java b/src/java/com/samskivert/depot/util/Sequence.java index 5ac5c0f..c1c3b38 100644 --- a/src/java/com/samskivert/depot/util/Sequence.java +++ b/src/java/com/samskivert/depot/util/Sequence.java @@ -24,6 +24,7 @@ import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Collection; +import java.util.HashSet; import java.util.Iterator; import com.google.common.base.Function; @@ -59,11 +60,10 @@ public abstract class Sequence implements Iterable return source.isEmpty(); } public ArrayList toList () { - ArrayList list = new ArrayList(source.size()); - for (F elem : source) { - list.add(func.apply(elem)); - } - return list; + return copyInto(new ArrayList(source.size())); + } + public HashSet toSet () { + return copyInto(new HashSet(source.size())); } // In a perfect world this would be S[] toArray (Class clazz) public T[] toArray (Class clazz) { @@ -75,6 +75,12 @@ public abstract class Sequence implements Iterable } return array; } + protected > C copyInto (C coll) { + for (F elem : source) { + coll.add(func.apply(elem)); + } + return coll; + } }; } @@ -96,6 +102,11 @@ public abstract class Sequence implements Iterable */ public abstract ArrayList toList (); + /** + * Converts this sequence into a HashSet. + */ + public abstract HashSet toSet (); + /** * Converts this sequence into an array. * I wish this were S[] toArray (Class clazz);