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.component;
17  
18  import jp.liq.container.Component;
19  import jp.liq.container.Resolver;
20  
21  /**
22   * 自分のクラスのサブクラスのインスタンスを取得して返すコンポーネントです。
23   * @author nosen
24   */
25  public class MappingComponent<T> implements Component<T> {
26  
27      private Class<?> subtype;
28      private Class<T> type;
29  
30      /**
31       * このクラスのインスタンスを構築します。
32       * @param type このコンポーネントによってインスタンスを生成されるクラスです。
33       * @param subtype 引数 type のサブクラスです。
34       */
35      public MappingComponent(Class<T> type, Class<?> subtype) {
36          this.type = type;
37          this.subtype = subtype;
38      }
39  
40      /**
41       * @see jp.liq.container.Component#getDescription()
42       */
43      public Object getDescription() {
44          return "{" + type + "=>" + subtype + "}";
45      }
46  
47      /**
48       * @see jp.liq.container.Component#getInstance(jp.liq.container.Resolver)
49       */
50      public T getInstance(Resolver resolver) {
51          return type.cast(resolver.resolve(subtype));
52      }
53  
54      /**
55       * @see jp.liq.container.Component#getType()
56       */
57      public Class<T> getType() {
58          return type;
59      }
60  
61  }