View Javadoc
1   /**
2    * Copyright 2005-2015 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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.kuali.rice.krms.impl.repository.language;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.List;
21  import java.util.Map;
22  
23  
24  /**
25   * This class is a registry of template contexts which the requirement 
26   * component translator uses to generate natural language.
27   */
28  public class ContextRegistry<T extends Context>  {
29  
30  	/** Registry context map */
31  	private Map<String, List<T>> registry = new HashMap<String, List<T>>();
32  
33  	/**
34  	 * Constructor.
35  	 */
36  	public ContextRegistry() {
37  	}
38  
39  	/**
40  	 * Constructor. Adds a context registry as a map.
41  	 * 
42  	 * @param registry Context registry
43  	 */
44  	public ContextRegistry(final Map<String, List<T>> registry) {
45  		this.registry = registry;
46  	}
47  
48  	/**
49  	 * Adds a context to the registry. Key is usually a TermParameterType key.
50  	 * 
51  	 * @param key Context key
52  	 * @param context Context
53  	 */
54  	public void add(final String key, final T context) {
55  		if(this.registry.containsKey(key)) {
56  			this.registry.get(key).add(context);
57  		} else {
58  			List<T> list = new ArrayList<T>();
59  			list.add(context);
60  			this.registry.put(key, list);
61  		}
62  	}
63  
64  	/**
65  	 * Gets a context from the registry. Key is usually a TermParameterType key.
66  	 * 
67  	 * @param key Context key
68  	 * @return A context
69  	 */
70  	public List<T> get(final String key) {
71  		return this.registry.get(key);
72  	}
73  
74  	/**
75  	 * Returns true if a context exists for <code>key</code>; otherwise false.
76  	 * 
77  	 * @param key Context key
78  	 * @return True if a context exists otherwise false
79  	 */
80  	public boolean containsKey(final String key) {
81  		return this.registry.containsKey(key);
82  	}
83  
84  	/**
85  	 * Remove a context from the registry. Key is usually a 
86  	 *
87  	 * @param key
88  	 * @return
89  	 */
90  	public List<T> remove(final String key) {
91  		return this.registry.remove(key);
92  	}
93  
94  	/**
95  	 * Returns the number of keys of the registry.
96  	 * 
97  	 * @return Number of keys in the registry
98  	 */
99  	public int size() {
100 		return this.registry.size();
101 	}
102 
103 	@Override
104 	public String toString() {
105 		return this.registry.toString();
106 	}
107 }