View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.util;
21  
22  import java.util.Iterator;
23  
24  /**
25   *
26   */
27  public abstract class SkipMatchIterator<T> implements Iterator<T>
28  {
29      private Iterator<T> delegate;
30      private T value;
31  
32  
33      public SkipMatchIterator(Iterator<T> delegate)
34      {
35          this.delegate = delegate;
36          this.value = null;
37      }
38  
39      @Override
40      public boolean hasNext()
41      {
42          if (this.value != null)
43          {
44              return true;
45          }
46          if (delegate.hasNext())
47          {
48              do 
49              {
50                  this.value = delegate.next();
51                  if (match(value))
52                  {
53                      //Skip
54                      value = null;
55                  }
56              }
57              while (value == null && delegate.hasNext());
58  
59              if (value != null)
60              {
61                  return true;
62              }
63              else
64              {
65                  return false;
66              }
67          }
68          else
69          {
70              return false;
71          }
72      }
73  
74      @Override
75      public T next()
76      {
77          T value = this.value;
78          do 
79          {
80              if (value == null)
81              {
82                  if (hasNext())
83                  {
84                      value = null;
85                      return this.value;
86                  }
87                  else
88                  {
89                      return null;
90                  }
91              }
92              else
93              {
94                  this.value = null;
95                  return value;
96              }
97          }
98          while (value == null);
99      }
100     
101     protected abstract boolean match(T instance);
102 }