View Javadoc

1   package com.atlassian.plugin.util.collect;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   import java.util.Collections;
6   import java.util.Comparator;
7   import java.util.Iterator;
8   import java.util.List;
9   
10  /**
11   * @deprecated since 2.12.3. Use Google Guava instead.
12   */
13  public class CollectionUtil
14  {
15  
16      public static <T> void foreach(final Iterator<T> iterator, final Consumer<T> sink)
17      {
18          while (iterator.hasNext())
19          {
20              sink.consume(iterator.next());
21          }
22      }
23  
24      public static <T> void foreach(final Iterable<T> iterable, final Consumer<T> sink)
25      {
26          if (iterable != null)
27          {
28              foreach(iterable.iterator(), sink);
29          }
30      }
31  
32      /**
33       * @return the given iterable converted to a List.
34       * @deprecated since 2.12.3. Use {@link com.google.common.collect.ImmutableList#copyOf(Iterable)} instead.
35       */
36      @Deprecated
37      public static <T> List<T> toList(final Iterable<T> iterable)
38      {
39          return toList(iterable.iterator());
40      }
41  
42      /**
43       * @return the given iterator converted to a List.
44       * @deprecated since 2.12.3. Use {@link com.google.common.collect.ImmutableList#copyOf(Iterator)} instead.
45       */
46      @Deprecated
47      public static <T> List<T> toList(final Iterator<T> iterator)
48      {
49          final List<T> result = new ArrayList<T>();
50          foreach(iterator, new Consumer<T>()
51          {
52              public void consume(final T element)
53              {
54                  if (element != null)
55                  {
56                      result.add(element);
57                  }
58              }
59          });
60          return result;
61      }
62  
63      /**
64       * @deprecated since 2.12.3. Use {@link com.google.common.collect.Lists#transform(java.util.List, com.google.common.base.Function)} instead.
65       */
66      @Deprecated
67      public static <T, R> List<R> transform(final Iterator<T> iterator, final Function<T, R> transformer)
68      {
69          return toList(transformIterator(iterator, transformer));
70      }
71  
72      /**
73       * @deprecated since 2.12.3. Use {@link com.google.common.collect.Iterables#transform(Iterable, com.google.common.base.Function)} instead.
74       */
75      @Deprecated
76      public static <T, R> List<R> transform(final Iterable<T> iterable, final Function<T, R> transformer)
77      {
78          if (iterable == null)
79          {
80              return Collections.emptyList();
81          }
82          return transform(iterable.iterator(), transformer);
83      }
84  
85      /**
86       * @deprecated since 2.12.3. Use {@link com.google.common.collect.Iterators#transform(java.util.Iterator, com.google.common.base.Function)} instead.
87       */
88      @Deprecated
89      public static <T, R> Iterator<R> transformIterator(final Iterator<T> iterator, final Function<T, R> transformer)
90      {
91          return new TransformingIterator<T, R>(iterator, transformer);
92      }
93  
94      /**
95       * Create a filtered {@link Iterator}.
96       * @deprecated since 2.12.3. Use {@link com.google.common.collect.Iterators#filter(java.util.Iterator, com.google.common.base.Predicate)} instead.
97       */
98      @Deprecated
99      public static <T> Iterator<T> filter(final Iterator<T> iterator, final Predicate<T> predicate)
100     {
101         return new FilteredIterator<T>(iterator, predicate);
102     }
103 
104     /**
105      * Create a filtered {@link Iterator}.
106      * @deprecated since 2.12.3. Use {@link com.google.common.collect.Iterables#filter(java.lang.Iterable, com.google.common.base.Predicate)} instead.
107      */
108     public static <T> Iterable<T> filter(final Iterable<T> iterable, final Predicate<T> predicate)
109     {
110         return new FilteredIterable<T>(iterable, predicate);
111     }
112 
113     static class FilteredIterable<T> implements Iterable<T>
114     {
115         private final Iterable<T> delegate;
116         private final Predicate<T> predicate;
117 
118         FilteredIterable(final Iterable<T> delegate, final Predicate<T> predicate)
119         {
120             this.delegate = delegate;
121             this.predicate = predicate;
122         }
123 
124         public Iterator<T> iterator()
125         {
126             return new FilteredIterator<T>(delegate.iterator(), predicate);
127         }
128 
129         @Override
130         public String toString()
131         {
132             return toList(this).toString();
133         }
134     }
135 
136     public static <T> List<T> sort(final Collection<T> collection, final Comparator<T> comparator)
137     {
138         final List<T> sorted = new ArrayList<T>(collection);
139         if (sorted.size() > 1)
140         {
141             Collections.sort(sorted, comparator);
142         }
143         return sorted;
144     }
145 }