View Javadoc

1   // Copyright 2008 Thiago H. de Paula Figueiredo
2   //
3   // Licensed under the Apache License, Version 2.0 (the "License");
4   // you may not use this file except in compliance with the License.
5   // You may obtain a copy of the License at
6   //
7   //     http://www.apache.org/licenses/LICENSE-2.0
8   //
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  
15  package br.com.arsmachina.tapestrycrud.selectmodel.impl;
16  
17  import java.util.List;
18  import java.util.Map;
19  
20  import org.apache.tapestry5.Link;
21  import org.apache.tapestry5.SelectModel;
22  import org.apache.tapestry5.ioc.util.StrategyRegistry;
23  
24  import br.com.arsmachina.module.service.ControllerSource;
25  import br.com.arsmachina.tapestrycrud.selectmodel.SelectModelFactory;
26  import br.com.arsmachina.tapestrycrud.selectmodel.SingleTypeSelectModelFactory;
27  import br.com.arsmachina.tapestrycrud.services.EncoderSource;
28  
29  /**
30   * Default {@link SelectModelFactory} implementation. It delegates all its methods for
31   * {@link SelectModelFactory} instances.
32   * 
33   * @author Thiago H. de Paula Figueiredo
34   * @see SelectModel
35   * @see SingleTypeSelectModelFactory
36   * @todo handle enums.
37   */
38  public class SelectModelFactoryImpl implements SelectModelFactory {
39  
40  	@SuppressWarnings("unchecked")
41  	final private StrategyRegistry<SingleTypeSelectModelFactory> registry;
42  
43  	/**
44  	 * Single constructor.
45  	 * 
46  	 * @param registrations a {@link Map}&lt;{@link Class}, {@link Link}{@link SingleTypeSelectModelFactory}&gt;.
47  	 * It cannot be null.
48  	 * @param controllerSource a {@link ControllerSource}. It cannot be null.
49  	 * @param encoderSource an {@link EncoderSource}. It cannot be null.
50  	 */
51  	@SuppressWarnings("unchecked")
52  	public SelectModelFactoryImpl(Map<Class, SingleTypeSelectModelFactory> registrations) {
53  		registry = StrategyRegistry.newInstance(SingleTypeSelectModelFactory.class, registrations);
54  	}
55  
56  	/**
57  	 * @see br.com.arsmachina.tapestrycrud.selectmodel.SelectModelFactory#create(java.lang.Class)
58  	 */
59  	public SelectModel create(Class<?> clasz) {
60  		return create(clasz, null);
61  	}
62  
63  	/**
64  	 * @see br.com.arsmachina.tapestrycrud.selectmodel.SelectModelFactory#create(java.lang.Class,
65  	 * java.util.List)
66  	 */
67  	public <T> SelectModel create(Class<T> clasz, List<T> objects) {
68  		return get(clasz).create(objects);
69  	}
70  
71  	@SuppressWarnings("unchecked")
72  	private <T> SingleTypeSelectModelFactory<T> get(Class<T> clasz) {
73  		return registry.get(clasz);
74  	}
75  
76  }