001    /**
002     * Copyright 2005-2012 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.KeyValue;
019    
020    import java.io.Serializable;
021    import java.util.ArrayList;
022    import java.util.Collection;
023    import java.util.Collections;
024    import java.util.HashMap;
025    import java.util.List;
026    import java.util.Map;
027    
028    /**
029     * Abstract base implementation of {@link KeyValuesFinder}
030     *
031     * @author Kuali Rice Team (rice.collab@kuali.org)
032     */
033    public abstract class KeyValuesBase implements KeyValuesFinder, Serializable {
034    
035        public Collection<String> getOptionLabels() {
036            Collection<String> optionLabels = new ArrayList<String>();
037    
038            Collection<KeyValue> keyLabels = getKeyValues();
039            for (KeyValue keyLabel : keyLabels) {
040                    optionLabels.add(keyLabel.getValue());
041            }
042            return optionLabels;
043        }
044    
045        public Collection<String> getOptionValues() {
046            Collection<String> optionValues = new ArrayList<String>();
047    
048            Collection<KeyValue> keyLabels = getKeyValues();
049            for (KeyValue keyLabel : keyLabels) {
050                    optionValues.add(keyLabel.getKey());
051            }
052            return optionValues;
053        }
054    
055        @Override
056            public Map<String, String> getKeyLabelMap() {
057            Map<String, String> keyLabelMap = new HashMap<String, String>();
058    
059            List<KeyValue> keyLabels = getKeyValues();
060            for (KeyValue keyLabel : keyLabels) {
061                    keyLabelMap.put(keyLabel.getKey(), keyLabel.getValue());
062            }
063    
064            return keyLabelMap;
065        }
066    
067        @Override
068            public String getKeyLabel(String key) {
069            Map<String, String> keyLabelMap = getKeyLabelMap();
070    
071            if (keyLabelMap.containsKey(key)) {
072                return keyLabelMap.get(key);
073            }
074            return null;
075        }
076        
077        @Override
078            public List<KeyValue> getKeyValues(boolean includeActiveOnly){
079            return Collections.emptyList();
080        }
081        
082            @Override
083            public void clearInternalCache() {
084                    // do nothing
085            }
086    
087    }