View Javadoc

1   /*
2   
3       dsh-collect  Collection and map utility classes.
4       Copyright (c) 2008-2013 held jointly by the individual authors.
5   
6       This library is free software; you can redistribute it and/or modify it
7       under the terms of the GNU Lesser General Public License as published
8       by the Free Software Foundation; either version 3 of the License, or (at
9       your option) any later version.
10  
11      This library is distributed in the hope that it will be useful, but WITHOUT
12      ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or
13      FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14      License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public License
17      along with this library;  if not, write to the Free Software Foundation,
18      Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA.
19  
20      > http://www.fsf.org/licensing/licenses/lgpl.html
21      > http://www.opensource.org/licenses/lgpl-license.php
22  
23  */
24  package org.dishevelled.collect;
25  
26  import java.util.Collection;
27  import java.util.Collections;
28  import java.util.Comparator;
29  import java.util.HashSet;
30  import java.util.Iterator;
31  import java.util.NavigableSet;
32  import java.util.Set;
33  import java.util.SortedSet;
34  import java.util.TreeSet;
35  
36  import java.util.concurrent.ConcurrentSkipListSet;
37  import java.util.concurrent.CopyOnWriteArraySet;
38  
39  import com.google.common.collect.ImmutableSet;
40  import com.google.common.collect.ImmutableSortedSet;
41  
42  import org.cliffc.high_scale_lib.NonBlockingHashSet;
43  
44  /**
45   * Static utility methods for Sets.
46   *
47   * @author  Michael Heuer
48   * @version $Revision$ $Date$
49   */
50  public final class Sets
51  {
52  
53      /**
54       * Private no-arg constructor.
55       */
56      private Sets()
57      {
58          // empty
59      }
60  
61  
62      /**
63       * Create and return a new empty set.  Delegates to <code>Collections.emptySet()</code>.
64       *
65       * @param <T> element type
66       * @return a new empty set
67       */
68      public static <T> Set<T> emptySet()
69      {
70          return Collections.<T>emptySet();
71      }
72  
73      //
74      // asXxx methods, which accept elements as parameters
75  
76      /**
77       * Create and return a set containing the unique elements in <code>elements</code>.
78       *
79       * @param <T> element type
80       * @param elements variable number of elements to be added to the
81       *    returned set
82       * @return a set containing the unique elements in <code>elements</code>
83       */
84      public static <T> Set<T> asSet(final T... elements)
85      {
86          // TODO:  check null?
87          Set<T> set = createSet(elements.length);
88          for (T t : elements)
89          {
90              set.add(t);
91          }
92          return set;
93      }
94  
95      /**
96       * Create and return a set containing the unique elements returned by the
97       * specified iterator.
98       *
99       * @param <T> element type
100      * @param iterator iterator, must not be null
101      * @return a set containing the unique elements returned by the specified iterator
102      */
103     public static <T> Set<T> asSet(final Iterator<? extends T> iterator)
104     {
105         if (iterator == null)
106         {
107             throw new IllegalArgumentException("iterator must not be null");
108         }
109         Set<T> set = createSet();
110         while (iterator.hasNext())
111         {
112             set.add(iterator.next());
113         }
114         return set;
115     }
116 
117    /**
118      * Create and return a set containing the unique elements returned by the
119      * specified iterable.
120      *
121      * @param <T> element type
122      * @param iterable iterable, must not be null
123      * @return a set containing the unique elements returned by the specified iterable
124      */
125     public static <T> Set<T> asSet(final Iterable<? extends T> iterable)
126     {
127         if (iterable == null)
128         {
129             throw new IllegalArgumentException("iterable must not be null");
130         }
131         Set<T> set = createSet();
132         for (T t : iterable)
133         {
134             set.add(t);
135         }
136         return set;
137     }
138 
139     /**
140      * Create and return a sorted set containing the unique elements in <code>elements</code>
141      * sorted in their natural order.
142      *
143      * @param <T> element type
144      * @param elements variable number of elements to be added to the
145      *    returned sorted set
146      * @return a sorted set containing the unique elements in <code>elements</code>
147      *    sorted in their natural order
148      */
149     public static <T> SortedSet<T> asSortedSet(final T... elements)
150     {
151         SortedSet<T> sortedSet = createSortedSet();
152         for (T t : elements)
153         {
154             sortedSet.add(t);
155         }
156         return sortedSet;
157     }
158 
159     /**
160      * Create and return a sorted set containing the unique elements returned by the
161      * specified iterator sorted in their natural order.
162      *
163      * @param <T> element type
164      * @param iterator iterator, must not be null
165      * @return a sorted set containing the unique elements returned by the specified iterator
166      *    sorted in their natural order
167      */
168     public static <T> SortedSet<T> asSortedSet(final Iterator<? extends T> iterator)
169     {
170         if (iterator == null)
171         {
172             throw new IllegalArgumentException("iterator must not be null");
173         }
174         SortedSet<T> sortedSet = createSortedSet();
175         while (iterator.hasNext())
176         {
177             sortedSet.add(iterator.next());
178         }
179         return sortedSet;
180     }
181 
182    /**
183      * Create and return a sorted set containing the unique elements returned by the
184      * specified iterable sorted in their natural order.
185      *
186      * @param <T> element type
187      * @param iterable iterable, must not be null
188      * @return a sorted set containing the unique elements returned by the specified iterable
189      *    sorted in their natural order
190      */
191     public static <T> SortedSet<T> asSortedSet(final Iterable<? extends T> iterable)
192     {
193         if (iterable == null)
194         {
195             throw new IllegalArgumentException("iterable must not be null");
196         }
197         SortedSet<T> sortedSet = createSortedSet();
198         for (T t : iterable)
199         {
200             sortedSet.add(t);
201         }
202         return sortedSet;
203     }
204 
205     /**
206      * Create and return a sorted set containing the unique elements returned by the
207      * specified iterator sorted according to the specified comparator.
208      *
209      * @param <T> element type
210      * @param iterator iterator, must not be null
211      * @param comparator comparator to be used to sort the returned sorted set
212      * @return a sorted set containing the unique elements returned by the specified iterator
213      *    sorted according to the specified comparator
214      */
215     public static <T> SortedSet<T> asSortedSet(final Iterator<? extends T> iterator,
216                                                final Comparator<? super T> comparator)
217     {
218         if (iterator == null)
219         {
220             throw new IllegalArgumentException("iterator must not be null");
221         }
222         SortedSet<T> sortedSet = createSortedSet(comparator);
223         while (iterator.hasNext())
224         {
225             sortedSet.add(iterator.next());
226         }
227         return sortedSet;
228     }
229 
230    /**
231      * Create and return a sorted set containing the unique elements returned by the
232      * specified iterable sorted according to the specified comparator.
233      *
234      * @param <T> element type
235      * @param iterable iterable, must not be null
236      * @param comparator comparator to be used to sort the returned sorted set
237      * @return a sorted set containing the unique elements returned by the specified iterable
238      *    sorted according to the specified comparator
239      */
240     public static <T> SortedSet<T> asSortedSet(final Iterable<? extends T> iterable,
241                                                final Comparator<? super T> comparator)
242     {
243         if (iterable == null)
244         {
245             throw new IllegalArgumentException("iterable must not be null");
246         }
247         SortedSet<T> sortedSet = createSortedSet(comparator);
248         for (T t : iterable)
249         {
250             sortedSet.add(t);
251         }
252         return sortedSet;
253     }
254 
255     /**
256      * Create and return an immutable set containing the unique elements in <code>elements</code>.
257      * The returned immutable set is a high-performance, immutable <code>Set</code> with reliable
258      * iteration order.
259      *
260      * @param <T> element type
261      * @param elements variable number of elements to be added to the
262      *    returned immutable set
263      * @return an immutable set containing the unique elements in <code>elements</code>
264      */
265     public static <T> Set<T> asImmutableSet(final T... elements)
266     {
267         return ImmutableSet.copyOf(elements);
268     }
269 
270    /**
271      * Create and return an immutable set containing the unique elements returned by the
272      * specified iterator.  The returned immutable set is a high-performance, immutable <code>Set</code>
273      * with reliable iteration order.
274      *
275      * @param <T> element type
276      * @param iterator iterator, must not be null
277      * @return an immutable set containing the unique elements returned by the specified iterator
278      */
279     public static <T> Set<T> asImmutableSet(final Iterator<? extends T> iterator)
280     {
281         if (iterator == null)
282         {
283             throw new IllegalArgumentException("iterator must not be null");
284         }
285         return ImmutableSet.copyOf(iterator);
286     }
287 
288    /**
289      * Create and return an immutable set containing the unique elements returned by the
290      * specified iterable.  The returned immutable set is a high-performance, immutable <code>Set</code>
291      * with reliable iteration order.
292      *
293      * @param <T> element type
294      * @param iterable iterable, must not be null
295      * @return an immutable set containing the unique elements returned by the specified iterable
296      */
297     public static <T> Set<T> asImmutableSet(final Iterable<? extends T> iterable)
298     {
299         if (iterable == null)
300         {
301             throw new IllegalArgumentException("iterable must not be null");
302         }
303         return ImmutableSet.copyOf(iterable);
304     }
305 
306     /**
307      * Create and return an immutable sorted set containing the unique elements in <code>elements</code>
308      * sorted by their natural order. The returned immutable sorted set is a high-performance, immutable
309      * <code>SortedSet</code> that stores its elements in a sorted array.
310      *
311      * @param <T> element type
312      * @param elements variable number of elements to be added to the
313      *    returned immutable sorted set
314      * @return an immutable sorted set containing the unique elements in <code>elements</code>
315      *    sorted by their natural order
316      */
317     public static <T extends Comparable<? super T>> SortedSet<T> asImmutableSortedSet(final T... elements)
318     {
319         return ImmutableSortedSet.copyOf(elements);
320     }
321 
322    /**
323      * Create and return an immutable sorted set containing the unique elements in the specified iterator
324      * sorted by their natural order. The returned immutable sorted set is a high-performance, immutable
325      * <code>SortedSet</code> that stores its elements in a sorted array.
326      *
327      * @param <T> element type
328      * @param iterator iterator, must not be null
329      * @return an immutable set containing the unique elements returned by the specified iterator
330      *    sorted by their natural order
331      */
332     public static <T extends Comparable<? super T>> SortedSet<T> asImmutableSortedSet(final Iterator<? extends T> iterator)
333     {
334         if (iterator == null)
335         {
336             throw new IllegalArgumentException("iterator must not be null");
337         }
338         return ImmutableSortedSet.copyOf(iterator);
339     }
340 
341    /**
342      * Create and return an immutable sorted set containing the unique elements in the specified iterable
343      * sorted by their natural order. The returned immutable sorted set is a high-performance, immutable
344      * <code>SortedSet</code> that stores its elements in a sorted array.
345      *
346      * @param <T> element type
347      * @param iterable iterable, must not be null
348      * @return an immutable set containing the unique elements returned by the specified iterable
349      *    sorted by their natural order
350      */
351     public static <T extends Comparable<? super T>> SortedSet<T> asImmutableSortedSet(final Iterable<? extends T> iterable)
352     {
353         if (iterable == null)
354         {
355             throw new IllegalArgumentException("iterable must not be null");
356         }
357         return ImmutableSortedSet.copyOf(iterable);
358     }
359 
360    /**
361      * Create and return an immutable sorted set containing the unique elements in the specified iterator
362      * sorted according to the specified comparator. The returned immutable sorted set is a high-performance,
363      * immutable <code>SortedSet</code> that stores its elements in a sorted array.
364      *
365      * @param <T> element type
366      * @param iterator iterator, must not be null
367      * @param comparator comparator to be used to sort the returned immutable sorted set
368      * @return an immutable set containing the unique elements returned by the specified iterator
369      *    sorted according to the specified comparator
370      */
371     public static <T extends Comparable<? super T>> SortedSet<T> asImmutableSortedSet(final Iterator<? extends T> iterator,
372                                                                               final Comparator<? super T> comparator)
373     {
374         if (iterator == null)
375         {
376             throw new IllegalArgumentException("iterator must not be null");
377         }
378         if (comparator == null)
379         {
380             return asImmutableSortedSet(iterator);
381         }
382         ImmutableSortedSet.Builder<T> builder = new ImmutableSortedSet.Builder<T>(comparator);
383         builder.addAll(iterator);
384         return builder.build();
385     }
386 
387    /**
388      * Create and return an immutable sorted set containing the unique elements in the specified iterable
389      * sorted according to the specified comparator. The returned immutable sorted set is a high-performance,
390      * immutable <code>SortedSet</code> that stores its elements in a sorted array.
391      *
392      * @param <T> element type
393      * @param iterable iterable, must not be null
394      * @param comparator comparator to be used to sort the returned immutable sorted set
395      * @return an immutable set containing the unique elements returned by the specified iterable
396      *    sorted according to the specified comparator
397      */
398     public static <T extends Comparable<? super T>> SortedSet<T> asImmutableSortedSet(final Iterable<? extends T> iterable,
399                                                                               final Comparator<? super T> comparator)
400     {
401         if (iterable == null)
402         {
403             throw new IllegalArgumentException("iterable must not be null");
404         }
405         if (comparator == null)
406         {
407             return asImmutableSortedSet(iterable);
408         }
409         ImmutableSortedSet.Builder<T> builder = new ImmutableSortedSet.Builder<T>(comparator);
410         builder.addAll(iterable);
411         return builder.build();
412     }
413 
414     //
415     // createXxx methods, which accept constructor parameters
416 
417     /**
418      * Create and return a new instance of Set.
419      *
420      * @param <T> element type
421      * @return a new instance of Set
422      */
423     public static <T> Set<T> createSet()
424     {
425         return new HashSet<T>();
426     }
427 
428     /**
429      * Create and return a new instance of Set containing the elements of the
430      * specified collection.
431      *
432      * @param <T> element type
433      * @param elements elements to be added to the returned set, must not be null
434      * @return a new instance of Set containing the elements of the specified
435      *    collection
436      */
437     public static <T> Set<T> createSet(final Collection<? extends T> elements)
438     {
439         return new HashSet<T>(elements);
440     }
441 
442     /**
443      * Create and return a new instance of Set with the specified initial capacity.
444      *
445      * @param <T> element type
446      * @param initialCapacity initial capacity
447      * @return a new instance of Set with the specified initial capacity
448      */
449     public static <T> Set<T> createSet(final int initialCapacity)
450     {
451         return new HashSet<T>(initialCapacity);
452     }
453 
454     /**
455      * Create and return a new instance of Set with the specified initial capacity
456      * and load factor.
457      *
458      * @param <T> element type
459      * @param initialCapacity initial capacity
460      * @param loadFactor load factor
461      * @return a new instance of Set with the specified initial capacity and
462      *    load factor
463      */
464     public static <T> Set<T> createSet(final int initialCapacity, final float loadFactor)
465     {
466         return new HashSet<T>(initialCapacity, loadFactor);
467     }
468 
469     /**
470      * Create and return a new instance of SortedSet.
471      *
472      * @param <T> element type
473      * @return a new instance of SortedSet
474      */
475     public static <T> SortedSet<T> createSortedSet()
476     {
477         return new TreeSet<T>();
478     }
479 
480     /**
481      * Create and return a new instance of SortedSet containing the elements
482      * of the specified collection.
483      *
484      * @param <T> element type
485      * @param elements elements to be added to the returned set, must not be null
486      * @return a new instance of SortedSet containing the elements of the
487      *    specified collection
488      */
489     public static <T> SortedSet<T> createSortedSet(final Collection<? extends T> elements)
490     {
491         return new TreeSet<T>(elements);
492     }
493 
494     /**
495      * Create and return a new instance of SortedSet sorted according to the specified
496      * comparator.
497      *
498      * @param <T> element type
499      * @param comparator comparator to be used to sort the returned set
500      * @return a new instance of SortedSet
501      */
502     public static <T> SortedSet<T> createSortedSet(final Comparator<? super T> comparator)
503     {
504         return new TreeSet<T>(comparator);
505     }
506 
507     /**
508      * Create and return a new instance of SortedSet containing the elements
509      * of the specified collection sorted according to the specified comparator.
510      *
511      * @param <T> element type
512      * @param elements elements to be added to the returned set, must not be null
513      * @param comparator comparator to be used to sort the returned set
514      * @return a new instance of SortedSet containing the elements of the
515      *    specified collection sorted according to the specified comparator
516      */
517     public static <T> SortedSet<T> createSortedSet(final Collection<? extends T> elements,
518                                                 final Comparator<? super T> comparator)
519     {
520         SortedSet<T> sortedSet = createSortedSet(comparator);
521         sortedSet.addAll(elements);
522         return sortedSet;
523     }
524 
525     /**
526      * Create and return a new instance of SortedSet containing the same elements
527      * in the same order as the specified sorted set.
528      *
529      * @param <T> element type
530      * @param sortedSet sorted set of elements to be added to the returned set, must not be null
531      * @return a new instance of SortedSet containing the same elements in the same
532      *    order as the specified sorted set
533      */
534     public static <T> SortedSet<T> createSortedSet(final SortedSet<T> sortedSet)
535     {
536         return new TreeSet<T>(sortedSet);
537     }
538 
539     /**
540      * Create and return a new instance of NavigableSet.
541      *
542      * @param <T> element type
543      * @return a new instance of NavigableSet
544      */
545     public static <T> NavigableSet<T> createNavigableSet()
546     {
547         return new TreeSet<T>();
548     }
549 
550     /**
551      * Create and return a new instance of NavigableSet containing the elements
552      * of the specified collection.
553      *
554      * @param <T> element type
555      * @param elements elements to be added to the returned set, must not be null
556      * @return a new instance of NavigableSet containing the elements of the
557      *    specified collection
558      */
559     public static <T> NavigableSet<T> createNavigableSet(final Collection<? extends T> elements)
560     {
561         return new TreeSet<T>(elements);
562     }
563 
564     /**
565      * Create and return a new instance of NavigableSet sorted according to the specified
566      * comparator.
567      *
568      * @param <T> element type
569      * @param comparator comparator to be used to sort the returned set
570      * @return a new instance of NavigableSet sorted according to the specified comparator
571      */
572     public static <T> NavigableSet<T> createNavigableSet(final Comparator<? super T> comparator)
573     {
574         return new TreeSet<T>(comparator);
575     }
576 
577     /**
578      * Create and return a new instance of NavigableSet containing the elements
579      * of the specified collection sorted according to the specified comparator.
580      *
581      * @param <T> element type
582      * @param elements elements to be added to the returned set, must not be null
583      * @param comparator comparator to be used to sort the returned set
584      * @return a new instance of NavigableSet containing the elements of the
585      *    specified collection sorted according to the specified comparator
586      */
587     public static <T> NavigableSet<T> createNavigableSet(final Collection<? extends T> elements,
588                                                       final Comparator<? super T> comparator)
589     {
590         NavigableSet<T> navigableSet = createNavigableSet(comparator);
591         navigableSet.addAll(elements);
592         return navigableSet;
593     }
594 
595     /**
596      * Create and return a new instance of NavigableSet containing the same elements
597      * in the same order as the specified sorted set.
598      *
599      * @param <T> element type
600      * @param sortedSet sorted set of elements to be added to the returned set, must not be null
601      * @return a new instance of NavigableSet containing the same elements in the same
602      *    order as the specified sorted set
603      */
604     public static <T> NavigableSet<T> createNavigableSet(final SortedSet<T> sortedSet)
605     {
606         return new TreeSet<T>(sortedSet);
607     }
608 
609     /**
610      * Create and return a new instance of ConcurrentSkipListSet.
611      *
612      * @param <T> element type
613      * @return a new instance of ConcurrentSkipListSet
614      */
615     public static <T> ConcurrentSkipListSet<T> createConcurrentSkipListSet()
616     {
617         return new ConcurrentSkipListSet<T>();
618     }
619 
620     /**
621      * Create and return a new instance of ConcurrentSkipListSet containing the elements
622      * of the specified collection.
623      *
624      * @param <T> element type
625      * @param elements elements to be added to the returned set, must not be null
626      * @return a new instance of ConcurrentSkipListSet containing the elements of the
627      *    specified collection
628      */
629     public static <T> ConcurrentSkipListSet<T> createConcurrentSkipListSet(final Collection<? extends T> elements)
630     {
631         return new ConcurrentSkipListSet<T>(elements);
632     }
633 
634     /**
635      * Create and return a new instance of ConcurrentSkipListSet sorted according to the specified
636      * comparator.
637      *
638      * @param <T> element type
639      * @param comparator comparator to be used to sort the returned set
640      * @return a new instance of ConcurrentSkipListSet
641      */
642     public static <T> ConcurrentSkipListSet<T> createConcurrentSkipListSet(final Comparator<? super T> comparator)
643     {
644         return new ConcurrentSkipListSet<T>(comparator);
645     }
646 
647     /**
648      * Create and return a new instance of ConcurrentSkipListSet containing the elements
649      * of the specified collection sorted according to the specified comparator.
650      *
651      * @param <T> element type
652      * @param elements elements to be added to the returned set, must not be null
653      * @param comparator comparator to be used to sort the returned set
654      * @return a new instance of ConcurrentSkipListSet containing the elements of the
655      *    specified collection sorted according to the specified comparator
656      */
657     public static <T> ConcurrentSkipListSet<T> createConcurrentSkipListSet(final Collection<? extends T> elements,
658                                                                         final Comparator<? super T> comparator)
659     {
660         ConcurrentSkipListSet<T> set = createConcurrentSkipListSet(comparator);
661         set.addAll(elements);
662         return set;
663     }
664 
665     /**
666      * Create and return a new instance of ConcurrentSkipListSet containing the same elements
667      * in the same order as the specified sorted set.
668      *
669      * @param <T> element type
670      * @param sortedSet sorted set of elements to be added to the returned set, must not be null
671      * @return a new instance of ConcurrentSkipListSet containing the same elements in the same
672      *    order as the specified sorted set
673      */
674     public static <T> ConcurrentSkipListSet<T> createConcurrentSkipListSet(final SortedSet<T> sortedSet)
675     {
676         return new ConcurrentSkipListSet<T>(sortedSet);
677     }
678 
679     /**
680      * Create and return a new instance of CopyOnWriteArraySet.
681      *
682      * @param <T> element type
683      * @return a new instance of CopyOnWriteArraySet
684      */
685     public static <T> CopyOnWriteArraySet<T> createCopyOnWriteArraySet()
686     {
687         return new CopyOnWriteArraySet<T>();
688     }
689 
690     /**
691      * Create and return a new instance of CopyOnWriteArraySet containing the elements
692      * of the specified collection.
693      *
694      * @param <T> element type
695      * @param elements elements to be added to the returned set, must not be null
696      * @return a new instance of CopyOnWriteArraySet containing the elements of the
697      *    specified collection
698      */
699     public static <T> CopyOnWriteArraySet<T> createCopyOnWriteArraySet(final Collection<? extends T> elements)
700     {
701         return new CopyOnWriteArraySet<T>(elements);
702     }
703 
704     /**
705      * Create and return a new non-blocking implementation of Set.
706      *
707      * @param <T> element type
708      * @return a new non-blocking implementation of Set
709      */
710     public static <T> Set<T> createNonBlockingSet()
711     {
712         return new NonBlockingHashSet<T>();
713     }
714 
715     /**
716      * Create and return a new non-blocking implementation of Set containing the elements
717      * of the specified collection.
718      *
719      * @param <T> element type
720      * @param elements elements to be added to the returned set, must not be null
721      * @return a new non-blocking implementation of Set containing the elements of the
722      *    specified collection
723      */
724     public static <T> Set<T> createNonBlockingSet(final Collection<? extends T> elements)
725     {
726         Set<T> set = createNonBlockingSet();
727         set.addAll(elements);
728         return set;
729     }
730 
731     //
732     // view or wrapper methods, which accept sets or sorted sets as parameters
733 
734     /**
735      * Create and return an unmodifiable view of the specified set.  Query operations on the
736      * returned set "read through" to the specified set, and attempts to modify the returned set,
737      * whether direct or via its iterator, result in an <code>UnsupportedOperationException</code>.
738      *
739      * @param <T> element type
740      * @param set set to view, must not be null
741      * @return an unmodifiable view of the specified set
742      */
743     public static <T> Set<T> unmodifiableSet(final Set<? extends T> set)
744     {
745         return Collections.unmodifiableSet(set);
746     }
747 
748     /**
749      * Create and return an unmodifiable view of the specified sorted set.  Query operations
750      * on the returned sorted set "read through" to the specified sorted set.  Attempts to modify
751      * the returned sorted set, whether direct, via its iterator, or via its <code>subSet</code>,
752      * <code>headSet</code>, or <code>tailSet</code> views, result in an
753      * <code>UnsupportedOperationException</code>.
754      *
755      * @param <T> element type
756      * @param sortedSet sorted set to view, must not be null
757      * @return an unmodifiable view of the specified sorted set
758      */
759     public static <T> SortedSet<T> unmodifiableSortedSet(final SortedSet<T> sortedSet)
760     {
761         return Collections.unmodifiableSortedSet(sortedSet);
762     }
763 
764     /**
765      * Create and return an immutable set containing the elements in the specified set, in order.
766      * The returned immutable set is a high-performance, immutable <code>Set</code>
767      * with reliable iteration order.
768      *
769      * @param <T> element type
770      * @param set set to copy, must not be null
771      * @return an immutable set containing the elements in the specified set, in order
772      */
773     public static <T> Set<T> immutableSet(final Set<? extends T> set)
774     {
775         return ImmutableSet.copyOf(set);
776     }
777 
778     /**
779      * Create and return an immutable sorted set containing the elements in the specified
780      * sorted set, sorted by the same comparator.
781      *
782      * @param <T> element type
783      * @param sortedSet sorted set to copy, must not be null
784      * @return an immutable sorted set containing the elements in the specified sorted set,
785      *    sorted by the same comparator
786      */
787     public static <T> SortedSet<T> immutableSortedSet(final SortedSet<T> sortedSet)
788     {
789         return ImmutableSortedSet.copyOfSorted(sortedSet);
790     }
791 }