View Javadoc

1   /*
2    * Copyright 2006 Outsource Cafe, Inc.
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 org.javagen.agile.core.emitter.binding;
17  
18  import java.util.Collections;
19  import java.util.HashMap;
20  import java.util.HashSet;
21  import java.util.Map;
22  import java.util.Set;
23  
24  import org.javagen.agile.core.emitter.Emitter;
25  import org.javagen.agile.core.model.Model;
26  
27  /***
28   * Binds Emitters to model instances based on Model's modelType
29   * property.  Emitters are mapped to modelTypes using a Map.
30   * 
31   * @author Richard Easterling
32   */
33  public class ModelTypeBinder implements Binder {
34  	
35  	static final Set<Emitter> EMPTY_EMITTER_SET = Collections.emptySet();
36  	
37  	Map<String,Set<Emitter>> emitterBindings = new HashMap<String,Set<Emitter>>();
38  
39  	////////////////////////////////////////////////////////////////////////////
40  	// Binder inteface:
41  	////////////////////////////////////////////////////////////////////////////
42  	
43  	public Set<Emitter> boundEmitters(Model modelNode) {
44  		return boundEmitters(modelNode==null ? null : modelNode.getModelType());
45  	}
46  
47  	////////////////////////////////////////////////////////////////////////////
48  	// modelType Binder methods:
49  	////////////////////////////////////////////////////////////////////////////
50  	
51  	public void bindEmitter(String modelType, Emitter emitter) {
52  		Set<Emitter> emitterSet = emitterBindings.get(modelType);
53  		if (emitterSet==null) {
54  			emitterSet = new HashSet<Emitter>();
55  			emitterBindings.put(modelType, emitterSet);
56  		}
57  		emitterSet.add(emitter);
58  	}
59  	
60  	public Set<Emitter> boundEmitters(String modelType) {
61  		if (modelType==null)
62  			return EMPTY_EMITTER_SET;
63  		Set<Emitter> emitters = emitterBindings.get(modelType);
64  		return emitters==null ? EMPTY_EMITTER_SET : emitters;
65  	}
66  
67  	public Map<String, Set<Emitter>> getEmitterBindings() {
68  		return emitterBindings;
69  	}
70  
71  	public void setEmitterBindings(Map<String, Set<Emitter>> emitterCache) {
72  		this.emitterBindings = emitterCache;
73  	}
74  }