Clover coverage report - PLT Utilities Test Coverage (plt-20120304-r5436)
Coverage timestamp: Sat Mar 3 2012 22:01:56 CST
file stats: LOC: 1,001   Methods: 148
NCLOC: 563   Classes: 26
 
 Source file Conditionals Statements Methods TOTAL
CollectUtil.java 26.4% 29.1% 28.4% 28.5%
coverage coverage
 1    /*BEGIN_COPYRIGHT_BLOCK*
 2   
 3    PLT Utilities BSD License
 4   
 5    Copyright (c) 2007-2010 JavaPLT group at Rice University
 6    All rights reserved.
 7   
 8    Developed by: Java Programming Languages Team
 9    Rice University
 10    http://www.cs.rice.edu/~javaplt/
 11   
 12    Redistribution and use in source and binary forms, with or without modification, are permitted
 13    provided that the following conditions are met:
 14   
 15    - Redistributions of source code must retain the above copyright notice, this list of conditions
 16    and the following disclaimer.
 17    - Redistributions in binary form must reproduce the above copyright notice, this list of
 18    conditions and the following disclaimer in the documentation and/or other materials provided
 19    with the distribution.
 20    - Neither the name of the JavaPLT group, Rice University, nor the names of the library's
 21    contributors may be used to endorse or promote products derived from this software without
 22    specific prior written permission.
 23   
 24    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 25    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 26    FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND
 27    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 28    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 29    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 30    IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 31    OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 32   
 33    *END_COPYRIGHT_BLOCK*/
 34   
 35    package edu.rice.cs.plt.collect;
 36   
 37    import java.util.*;
 38    import java.util.concurrent.CopyOnWriteArrayList;
 39    import java.util.concurrent.CopyOnWriteArraySet;
 40    import java.io.Serializable;
 41    import edu.rice.cs.plt.lambda.*;
 42    import edu.rice.cs.plt.tuple.Option;
 43    import edu.rice.cs.plt.tuple.Pair;
 44    import edu.rice.cs.plt.iter.SizedIterable;
 45    import edu.rice.cs.plt.iter.IterUtil;
 46    import edu.rice.cs.plt.object.ObjectUtil;
 47   
 48    public final class CollectUtil {
 49   
 50    /** Prevents instance creation */
 51  0 private CollectUtil() {}
 52   
 53    /** A predicate that accepts Set-Object pairs such that the given object is contained by the set. */
 54    public static final Predicate2<Set<?>, Object> SET_CONTENTS_PREDICATE = new SetContentsPredicate();
 55   
 56    private static final class SetContentsPredicate implements Predicate2<Set<?>, Object>, Serializable {
 57  16 private SetContentsPredicate() {}
 58  0 public boolean contains(Set<?> set, Object val) { return set.contains(val); }
 59    }
 60   
 61    /**
 62    * Get a factory that produces HashSets by invoking the empty HashSet constructor.
 63    * @see HashSet#HashSet()
 64    */
 65  1 @SuppressWarnings("unchecked") public static <T> Thunk<Set<T>> hashSetFactory() {
 66  1 return (Thunk<Set<T>>) (Thunk<?>) DefaultHashSetFactory.INSTANCE;
 67    }
 68   
 69    private static final class DefaultHashSetFactory<T> implements Thunk<Set<T>>, Serializable {
 70    public static final DefaultHashSetFactory<Object> INSTANCE = new DefaultHashSetFactory<Object>();
 71  1 private DefaultHashSetFactory() {}
 72  1 public Set<T> value() { return new HashSet<T>(); }
 73    }
 74   
 75    /**
 76    * Get a factory that produces HashSets with the given initial capacity.
 77    * @see HashSet#HashSet(int)
 78    */
 79  3 public static <T> Thunk<Set<T>> hashSetFactory(int initialCapacity) {
 80  3 return new CustomHashSetFactory<T>(initialCapacity);
 81    }
 82   
 83    private static final class CustomHashSetFactory<T> implements Thunk<Set<T>>, Serializable {
 84    private final int _initialCapacity;
 85  3 public CustomHashSetFactory(int initialCapacity) { _initialCapacity = initialCapacity; }
 86  7 public Set<T> value() { return new HashSet<T>(_initialCapacity); }
 87    }
 88   
 89    /**
 90    * Get a factory that produces LinkedHashSets by invoking the empty LinkedHashSet constructor.
 91    * @see LinkedHashSet#LinkedHashSet()
 92    */
 93  0 @SuppressWarnings("unchecked") public static <T> Thunk<Set<T>> linkedHashSetFactory() {
 94  0 return (Thunk<Set<T>>) (Thunk<?>) DefaultLinkedHashSetFactory.INSTANCE;
 95    }
 96   
 97    private static final class DefaultLinkedHashSetFactory<T> implements Thunk<Set<T>>, Serializable {
 98    public static final DefaultLinkedHashSetFactory<Object> INSTANCE = new DefaultLinkedHashSetFactory<Object>();
 99  0 private DefaultLinkedHashSetFactory() {}
 100  0 public Set<T> value() { return new LinkedHashSet<T>(); }
 101    }
 102   
 103    /**
 104    * Get a factory that produces LinkedHashSets with the given initial capacity.
 105    * @see LinkedHashSet#LinkedHashSet(int)
 106    */
 107  0 public static <T> Thunk<Set<T>> linkedHashSetFactory(int initialCapacity) {
 108  0 return new CustomLinkedHashSetFactory<T>(initialCapacity);
 109    }
 110   
 111    private static final class CustomLinkedHashSetFactory<T> implements Thunk<Set<T>>, Serializable {
 112    private final int _initialCapacity;
 113  0 public CustomLinkedHashSetFactory(int initialCapacity) { _initialCapacity = initialCapacity; }
 114  0 public Set<T> value() { return new LinkedHashSet<T>(_initialCapacity); }
 115    }
 116   
 117    /**
 118    * Get a factory that produces TreeSets sorted according to the elements' natural order.
 119    * @see TreeSet#TreeSet()
 120    */
 121  3 @SuppressWarnings("unchecked")
 122    public static <T extends Comparable<? super T>> Thunk<Set<T>> treeSetFactory() {
 123    // not sure why the weakening cast is necessary here but not elsewhere
 124  3 return (Thunk<Set<T>>) (Thunk<? extends Set<?>>) DefaultTreeSetFactory.INSTANCE;
 125    }
 126   
 127    private static final class DefaultTreeSetFactory<T extends Comparable<? super T>>
 128    implements Thunk<Set<T>>, Serializable {
 129    public static final DefaultTreeSetFactory<String> INSTANCE = new DefaultTreeSetFactory<String>();
 130  1 private DefaultTreeSetFactory() {}
 131  9 public Set<T> value() { return new TreeSet<T>(); }
 132    }
 133   
 134    /**
 135    * Get a factory that produces TreeSets sorted according to the given comparator.
 136    * @see TreeSet#TreeSet(Comparator)
 137    */
 138  0 public static <T> Thunk<Set<T>> treeSetFactory(Comparator<? super T> comparator) {
 139  0 return new CustomTreeSetFactory<T>(comparator);
 140    }
 141   
 142    private static final class CustomTreeSetFactory<T> implements Thunk<Set<T>>, Serializable {
 143    private final Comparator<? super T> _comp;
 144  0 public CustomTreeSetFactory(Comparator<? super T> comp) { _comp = comp; }
 145  0 public Set<T> value() { return new TreeSet<T>(_comp); }
 146    }
 147   
 148    /**
 149    * Get a factory that produces CopyOnWriteArraySets by invoking the empty CopyOnWriteArraySet constructor.
 150    * @see CopyOnWriteArraySet#CopyOnWriteArraySet()
 151    */
 152  0 @SuppressWarnings("unchecked") public static <T> Thunk<Set<T>> copyOnWriteArraySetFactory() {
 153  0 return (Thunk<Set<T>>) (Thunk<?>) CopyOnWriteArraySetFactory.INSTANCE;
 154    }
 155   
 156    private static final class CopyOnWriteArraySetFactory<T> implements Thunk<Set<T>>, Serializable {
 157    public static final CopyOnWriteArraySetFactory<Object> INSTANCE = new CopyOnWriteArraySetFactory<Object>();
 158  0 private CopyOnWriteArraySetFactory() {}
 159  0 public Set<T> value() { return new CopyOnWriteArraySet<T>(); }
 160    }
 161   
 162    /**
 163    * Get a factory that produces HashMaps by invoking the empty HashMap constructor.
 164    * @see HashMap#HashMap()
 165    */
 166  3 @SuppressWarnings("unchecked") public static <K, V> Thunk<Map<K, V>> hashMapFactory() {
 167  3 return (Thunk<Map<K, V>>) (Thunk<?>) DefaultHashMapFactory.INSTANCE;
 168    }
 169   
 170    private static final class DefaultHashMapFactory<K, V> implements Thunk<Map<K, V>>, Serializable {
 171    public static final DefaultHashMapFactory<Object, Object> INSTANCE =
 172    new DefaultHashMapFactory<Object, Object>();
 173  1 private DefaultHashMapFactory() {}
 174  3 public Map<K, V> value() { return new HashMap<K, V>(); }
 175    }
 176   
 177    /**
 178    * Get a factory that produces HashMaps with the given initial capacity.
 179    * @see HashMap#HashMap(int)
 180    */
 181  0 public static <K, V> Thunk<Map<K, V>> hashMapFactory(int initialCapacity) {
 182  0 return new CustomHashMapFactory<K, V>(initialCapacity);
 183    }
 184   
 185    private static final class CustomHashMapFactory<K, V> implements Thunk<Map<K, V>>, Serializable {
 186    private final int _initialCapacity;
 187  0 public CustomHashMapFactory(int initialCapacity) { _initialCapacity = initialCapacity; }
 188  0 public Map<K, V> value() { return new HashMap<K, V>(_initialCapacity); }
 189    }
 190   
 191    /**
 192    * Get a factory that produces LinkedHashMaps by invoking the empty LinkedHashMap constructor.
 193    * @see LinkedHashMap#LinkedHashMap()
 194    */
 195  0 @SuppressWarnings("unchecked") public static <K, V> Thunk<Map<K, V>> linkedHashMapFactory() {
 196  0 return (Thunk<Map<K, V>>) (Thunk<?>) DefaultLinkedHashMapFactory.INSTANCE;
 197    }
 198   
 199    private static final class DefaultLinkedHashMapFactory<K, V> implements Thunk<Map<K, V>>, Serializable {
 200    public static final DefaultLinkedHashMapFactory<Object, Object> INSTANCE =
 201    new DefaultLinkedHashMapFactory<Object, Object>();
 202  0 private DefaultLinkedHashMapFactory() {}
 203  0 public Map<K, V> value() { return new LinkedHashMap<K, V>(); }
 204    }
 205   
 206    /**
 207    * Get a factory that produces LinkedHashMaps with the given initial capacity.
 208    * @see LinkedHashMap#LinkedHashMap(int)
 209    */
 210  0 public static <K, V> Thunk<Map<K, V>> linkedHashMapFactory(int initialCapacity) {
 211  0 return new CustomLinkedHashMapFactory<K, V>(initialCapacity);
 212    }
 213   
 214    private static final class CustomLinkedHashMapFactory<K, V> implements Thunk<Map<K, V>>, Serializable {
 215    private final int _initialCapacity;
 216  0 public CustomLinkedHashMapFactory(int initialCapacity) { _initialCapacity = initialCapacity; }
 217  0 public Map<K, V> value() { return new LinkedHashMap<K, V>(_initialCapacity); }
 218    }
 219   
 220    /**
 221    * Get a factory that produces TreeMaps sorted according to the elements' natural order.
 222    * @see TreeMap#TreeMap()
 223    */
 224  3 @SuppressWarnings("unchecked")
 225    public static <K extends Comparable<? super K>, V> Thunk<Map<K, V>> treeMapFactory() {
 226    // not sure why the weakening cast is necessary here but not elsewhere
 227  3 return (Thunk<Map<K, V>>) (Thunk<? extends Map<?, ?>>) DefaultTreeMapFactory.INSTANCE;
 228    }
 229   
 230    private static final class DefaultTreeMapFactory<K extends Comparable<? super K>, V>
 231    implements Thunk<Map<K, V>>, Serializable {
 232    public static final DefaultTreeMapFactory<String, Object> INSTANCE =
 233    new DefaultTreeMapFactory<String, Object>();
 234  1 private DefaultTreeMapFactory() {}
 235  3 public Map<K, V> value() { return new TreeMap<K, V>(); }
 236    }
 237   
 238    /**
 239    * Get a factory that produces TreeMaps sorted according to the given comparator.
 240    * @see TreeMap#TreeMap(Comparator)
 241    */
 242  0 public static <K, V> Thunk<Map<K, V>> treeMapFactory(Comparator<? super K> comparator) {
 243  0 return new CustomTreeMapFactory<K, V>(comparator);
 244    }
 245   
 246    private static final class CustomTreeMapFactory<K, V> implements Thunk<Map<K, V>>, Serializable {
 247    private final Comparator<? super K> _comp;
 248  0 public CustomTreeMapFactory(Comparator<? super K> comp) { _comp = comp; }
 249  0 public Map<K, V> value() { return new TreeMap<K, V>(_comp); }
 250    }
 251   
 252    /**
 253    * Get a factory that produces ArrayLists by invoking the empty ArrayList constructor.
 254    * @see ArrayList#ArrayList()
 255    */
 256  0 @SuppressWarnings("unchecked") public static <T> Thunk<List<T>> arrayListFactory() {
 257  0 return (Thunk<List<T>>) (Thunk<?>) DefaultArrayListFactory.INSTANCE;
 258    }
 259   
 260    private static final class DefaultArrayListFactory<T> implements Thunk<List<T>>, Serializable {
 261    public static final DefaultArrayListFactory<Object> INSTANCE = new DefaultArrayListFactory<Object>();
 262  0 private DefaultArrayListFactory() {}
 263  0 public List<T> value() { return new ArrayList<T>(); }
 264    }
 265   
 266    /**
 267    * Get a factory that produces ArrayLists with the given initial capacity.
 268    * @see ArrayList#ArrayList(int)
 269    */
 270  0 public static <T> Thunk<List<T>> arrayListFactory(int initialCapacity) {
 271  0 return new CustomArrayListFactory<T>(initialCapacity);
 272    }
 273   
 274    private static final class CustomArrayListFactory<T> implements Thunk<List<T>>, Serializable {
 275    private final int _initialCapacity;
 276  0 public CustomArrayListFactory(int initialCapacity) { _initialCapacity = initialCapacity; }
 277  0 public List<T> value() { return new ArrayList<T>(_initialCapacity); }
 278    }
 279   
 280    /**
 281    * Get a factory that produces LinkedLists by invoking the empty LinkedList constructor.
 282    * @see LinkedList#LinkedList()
 283    */
 284  0 @SuppressWarnings("unchecked") public static <T> Thunk<List<T>> linkedListFactory() {
 285  0 return (Thunk<List<T>>) (Thunk<?>) DefaultLinkedListFactory.INSTANCE;
 286    }
 287   
 288    private static final class DefaultLinkedListFactory<T> implements Thunk<List<T>>, Serializable {
 289    public static final DefaultLinkedListFactory<Object> INSTANCE = new DefaultLinkedListFactory<Object>();
 290  0 private DefaultLinkedListFactory() {}
 291  0 public List<T> value() { return new LinkedList<T>(); }
 292    }
 293   
 294    /**
 295    * Get a factory that produces CopyOnWriteArraySets by invoking the empty CopyOnWriteArraySet constructor.
 296    * @see CopyOnWriteArraySet#CopyOnWriteArraySet()
 297    */
 298  0 @SuppressWarnings("unchecked") public static <T> Thunk<List<T>> copyOnWriteArrayListFactory() {
 299  0 return (Thunk<List<T>>) (Thunk<?>) CopyOnWriteArrayListFactory.INSTANCE;
 300    }
 301   
 302    private static final class CopyOnWriteArrayListFactory<T> implements Thunk<List<T>>, Serializable {
 303    public static final CopyOnWriteArrayListFactory<Object> INSTANCE = new CopyOnWriteArrayListFactory<Object>();
 304  0 private CopyOnWriteArrayListFactory() {}
 305  0 public List<T> value() { return new CopyOnWriteArrayList<T>(); }
 306    }
 307   
 308    /**
 309    * Create an immutable {@code PredicateSet} based on the given elements. May depend on a valid
 310    * {@code hashCode()} implementation.
 311    */
 312  158 public static <T> PredicateSet<T> makeSet(T... elements) {
 313  158 return makeSet(IterUtil.asIterable(elements));
 314    }
 315   
 316    /**
 317    * Create an immutable {@code PredicateSet} based on the given elements. May depend on a valid
 318    * {@code hashCode()} implementation.
 319    */
 320  158 public static <T> PredicateSet<T> makeSet(Iterable<? extends T> elements) {
 321  158 if (IterUtil.isEmpty(elements)) {
 322  0 return EmptySet.make();
 323    }
 324  158 else if (IterUtil.sizeOf(elements, 2) == 1) {
 325  84 return new SingletonSet<T>(IterUtil.first(elements));
 326    }
 327    else {
 328  74 Set<T> result = new LinkedHashSet<T>(asCollection(elements));
 329  74 return new ImmutableSet<T>(result) {
 330  0 @Override public boolean hasFixedSize() { return true; }
 331  0 @Override public boolean isStatic() { return true; }
 332    };
 333    }
 334    }
 335   
 336    /** Produce an empty or singleton set based on the given Option. */
 337  0 public static <T> PredicateSet<T> makeSet(Option<? extends T> opt) {
 338  0 if (opt.isSome()) { return new SingletonSet<T>(opt.unwrap()); }
 339  0 else { return EmptySet.make(); }
 340    }
 341   
 342    /**
 343    * Create an immutable {@code Relation} based on the given elements. May depend on a valid
 344    * {@code hashCode()} implementation.
 345    */
 346  0 public static <T1, T2> Relation<T1, T2> makeRelation(Iterable<? extends Pair<? extends T1, ? extends T2>> pairs) {
 347  0 if (IterUtil.isEmpty(pairs)) {
 348  0 return EmptyRelation.make();
 349    }
 350  0 else if (IterUtil.sizeOf(pairs, 2) == 1) {
 351  0 Pair<? extends T1, ? extends T2> elt = IterUtil.first(pairs);
 352  0 return new SingletonRelation<T1, T2>(elt.first(), elt.second());
 353    }
 354    else {
 355  0 Relation<T1, T2> result = IndexedRelation.makeLinkedHashBased();
 356  0 for (Pair<? extends T1, ? extends T2> elt : pairs) {
 357  0 result.add(elt.first(), elt.second());
 358    }
 359  0 return new ImmutableRelation<T1, T2>(result) {
 360  0 @Override public boolean hasFixedSize() { return true; }
 361  0 @Override public boolean isStatic() { return true; }
 362    };
 363    }
 364    }
 365   
 366    /** Make an {@code ArrayList} with the given elements. */
 367  0 public static <T> List<T> makeList(Iterable<? extends T> iter) {
 368  0 return makeArrayList(iter);
 369    }
 370   
 371    /** Make an {@code ArrayList} with the given elements. */
 372  0 public static <T> ArrayList<T> makeArrayList(Iterable<? extends T> iter) {
 373  0 if (iter instanceof Collection<?>) {
 374  0 @SuppressWarnings("unchecked") // should be legal, but javac 6 doesn't like it
 375    Collection<? extends T> cast = (Collection<? extends T>) iter;
 376  0 return new ArrayList<T>(cast);
 377    }
 378  0 else if (iter instanceof SizedIterable<?>) {
 379  0 ArrayList<T> result = new ArrayList<T>(((SizedIterable<?>) iter).size());
 380  0 for (T e : iter) { result.add(e); }
 381  0 return result;
 382    }
 383    else {
 384  0 ArrayList<T> result = new ArrayList<T>();
 385  0 for (T e : iter) { result.add(e); }
 386  0 return result;
 387    }
 388    }
 389   
 390    /** Make a {@code LinkedList} with the given elements. */
 391  10 public static <T> LinkedList<T> makeLinkedList(Iterable<? extends T> iter) {
 392  10 if (iter instanceof Collection<?>) {
 393  10 @SuppressWarnings("unchecked") // should be legal, but javac 6 doesn't like it
 394    Collection<? extends T> cast = (Collection<? extends T>) iter;
 395  10 return new LinkedList<T>(cast);
 396    }
 397    else {
 398  0 LinkedList<T> result = new LinkedList<T>();
 399  0 for (T e : iter) { result.add(e); }
 400  0 return result;
 401    }
 402    }
 403   
 404    /** Make a {@link ConsList} with the given elements. */
 405  0 public static <T> ConsList<T> makeConsList(Iterable<? extends T> iter) {
 406  0 ConsList<T> result = ConsList.empty();
 407  0 for (T elt : IterUtil.reverse(iter)) { result = ConsList.cons(elt, result); }
 408  0 return result;
 409    }
 410   
 411    /**
 412    * Produce an immutable empty list. Equivalent to {@link Collections#emptyList}; defined here for
 413    * Java 1.4 compatibility.
 414    */
 415  0 @SuppressWarnings("unchecked") public static <T> List<T> emptyList() {
 416  0 return (List<T>) Collections.EMPTY_LIST;
 417    }
 418   
 419    /**
 420    * Produce an immutable empty set. Similar to {@link Collections#emptySet}, but the result here is
 421    * a {@link PredicateSet}. Also defined for Java 1.4 compatibility.
 422    */
 423  2 @SuppressWarnings("unchecked") public static <T> EmptySet<T> emptySet() {
 424  2 return (EmptySet<T>) EmptySet.INSTANCE;
 425    }
 426   
 427    /**
 428    * Produce an immutable empty map. Similar to {@link Collections#emptyMap}, but the result here is
 429    * a {@link LambdaMap}. Also defined for Java 1.4 compatibility.
 430    */
 431  0 @SuppressWarnings("unchecked") public static <K, V> EmptyMap<K, V> emptyMap() {
 432  0 return (EmptyMap<K, V>) EmptyMap.INSTANCE;
 433    }
 434   
 435    /** Produce an immutable empty relation. */
 436  0 @SuppressWarnings("unchecked") public static <T1, T2> EmptyRelation<T1, T2> emptyRelation() {
 437  0 return (EmptyRelation<T1, T2>) EmptyRelation.INSTANCE;
 438    }
 439   
 440    /** Create an immutable singleton set. Similar to {@link Collections#singleton}, but produces a PredicateSet. */
 441  0 public static <T> SingletonSet<T> singleton(T elt) {
 442  0 return new SingletonSet<T>(elt);
 443    }
 444   
 445    /** Create an immutable singleton relation. */
 446  0 public static <T1, T2> SingletonRelation<T1, T2> singleton(T1 first, T2 second) {
 447  0 return new SingletonRelation<T1, T2>(first, second);
 448    }
 449   
 450    /** Create an immutable singleton map. Similar to {@link Collections#singletonMap}, but produces a LambdaMap. */
 451  0 public static <K, V> SingletonMap<K, V> singletonMap(K key, V value) {
 452  0 return new SingletonMap<K, V>(key, value);
 453    }
 454   
 455    /**
 456    * Convert the given {@code Iterable} to a {@code Set}. If it already <em>is</em>
 457    * a {@code Set}, cast it as such. Otherwise, create an {@link IterableSet}.
 458    */
 459  0 public static <T> Set<T> asSet(Iterable<T> iter) {
 460  0 if (iter instanceof Set<?>) { return (Set<T>) iter; }
 461  0 else { return new IterableSet<T>(iter); }
 462    }
 463   
 464    /**
 465    * Convert the given {@code Iterable} to a {@code PredicateSet}. If it already <em>is</em>
 466    * a {@code PredicateSet}, cast it as such. If it is a {@code Set}, produce a {@link DelegatingSet}.
 467    * Otherwise, create an {@link IterableSet}.
 468    */
 469  48 public static <T> PredicateSet<T> asPredicateSet(Iterable<T> iter) {
 470  0 if (iter instanceof PredicateSet<?>) { return (PredicateSet<T>) iter; }
 471  48 else if (iter instanceof Set<?>) { return new DelegatingSet<T>((Set<T>) iter); }
 472  0 else { return new IterableSet<T>(iter); }
 473    }
 474   
 475    /**
 476    * Convert the given {@code Iterable} to a {@code Collection}. If it already <em>is</em>
 477    * a {@code Collection}, cast it as such. Otherwise, create an {@link IterableCollection}.
 478    */
 479  103 public static <T> Collection<T> asCollection(Iterable<T> iter) {
 480  29 if (iter instanceof Collection<?>) { return (Collection<T>) iter; }
 481  74 else { return new IterableCollection<T>(iter); }
 482    }
 483   
 484    /**
 485    * Convert the given {@code Map} to a {@code LambdaMap}. If it already <em>is</em>
 486    * a {@code LambdaMap}, cast it as such. Otherwise, create a {@link DelegatingMap}.
 487    */
 488  0 public static <K, V> LambdaMap<K, V> asLambdaMap(Map<K, V> m) {
 489  0 if (m instanceof LambdaMap<?, ?>) { return (LambdaMap<K, V>) m; }
 490  0 else { return new DelegatingMap<K, V>(m); }
 491    }
 492   
 493    /**
 494    * Convert a Dictionary to a Map. If it is a {@link Hashtable}, cast it as a Map.
 495    * Otherwise, create a {@link DictionaryMap}.
 496    */
 497  0 public static <K, V> Map<K, V> asMap(Dictionary<K, V> d) {
 498    // can't cast arbitrary Dictionaries because the dictionary type parameters may
 499    // be unrelated to map parameters -- it might be a Dictionary<K, V> and a Map<K, Foo>
 500  0 if (d instanceof Hashtable<?, ?>) { return (Hashtable<K, V>) d; }
 501  0 return new DictionaryMap<K, V>(d);
 502    }
 503   
 504    /**
 505    * Wrap a set in an immutable wrapper. Similar to {@link Collections#unmodifiableSet},
 506    * but produces a {@code PredicateSet}.
 507    */
 508  0 public static <T> PredicateSet<T> immutable(Set<? extends T> set) {
 509  0 return new ImmutableSet<T>(set);
 510    }
 511   
 512    /**
 513    * Wrap a map in an immutable wrapper. Similar to {@link Collections#unmodifiableMap},
 514    * but produces a {@code LambdaMap}.
 515    */
 516  0 public static <K, V> Map<K, V> immutable(Map<? extends K, ? extends V> map) {
 517  0 return new ImmutableMap<K, V>(map);
 518    }
 519   
 520    /** Wrap a relation in an immutable wrapper. Analogous to {@link Collections#unmodifiableSet}. */
 521  0 public static <T1, T2> ImmutableRelation<T1, T2> immutable(Relation<T1, T2> r) {
 522  0 return new ImmutableRelation<T1, T2>(r);
 523    }
 524   
 525    /** Alias for {@link #makeSet}. */
 526  0 public static <T> PredicateSet<T> snapshot(Set<? extends T> set) {
 527  0 return makeSet(set);
 528    }
 529   
 530    /**
 531    * Produce a snapshot of {@code set} if its composite size is greater than the given threshold.
 532    * @see ObjectUtil#compositeSize
 533    */
 534  0 public static <T> Iterable<T> conditionalSnapshot(Set<T> set, int threshold) {
 535  0 if (ObjectUtil.compositeSize(set) > threshold) { return makeSet(set); }
 536  0 else { return set; }
 537    }
 538   
 539    /** Alias for {@link #makeRelation}. */
 540  0 public static <T1, T2> Relation<T1, T2> snapshot(Relation<? extends T1, ? extends T2> relation) {
 541  0 return makeRelation(relation);
 542    }
 543   
 544    /**
 545    * Produce a snapshot of {@code set} if its composite size is greater than the given threshold.
 546    * @see ObjectUtil#compositeSize
 547    */
 548  0 public static <T1, T2> Relation<T1, T2> conditionalSnapshot(Relation<T1, T2> rel, int threshold) {
 549  0 if (ObjectUtil.compositeSize(rel) > threshold) { return makeRelation(rel); }
 550  0 else { return rel; }
 551    }
 552   
 553    /** Invoke the {@code HashMap#HashMap(Map)} constructor, and wrap the result as a LambdaMap. */
 554  24 public static <K, V> LambdaMap<K, V> snapshot(Map<? extends K, ? extends V> map) {
 555  24 return new DelegatingMap<K, V>(new HashMap<K, V>(map));
 556    }
 557   
 558    /**
 559    * Produce a snapshot of {@code set} if its composite size is greater than the given threshold.
 560    * @see ObjectUtil#compositeSize
 561    */
 562  0 public static <K, V> Map<K, V> conditionalSnapshot(Map<K, V> map, int threshold) {
 563  0 if (ObjectUtil.compositeSize(map) > threshold) { return snapshot(map); }
 564  0 else { return map; }
 565    }
 566   
 567    /** Alias for {@link #makeArrayList}. */
 568  0 public static <T> List<T> snapshot(List<? extends T> list) {
 569  0 return makeArrayList(list);
 570    }
 571   
 572    /**
 573    * Wrap {@code s} as a thread-safe set that produces snapshots to support concurrent iteration.
 574    * @see SnapshotSynchronizedSet
 575    */
 576  0 public static <T> SnapshotSynchronizedSet<T> snapshotSynchronized(Set<T> s) {
 577  0 return new SnapshotSynchronizedSet<T>(s);
 578    }
 579   
 580    /**
 581    * Wrap {@code l} as a thread-safe list that produces snapshots to support concurrent iteration.
 582    * @see SnapshotSynchronizedList
 583    */
 584  0 public static <T> SnapshotSynchronizedList<T> snapshotSynchronized(List<T> l) {
 585  0 return new SnapshotSynchronizedList<T>(l);
 586    }
 587   
 588    /** Produce a lazy union of two sets. Size-related operations have poor performance. */
 589  0 public static <T> PredicateSet<T> union(Set<? extends T> s1, Set<? extends T> s2) {
 590  0 return new UnionSet<T>(s1, s2);
 591    }
 592   
 593    /** Produce a lazy union of a set with an additional singleton element. */
 594  0 public static <T> PredicateSet<T> union(Set<? extends T> set, T elt) {
 595  0 return new UnionSet<T>(set, new SingletonSet<T>(elt));
 596    }
 597   
 598    /** Produce a lazy intersection of two sets. Size-related operations have poor performance. */
 599  0 public static <T> PredicateSet<T> intersection(Set<?> s1, Set<? extends T> s2) {
 600  0 return new IntersectionSet<T>(s1, s2);
 601    }
 602   
 603    /**
 604    * Produce the complement of a set in a domain, or, equivalently, the difference of two sets.
 605    * Size-related operations have poor performance.
 606    */
 607  0 public static <T> PredicateSet<T> complement(Set<? extends T> domain, Set<?> excluded) {
 608  0 return new ComplementSet<T>(domain, excluded);
 609    }
 610   
 611    /**
 612    * Produce the complement of a singleton in a domain set, or, equivalently, a set with
 613    * a certain element removed.
 614    */
 615  0 public static <T> PredicateSet<T> complement(Set<? extends T> domain, T excluded) {
 616  0 return new ComplementSet<T>(domain, new SingletonSet<T>(excluded));
 617    }
 618   
 619    /** Lazily filter the given set. Size-related operations have poor performance. */
 620  0 public static <T> PredicateSet<T> filter(Set<? extends T> set, Predicate<? super T> predicate) {
 621  0 return new FilteredSet<T>(set, predicate);
 622    }
 623   
 624    /** Produce the lazy cartesian (or cross) product of two sets. */
 625  0 public static <T1, T2> Relation<T1, T2> cross(Set<? extends T1> left, Set<? extends T2> right) {
 626  0 return new CartesianRelation<T1, T2>(left, right);
 627    }
 628   
 629    /** Produce a lazy union of two relations. Size-related operations have poor performance. */
 630  0 public static <T1, T2> Relation<T1, T2> union(Relation<T1, T2> r1, Relation<T1, T2> r2) {
 631  0 return new UnionRelation<T1, T2>(r1, r2);
 632    }
 633   
 634    /** Produce a lazy union of a relation with an additional singleton entry. */
 635  0 public static <T1, T2> Relation<T1, T2> union(Relation<T1, T2> rel, T1 first, T2 second) {
 636  0 return new UnionRelation<T1, T2>(rel, new SingletonRelation<T1, T2>(first, second));
 637    }
 638   
 639    /** Produce a lazy intersection of two relations. Size-related operations have poor performance. */
 640  0 public static <T1, T2> Relation<T1, T2> intersection(Relation<T1, T2> r1, Relation<T1, T2> r2) {
 641  0 return new IntersectionRelation<T1, T2>(r1, r2);
 642    }
 643   
 644    /**
 645    * Produce the complement of a relation in a domain, or, equivalently, the difference of two
 646    * relations. Size-related operations have poor performance.
 647    */
 648  0 public static <T1, T2> Relation<T1, T2> complement(Relation<T1, T2> domain,
 649    Relation<? super T1, ? super T2> excluded) {
 650  0 return new ComplementRelation<T1, T2>(domain, excluded);
 651    }
 652   
 653    /**
 654    * Produce the complement of a singleton in a domain relation, or, equivalently, a relation with
 655    * a certain entry removed.
 656    */
 657  0 public static <T1, T2> Relation<T1, T2> complement(Relation<T1, T2> domain, T1 first, T2 second) {
 658  0 return new ComplementRelation<T1, T2>(domain, new SingletonRelation<T1, T2>(first, second));
 659    }
 660   
 661    /** Produce a lazy transitive composition of two relations. Size-related operations have poor performance. */
 662  0 public static <T1, T2, T3> Relation<T1, T3> compose(Relation<T1, T2> left, Relation<T2, T3> right) {
 663  0 return new ComposedRelation<T1, T2, T3>(left, right);
 664    }
 665   
 666    /** Lazily filter the given relation. Size-related operations have poor performance. */
 667  0 public static <T1, T2> Relation<T1, T2> filter(Relation<T1, T2> relation,
 668    Predicate2<? super T1, ? super T2> pred) {
 669  0 return new FilteredRelation<T1, T2>(relation, pred);
 670    }
 671   
 672    /**
 673    * Produce a lazy union of two maps, with mappings in {@code child} shadowing those in {@code parent}.
 674    * Size-related operations have poor performance.
 675    */
 676  0 public static <K, V> LambdaMap<K, V> union(Map<? extends K, ? extends V> parent,
 677    Map<? extends K, ? extends V> child) {
 678  0 return new UnionMap<K, V>(parent, child);
 679    }
 680   
 681    /** Produce a lazy transitive composition of two maps. Size-related operations have poor performance. */
 682  0 public static <K, X, V> LambdaMap<K, V> compose(Map<? extends K, ? extends X> left,
 683    Map<? super X, ? extends V> right) {
 684  0 return new ComposedMap<K, X, V>(left, right);
 685    }
 686   
 687    /**
 688    * Cast the given object to a collection element type if that object is contained by the collection.
 689    * Assumes the collection is faithful to its specification: an object is contained by the collection
 690    * if and only if it appears as an element of the {@code Iterator<T>} produced by its {@code iterator()}
 691    * method (the implication being that the object must have type {@code T}).
 692    */
 693  578 @SuppressWarnings("unchecked")
 694    public static <T> Option<T> castIfContains(Collection<? extends T> c, Object obj) {
 695  578 if (c.contains(obj)) { return Option.some((T) obj); }
 696  0 else { return Option.none(); }
 697    }
 698   
 699    /**
 700    * Test whether a collection contains some element of a list.
 701    * @see Collection#containsAll
 702    * @see IterUtil#or
 703    */
 704  0 public static boolean containsAny(Collection<?> c, Iterable<?> candidates) {
 705  0 for (Object o : candidates) {
 706  0 if (c.contains(o)) { return true; }
 707    }
 708  0 return false;
 709    }
 710   
 711    /**
 712    * Test whether a collection contains all the elements of a list. Unlike {@link Collection#containsAll},
 713    * defined for arbitrary {@code Iterable}s. When possible, delegates to {@code c.containsAll()}.
 714    * @see IterUtil#and
 715    * @see IterUtil#containsAll
 716    */
 717  5 public static boolean containsAll(Collection<?> c, Iterable<?> subset) {
 718  5 if (subset instanceof Collection<?>) {
 719  5 return c.containsAll((Collection<?>) subset);
 720    }
 721    else {
 722  0 for (Object o : subset) {
 723  0 if (!c.contains(o)) { return false; }
 724    }
 725  0 return true;
 726    }
 727    }
 728   
 729    /**
 730    * Add the given elements to a collection. Unlike {@link Collection#addAll}, defined for arbitrary
 731    * {@code Iterable}s. When possible, delegates to {@code c.addAll()}.
 732    * @return {@code true} if {@code c} changed as a result of the call
 733    */
 734  22 public static <E> boolean addAll(Collection<E> c, Iterable<? extends E> elts) {
 735  22 if (elts instanceof Collection<?>) {
 736  0 @SuppressWarnings("unchecked") // should be legal, but javac 6 doesn't like it
 737    Collection<? extends E> eltsColl = (Collection<? extends E>) elts;
 738  0 return c.addAll(eltsColl);
 739    }
 740    else {
 741  22 boolean result = false;
 742  5 for (E elt : elts) { result |= c.add(elt); }
 743  22 return result;
 744    }
 745    }
 746   
 747    /**
 748    * Remove the given elements from a collection. Unlike {@link Collection#removeAll}, defined for arbitrary
 749    * {@code Iterable}s. When possible, delegates to {@code c.removeAll()}.
 750    * @return {@code true} if {@code c} changed as a result of the call
 751    */
 752  0 public static boolean removeAll(Collection<?> c, Iterable<?> elts) {
 753  0 if (elts instanceof Collection<?>) {
 754  0 return c.removeAll((Collection<?>) elts);
 755    }
 756    else {
 757  0 boolean result = false;
 758  0 for (Object elt : elts) { result |= c.remove(elt); }
 759  0 return result;
 760    }
 761    }
 762   
 763    /**
 764    * Remove all but the given elements from a collection. Unlike {@link Collection#retainAll}, defined for
 765    * arbitrary {@code Iterable}s. When possible, delegates to {@code c.retainAll()}.
 766    * @return {@code true} if {@code c} changed as a result of the call
 767    */
 768  0 public static boolean retainAll(Collection<?> c, Iterable<?> elts) {
 769  0 if (elts instanceof Collection<?>) {
 770  0 return c.retainAll((Collection<?>) elts);
 771    }
 772  0 else { return c.retainAll(makeSet(elts)); }
 773    }
 774   
 775    /**
 776    * Produce the set containing {@code base} and all values produced an arbitrary number of applications
 777    * of {@code function} to {@code base}.
 778    */
 779  0 public static <T> Set<T> functionClosure(T base, Lambda<? super T, ? extends T> function) {
 780  0 return functionClosure(Collections.singleton(base), function);
 781    }
 782   
 783    /**
 784    * Produce the set containing the elements in {@code base} and all values produced an arbitrary number
 785    * of applications of {@code function} to one of these elements.
 786    */
 787  0 public static <T> Set<T> functionClosure(Set<? extends T> base, final Lambda<? super T, ? extends T> function) {
 788  0 Lambda<T, Set<T>> neighbors = new Lambda<T, Set<T>>() {
 789  0 public Set<T> value(T node) { return Collections.<T>singleton(function.value(node)); }
 790    };
 791  0 return graphClosure(base, neighbors);
 792    }
 793   
 794    /**
 795    * Produce the set containing {@code base} and all values produced an arbitrary number of applications
 796    * of {@code function} to {@code base}.
 797    */
 798  0 public static <T> Set<T> partialFunctionClosure(T base, Lambda<? super T, ? extends Option<? extends T>> function) {
 799  0 return partialFunctionClosure(Collections.singleton(base), function);
 800    }
 801   
 802    /**
 803    * Produce the set containing the elements in {@code base} and all values produced an arbitrary number
 804    * of applications of {@code function} to one of these elements.
 805    */
 806  0 public static <T> Set<T> partialFunctionClosure(Set<? extends T> base,
 807    final Lambda<? super T, ? extends Option<? extends T>> function) {
 808  0 Lambda<T, Set<T>> neighbors = new Lambda<T, Set<T>>() {
 809  0 public Set<T> value(T node) { return makeSet(function.value(node)); }
 810    };
 811  0 return graphClosure(base, neighbors);
 812    }
 813   
 814    /** Produce the set of all nodes reachable from {@code base} in a directed graph defined by {@code neighbors}. */
 815  0 public static <T> Set<T> graphClosure(T base, Lambda<? super T, ? extends Iterable<? extends T>> neighbors) {
 816  0 return graphClosure(Collections.singleton(base), neighbors);
 817    }
 818   
 819    /**
 820    * Produce the set of all nodes reachable from the elements in {@code base} in a directed graph defined by
 821    * {@code neighbors}.
 822    */
 823  0 public static <T> Set<T> graphClosure(Set<? extends T> base,
 824    Lambda<? super T, ? extends Iterable<? extends T>> neighbors) {
 825  0 Set<T> result = new LinkedHashSet<T>(base);
 826  0 LinkedList<T> workList = new LinkedList<T>(base); // can't iterate over result because it mutates
 827  0 while (!workList.isEmpty()) {
 828  0 for (T newElt : neighbors.value(workList.removeFirst())) {
 829  0 if (!result.contains(newElt)) {
 830  0 result.add(newElt);
 831  0 workList.addLast(newElt);
 832    }
 833    }
 834    }
 835  0 return result;
 836    }
 837   
 838    /**
 839    * Get the maximal elements of the given list, based on the given order. All elements in
 840    * {@code vals} either appear in the result or precede some element in the result. Where
 841    * two elements are equivalent (each precedes the other), the second will always be
 842    * discarded.
 843    */
 844  14 public static <T> List<T> maxList(Iterable<? extends T> vals, Order<? super T> order) {
 845  14 switch (IterUtil.sizeOf(vals, 2)) {
 846  2 case 0: return Collections.emptyList();
 847  2 case 1: return Collections.singletonList(IterUtil.first(vals));
 848  10 default:
 849  10 LinkedList<? extends T> workList = makeLinkedList(vals);
 850  10 LinkedList<T> result = new LinkedList<T>();
 851  10 Iterable<T> remainingTs = IterUtil.compose(workList, result);
 852  10 while (!workList.isEmpty()) {
 853    // prefer discarding later elements when two are equivalent
 854  34 T t = workList.removeLast();
 855  34 boolean discard = IterUtil.or(remainingTs, LambdaUtil.bindFirst(order, t));
 856  16 if (!discard) { result.addFirst(t); }
 857    }
 858  10 return result;
 859    }
 860    }
 861   
 862    /**
 863    * Get the maximal elements of the given lists, each known to be a list of maximal elements, based
 864    * on the given order. The result is the same as {@code maxList(IterUtil.compose(vals1, vals2), order)},
 865    * but the implementation is more efficient, because it can avoid performing redundant comparisons.
 866    */
 867  18 public static <T> List<T> composeMaxLists(Iterable<? extends T> vals1, Iterable<? extends T> vals2,
 868    Order<? super T> order) {
 869  18 List<T> results2 = new LinkedList<T>();
 870  18 for (T t : vals2) {
 871    // does t precede anything in vals1?
 872  26 boolean discard = IterUtil.or(vals1, LambdaUtil.bindFirst(order, t));
 873  16 if (!discard) { results2.add(t); }
 874    }
 875  18 List<T> results1 = new LinkedList<T>();
 876  18 for (T t : vals1) {
 877    // does t precede anything in (what's left of) vals2?
 878  26 boolean discard = IterUtil.or(results2, LambdaUtil.bindFirst(order, t));
 879  24 if (!discard) { results1.add(t); }
 880    }
 881  18 results1.addAll(results2);
 882  18 return results1;
 883    }
 884   
 885    /**
 886    * Get the minimal elements of the given list, based on the given order. All elements in
 887    * {@code vals} either appear in the result or are preceded by some element in the result.
 888    * Where two elements are equivalent (each precedes the other), the second will always be
 889    * discarded.
 890    */
 891  7 public static <T> List<T> minList(Iterable<? extends T> vals, Order<? super T> order) {
 892  7 return maxList(vals, inverse(order));
 893    }
 894   
 895    /**
 896    * Get the minimal elements of the given lists, each known to be a list of minimal elements, based
 897    * on the given order. The result is the same as {@code minList(IterUtil.compose(vals1, vals2), order)},
 898    * but the implementation is more efficient, because it can avoid performing redundant comparisons.
 899    */
 900  9 public static <T> List<T> composeMinLists(Iterable<? extends T> vals1, Iterable<? extends T> vals2,
 901    Order<? super T> order) {
 902  9 return composeMaxLists(vals1, vals2, inverse(order));
 903    }
 904   
 905    /** Get a TotalOrder based on the natural (compareTo-based) order associated with the given type. */
 906  2 @SuppressWarnings("unchecked") public static <T extends Comparable<? super T>> TotalOrder<T> naturalOrder() {
 907  2 return (TotalOrder<T>) NaturalOrder.INSTANCE;
 908    }
 909   
 910    private static final class NaturalOrder<T extends Comparable<? super T>>
 911    extends TotalOrder<T> implements Serializable {
 912    private static final NaturalOrder<Comparable<Object>> INSTANCE = new NaturalOrder<Comparable<Object>>();
 913  1 private NaturalOrder() {}
 914  56 public int compare(T arg1, T arg2) { return arg1.compareTo(arg2); }
 915    }
 916   
 917    /** Wrap a Comparator as a TotalOrder. */
 918  0 public static <T> TotalOrder<T> asTotalOrder(Comparator<? super T> comp) {
 919  0 return new ComparatorTotalOrder<T>(comp);
 920    }
 921   
 922    private static final class ComparatorTotalOrder<T> extends TotalOrder<T> {
 923    private final Comparator<? super T> _comp;
 924  0 public ComparatorTotalOrder(Comparator<? super T> comp) { _comp = comp; }
 925  0 public int compare(T arg1, T arg2) { return _comp.compare(arg1, arg2); }
 926  0 public boolean equals(Object o) {
 927  0 if (this == o) { return true; }
 928  0 else if (!(o instanceof ComparatorTotalOrder<?>)) { return false; }
 929  0 else { return _comp.equals(((ComparatorTotalOrder<?>) o)._comp); }
 930    }
 931  0 public int hashCode() { return ObjectUtil.hash(ComparatorTotalOrder.class, _comp); }
 932    }
 933   
 934    /** Create an inverse of the given order. */
 935  16 public static <T> Order<T> inverse(Order<? super T> ord) {
 936  16 return new InverseOrder<T>(ord);
 937    }
 938   
 939    private static final class InverseOrder<T> implements Order<T> {
 940    private final Order<? super T> _ord;
 941  16 public InverseOrder(Order<? super T> ord) { _ord = ord; }
 942  46 public boolean contains(T arg1, T arg2) { return _ord.contains(arg2, arg1); }
 943  0 public boolean equals(Object o) {
 944  0 if (this == o) { return true; }
 945  0 else if (!(o instanceof InverseOrder<?>)) { return false; }
 946  0 else { return _ord.equals(((InverseOrder<?>) o)._ord); }
 947    }
 948  0 public int hashCode() { return ObjectUtil.hash(InverseOrder.class, _ord); }
 949    }
 950   
 951    /** Create an inverse of the given comparator (or TotalOrder). */
 952  0 public static <T> TotalOrder<T> inverse(Comparator<? super T> ord) {
 953  0 return new InverseTotalOrder<T>(ord);
 954    }
 955   
 956    private static final class InverseTotalOrder<T> extends TotalOrder<T> {
 957    private final Comparator<? super T> _ord;
 958  0 public InverseTotalOrder(Comparator<? super T> ord) { _ord = ord; }
 959  0 public int compare(T arg1, T arg2) { return _ord.compare(arg2, arg1); }
 960  0 public boolean equals(Object o) {
 961  0 if (this == o) { return true; }
 962  0 else if (!(o instanceof InverseTotalOrder<?>)) { return false; }
 963  0 else { return _ord.equals(((InverseTotalOrder<?>) o)._ord); }
 964    }
 965  0 public int hashCode() { return ObjectUtil.hash(InverseTotalOrder.class, _ord); }
 966    }
 967   
 968    /**
 969    * A partial order checking whether the first element is a subset of the second; implemented with
 970    * {@link IterUtil#containsAll} and {@link Collection#containsAll}.
 971    */
 972    public static final Order<Iterable<?>> SUBSET_ORDER = new SubsetOrder();
 973   
 974    private static final class SubsetOrder implements Order<Iterable<?>>, Serializable {
 975  16 private SubsetOrder() {}
 976  5 public boolean contains(Iterable<?> sub, Iterable<?> sup) { return IterUtil.containsAll(sup, sub); }
 977    }
 978   
 979    /**
 980    * A partial order checking whether the first element is a substring of the second; implemented with
 981    * {@link String#contains}.
 982    */
 983    public static final Order<String> SUBSTRING_ORDER = new SubstringOrder();
 984   
 985    private static final class SubstringOrder implements Order<String>, Serializable {
 986  16 private SubstringOrder() {}
 987  9 public boolean contains(String sub, String sup) { return sup.contains(sub); }
 988    }
 989   
 990    /**
 991    * A partial order checking whether the first element is a prefix of the second; implemented with
 992    * {@link String#contains}.
 993    */
 994    public static final Order<String> STRING_PREFIX_ORDER = new StringPrefixOrder();
 995   
 996    private static final class StringPrefixOrder implements Order<String>, Serializable {
 997  16 private StringPrefixOrder() {}
 998  101 public boolean contains(String pre, String s) { return s.startsWith(pre); }
 999    }
 1000   
 1001    }