View Javadoc

1   package com.atlassian.core.util.filter;
2   
3   import java.util.List;
4   import java.util.Iterator;
5   import java.util.ArrayList;
6   
7   /**
8    * Filter the contents of a list based on some criteria.
9    *
10   * @deprecated use Guava's {@code Iterables.filter()}.
11   */
12  @Deprecated
13  public class ListFilter<T>
14  {
15      private final Filter<T> filter;
16      private static final Object MYNULL = new Object();
17  
18      private class FilteredIterator<E extends T> implements Iterator<T>
19      {
20          private final Iterator<E> innerIterator;
21  
22          // We need to define our own "null", since there may be real
23          // nulls in a list. Funky, eh.
24          private T savedObject = myNull();
25  
26          public FilteredIterator(Iterator<E> innerIterator)
27          {
28              this.innerIterator = innerIterator;
29          }
30  
31          public void remove()
32          {
33              innerIterator.remove();
34          }
35  
36          public boolean hasNext()
37          {
38              if (savedObject != MYNULL)
39                  return true;
40  
41              while (innerIterator.hasNext())
42              {
43                  savedObject = innerIterator.next();
44                  if (filter.isIncluded(savedObject))
45                      return true;
46              }
47              savedObject = myNull();
48  
49              return false;
50          }
51  
52          public T next()
53          {
54              if (savedObject != MYNULL)
55                  return clearSavedObject();
56  
57              while (true)
58              {
59                  E o = innerIterator.next();
60                  if (filter.isIncluded(o))
61                      return o;
62              }
63          }
64  
65          private T clearSavedObject()
66          {
67              T ret = savedObject;
68              savedObject = myNull();
69              return ret;
70          }
71  
72          private T myNull() {
73              return (T) MYNULL;
74          }
75  
76      }
77  
78      /**
79       * Constructor, taking the filter implementation that will be used to
80       * filter the list.
81       *
82       * @param filter the filter implementation that will be used to filter the
83       *        list.
84       */
85      public ListFilter(Filter<T> filter)
86      {
87          this.filter = filter;
88      }
89  
90      /**
91       * Filter the contents of a list. Returns a new list with the filtered
92       * objects removed. Does not change the list passed in.
93       *
94       * @param list the list to filter
95       * @return a new list with the filtered objects removed.
96       */
97      public List<T> filterList(List<T> list)
98      {
99          if (list ==  null)
100             return list;
101         
102         List<T> filteredList = new ArrayList<T>();
103         Iterator<T> i = filterIterator(list.iterator());
104         while (i.hasNext())
105             filteredList.add(i.next());
106 
107         return filteredList;
108     }
109 
110     /**
111      * Filter the contents of an iterator. Returns an iterator that will only
112      * return un-filtered members of the supplied iterator.
113      */
114     public Iterator<T> filterIterator(Iterator<T> iterator)
115     {
116         return new FilteredIterator<T>(iterator);
117     }
118 
119 }