001    /**
002     * Copyright 2005-2013 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     */
016    package org.kuali.rice.krad.keyvalues;
017    
018    import org.kuali.rice.core.api.util.ConcreteKeyValue;
019    import org.kuali.rice.core.api.util.KeyValue;
020    
021    import java.util.ArrayList;
022    import java.util.Collections;
023    import java.util.HashMap;
024    import java.util.List;
025    import java.util.Map;
026    
027    /** a simple values finder that uses a map as the key/value source. */
028    public class MapValuesFinder implements KeyValuesFinder {
029    
030        private Map<String, String> keyValues;
031    
032        public MapValuesFinder(Map<String, String> keyValues) {
033            setKeyValues(keyValues);
034        }
035    
036        public void setKeyValues(Map<String, String> keyValues) {
037            if (keyValues == null) {
038                throw new IllegalArgumentException("keyValues was null");
039            }
040            this.keyValues = Collections.unmodifiableMap(new HashMap<String, String>(keyValues));
041        }
042    
043        @Override
044        public List<KeyValue> getKeyValues() {
045            final List<KeyValue> list = new ArrayList<KeyValue>();
046            for (Map.Entry<String, String> entry : keyValues.entrySet()) {
047                list.add(new ConcreteKeyValue(entry));
048            }
049            return Collections.unmodifiableList(list);
050        }
051    
052        @Override
053        public List<KeyValue> getKeyValues(boolean includeActiveOnly) {
054            return getKeyValues();
055        }
056    
057        @Override
058        public Map<String, String> getKeyLabelMap() {
059            return keyValues;
060        }
061    
062        @Override
063        public String getKeyLabel(String key) {
064            return keyValues.get(key);
065        }
066    
067        @Override
068        public void clearInternalCache() {
069            //do nothing
070        }
071    }