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.reflect;
17  
18  import jp.liq.container.util.Matcher;
19  
20  /**
21   * あるクラスが保持するフィールドの中から、
22   * 条件にマッチするものを順に返す Iterable です。
23   * @author nose
24   */
25  public abstract class FieldFinder<T extends FieldWrapper, S extends FieldFinder<T, S>> extends MemberFinder<T, S> {
26  
27      /**
28       * 引数で指定されたクラスを絞り込み条件に追加します。
29       * 引数で指定されたクラスのサブクラスのみがマッチします。
30       * @param type 絞り込み条件に追加するクラス。
31       * @return 自分自身のインスタンス。
32       */
33      public S isA(Class<?> type) {
34          addMatcher(new FieldTypeMatcher<T>(type));
35          return getThis();
36      }
37  
38      private class FieldTypeMatcher<F extends FieldWrapper> implements Matcher<F> {
39  
40          private Class<?> type;
41  
42          FieldTypeMatcher(Class<?> type) {
43              this.type = type;
44          }
45  
46          public boolean matches(F m) {
47              return type.isAssignableFrom(m.getField().getType());
48          }
49          
50      }
51  }
52