001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.krms.impl.repository.language; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023 024/** 025 * This class is a registry of template contexts which the requirement 026 * component translator uses to generate natural language. 027 */ 028public class ContextRegistry<T extends Context> { 029 030 /** Registry context map */ 031 private Map<String, List<T>> registry = new HashMap<String, List<T>>(); 032 033 /** 034 * Constructor. 035 */ 036 public ContextRegistry() { 037 } 038 039 /** 040 * Constructor. Adds a context registry as a map. 041 * 042 * @param registry Context registry 043 */ 044 public ContextRegistry(final Map<String, List<T>> registry) { 045 this.registry = registry; 046 } 047 048 /** 049 * Adds a context to the registry. Key is usually a TermParameterType key. 050 * 051 * @param key Context key 052 * @param context Context 053 */ 054 public void add(final String key, final T context) { 055 if(this.registry.containsKey(key)) { 056 this.registry.get(key).add(context); 057 } else { 058 List<T> list = new ArrayList<T>(); 059 list.add(context); 060 this.registry.put(key, list); 061 } 062 } 063 064 /** 065 * Gets a context from the registry. Key is usually a TermParameterType key. 066 * 067 * @param key Context key 068 * @return A context 069 */ 070 public List<T> get(final String key) { 071 return this.registry.get(key); 072 } 073 074 /** 075 * Returns true if a context exists for <code>key</code>; otherwise false. 076 * 077 * @param key Context key 078 * @return True if a context exists otherwise false 079 */ 080 public boolean containsKey(final String key) { 081 return this.registry.containsKey(key); 082 } 083 084 /** 085 * Remove a context from the registry. Key is usually a 086 * 087 * @param key 088 * @return 089 */ 090 public List<T> remove(final String key) { 091 return this.registry.remove(key); 092 } 093 094 /** 095 * Returns the number of keys of the registry. 096 * 097 * @return Number of keys in the registry 098 */ 099 public int size() { 100 return this.registry.size(); 101 } 102 103 @Override 104 public String toString() { 105 return this.registry.toString(); 106 } 107}