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   * 条件にマッチするものを返す、{@link FieldFinder}のサブクラスです。
27   * @author nosen
28   */
29  public class ObjectFieldFinder extends FieldFinder<ObjectField, ObjectFieldFinder> 
30      implements Iterable<ObjectField> {
31      private final Object target;
32  
33      public ObjectFieldFinder(Object target) {
34          this.target = target;
35  
36      }
37  
38      /**
39       * @see jp.liq.container.reflect.MemberFinder#getThis()
40       */
41      @Override
42      protected ObjectFieldFinder getThis() {
43          return this;
44      }
45  
46      @Override
47      protected Iterator<ObjectField> getCandidates() {
48          return new ObjectFieldIterator(
49                  new ArrayIterator<Field>(target.getClass().getFields()));
50      }
51  
52      private class ObjectFieldIterator extends IteratorWrapper<ObjectField, Field> {
53          public ObjectFieldIterator(Iterator<Field> iterator) {
54              super(iterator);
55          }
56  
57          public ObjectField next() {
58              return new ObjectField(iterator.next(), target);
59          }
60          
61      }
62  }