View Javadoc

1   /*
2    * Copyright 2007-2008 Naoki NOSE.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package jp.liq.container.util;
17  
18  import java.util.ArrayList;
19  import java.util.Iterator;
20  import java.util.List;
21  
22  /**
23   * クラスのメンバから指定した条件にマッチしたものを順に返す Iterable の実装です。
24   * @param T 検索するメンバ
25   * @param C メンバを保持するクラスの型
26   * @param S このクラスのサブクラスの型
27   * @author nose
28   */
29  public abstract class AbstractFinder<T, S extends AbstractFinder<T, S>> 
30          implements Iterable<T> {
31      private final List<Matcher<T>> matchers;
32  
33      /**
34       * このクラスのインスタンスを構築します。
35       */
36      public AbstractFinder() {
37          this.matchers = new ArrayList<Matcher<T>>();
38      }
39  
40      protected void addMatcher(Matcher<T> matcher) {
41          matchers.add(matcher);
42      }
43  
44      /**
45       * thisを返します。
46       */
47      protected abstract S getThis();
48  
49      protected abstract Iterator<T> getCandidates();
50  
51      public Iterator<T> iterator() {
52          return new MatchingIterator();
53      }
54  
55      private class MatchingIterator implements Iterator<T> {
56          private final Iterator<T> candidates;
57          private T nextMatches;
58          
59          MatchingIterator() {
60              candidates = getCandidates();
61              findNext();
62          }
63  
64          public void findNext() {
65              while(candidates.hasNext()) {
66                  T next = candidates.next();
67                  if(matches(next)) {
68                      nextMatches = next;
69                      return;
70                  }
71              }
72              nextMatches = null;
73          }
74  
75          public boolean hasNext() {
76              return nextMatches != null;
77          }
78  
79          public T next() {
80              T rv = nextMatches;
81              findNext();
82              return rv;
83          }
84  
85          private boolean matches(T candidate) {
86              for(Matcher<T> matcher: matchers) {
87                  if(matcher.matches(candidate)) {
88                      continue;
89                  } else {
90                      return false;
91                  }
92              }
93              return true;
94          }
95  
96          public void remove() {
97              throw new UnsupportedOperationException();
98              
99          }
100     }
101 
102 }