001 /**
002 * Copyright 2005-2014 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 com.google.common.base.Function;
019 import com.google.common.collect.Collections2;
020 import com.google.common.collect.ImmutableList;
021 import com.google.common.collect.ImmutableMap;
022 import org.kuali.rice.core.api.util.ConcreteKeyValue;
023 import org.kuali.rice.core.api.util.KeyValue;
024
025 import java.util.Collection;
026 import java.util.List;
027 import java.util.Map;
028
029 /** a factory for creating key-values finders. */
030 public final class KeyValuesFinderFactory {
031 private KeyValuesFinderFactory() {
032 throw new UnsupportedOperationException("do not call");
033 }
034
035 public static KeyValuesFinder fromMap(Map<String, String> map) {
036 if (map == null) {
037 throw new IllegalArgumentException("map is null");
038 }
039
040 return new MapBased(map);
041 }
042
043 private static final class MapBased implements KeyValuesFinder {
044 private final Map<String, String> map;
045
046 private MapBased(Map<String, String> map) {
047 this.map = ImmutableMap.copyOf(map);
048 }
049
050 @Override
051 public List<KeyValue> getKeyValues() {
052 Collection<KeyValue> kvs = Collections2.transform(map.entrySet(), new Function<Map.Entry<String, String>, KeyValue>() {
053 @Override
054 public KeyValue apply(Map.Entry<String, String> input) {
055 return new ConcreteKeyValue(input);
056 }
057 });
058 return ImmutableList.copyOf(kvs);
059 }
060
061 @Override
062 public List<KeyValue> getKeyValues(boolean includeActiveOnly) {
063 return getKeyValues();
064 }
065
066 @Override
067 public Map<String, String> getKeyLabelMap() {
068 return map;
069 }
070
071 @Override
072 public String getKeyLabel(String key) {
073 return map.get(key);
074 }
075
076 @Override
077 public void clearInternalCache() {
078 //do nothing
079 }
080 }
081 }