From 15ca8973bcc01920e3e5ec68970397eec62bca76 Mon Sep 17 00:00:00 2001 From: Dave Hoover Date: Fri, 8 May 2009 18:57:58 +0000 Subject: [PATCH] KeyWrapper is pretty handy when you really just want a DSet stuffed full of some pre-existing Streamable/Comparable type (e.g. String), so let's buff it up to let us do that cleanly, and publicize it. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5771 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../com/threerings/presents/dobj/DSet.java | 47 +++++++++++-------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/src/java/com/threerings/presents/dobj/DSet.java b/src/java/com/threerings/presents/dobj/DSet.java index 8d76fc761..01d05fe9c 100644 --- a/src/java/com/threerings/presents/dobj/DSet.java +++ b/src/java/com/threerings/presents/dobj/DSet.java @@ -68,6 +68,24 @@ public class DSet Comparable getKey (); } + /** + * A quick and easy DSet.Entry that holds some sort of Comparable. + * + * Remember: this type must also be {@link Streamable}. + */ + public static class KeyWrapper> implements DSet.Entry + { + public KeyWrapper (T key) { + _key = key; + } + + public T getKey () { + return _key; + } + + protected T _key; + } + /** * Creates a new DSet of the appropriate generic type. */ @@ -85,7 +103,7 @@ public class DSet } /** - * Compares the first comparable to the second. This is useful to avoid type safety warnings + * Compares the first comparable to the second. This is useful to avoid type safety warnings * when dealing with the keys of {@link DSet.Entry} values. */ public static int compare (Comparable c1, Comparable c2) @@ -138,9 +156,9 @@ public class DSet */ public DSet (E[] source) { - for (int ii = 0; ii < source.length; ii++) { - if (source[ii] != null) { - add(source[ii]); + for (E element : source) { + if (element != null) { + add(element); } } } @@ -186,7 +204,8 @@ public class DSet public E get (Comparable key) { // determine where we'll be adding the new element - int eidx = ArrayUtil.binarySearch(_entries, 0, _size, new KeyWrapper(key), ENTRY_COMP); + int eidx = ArrayUtil.binarySearch( + _entries, 0, _size, new KeyWrapper>(key), ENTRY_COMP); return (eidx < 0) ? null : _entries[eidx]; } @@ -371,7 +390,8 @@ public class DSet } // look up this entry's position in our set - int eidx = ArrayUtil.binarySearch(_entries, 0, _size, new KeyWrapper(key), ENTRY_COMP); + int eidx = ArrayUtil.binarySearch( + _entries, 0, _size, new KeyWrapper>(key), ENTRY_COMP); // if we found it, remove it if (eidx >= 0) { @@ -454,8 +474,7 @@ public class DSet { StringBuilder buf = new StringBuilder("("); String prefix = ""; - for (int i = 0; i < _entries.length; i++) { - Entry elem = _entries[i]; + for (E elem : _entries) { if (elem != null) { buf.append(prefix); prefix = ", "; @@ -494,18 +513,6 @@ public class DSet } } - /** Used to search for keys. */ - protected static class KeyWrapper implements DSet.Entry - { - public KeyWrapper (Comparable key) { - _key = key; - } - public Comparable getKey () { - return _key; - } - protected Comparable _key; - } - /** The entries of the set (in a sparse array). */ @SuppressWarnings("unchecked") protected E[] _entries = (E[])new Entry[INITIAL_CAPACITY];