1 /**
2 * Copyright 2010 The Kuali Foundation Licensed under the
3 * Educational Community License, Version 2.0 (the "License"); you may
4 * not use this file except in compliance with the License. You may
5 * obtain a copy of the License at
6 *
7 * http://www.osedu.org/licenses/ECL-2.0
8 *
9 * Unless required by applicable law or agreed to in writing,
10 * software distributed under the License is distributed on an "AS IS"
11 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 package org.kuali.student.core.statement.naturallanguage;
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
50 * <@link {@link ReqComponentType} key.
51 *
52 * @param key Context key
53 * @param context Context
54 */
55 public void add(final String key, final T context) {
56 if(this.registry.containsKey(key)) {
57 this.registry.get(key).add(context);
58 } else {
59 List<T> list = new ArrayList<T>();
60 list.add(context);
61 this.registry.put(key, list);
62 }
63 }
64
65 /**
66 * Gets a context from the registry. Key is usually a
67 * <@link {@link ReqComponentType} key.
68 *
69 * @param key Context key
70 * @return A context
71 */
72 public List<T> get(final String key) {
73 return this.registry.get(key);
74 }
75
76 /**
77 * Returns true if a context exists for <code>key</code>; otherwise false.
78 *
79 * @param key Context key
80 * @return True if a context exists otherwise false
81 */
82 public boolean containsKey(final String key) {
83 return this.registry.containsKey(key);
84 }
85
86 /**
87 * Remove a context from the registry. Key is usually a
88 * <@link {@link ReqComponentType} key.
89 *
90 * @param key
91 * @return
92 */
93 public List<T> remove(final String key) {
94 return this.registry.remove(key);
95 }
96
97 /**
98 * Returns the number of keys of the registry.
99 *
100 * @return Number of keys in the registry
101 */
102 public int size() {
103 return this.registry.size();
104 }
105
106 @Override
107 public String toString() {
108 return this.registry.toString();
109 }
110 }