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 java.lang.reflect.Field;
19  import java.util.Iterator;
20  
21  import jp.liq.container.util.ArrayIterator;
22  import jp.liq.container.util.IteratorWrapper;
23  
24  /**
25   * あるクラスが保持するフィールドの中から、
26   * 条件にマッチするものを順に返す Iterable です。
27   * @author nose
28   */
29  public class ClassFieldFinder<T> extends FieldFinder<FieldWrapper, ClassFieldFinder<T>> 
30      implements Iterable<FieldWrapper> {
31  
32      private final Class<T> ownerClass;
33      /**
34       * このクラスの新しいインスタンスを構築します。
35       * @param ownerClass フィールドを保持するクラス。
36       */
37      public ClassFieldFinder(Class<T> ownerClass) {
38          this.ownerClass = ownerClass;
39  
40      }
41  
42      /**
43       * @see jp.liq.container.reflect.MemberFinder#getThis()
44       */
45      @Override
46      protected ClassFieldFinder<T> getThis() {
47          return this;
48      }
49  
50      @Override
51      protected Iterator<FieldWrapper> getCandidates() {
52          return new FieldWrapperIterator(
53                  new ArrayIterator<Field>(ownerClass.getFields()));
54      }
55  
56      private class FieldWrapperIterator extends IteratorWrapper<FieldWrapper, Field> {
57          public FieldWrapperIterator(Iterator<Field> iterator) {
58              super(iterator);
59          }
60  
61          public FieldWrapper next() {
62              return new FieldWrapper(iterator.next());
63          }
64          
65      }
66  }