View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.krad.keyvalues;
17  
18  import com.google.common.base.Function;
19  import com.google.common.collect.Collections2;
20  import com.google.common.collect.ImmutableList;
21  import com.google.common.collect.ImmutableMap;
22  import org.kuali.rice.core.api.util.ConcreteKeyValue;
23  import org.kuali.rice.core.api.util.KeyValue;
24  
25  import java.util.Collection;
26  import java.util.List;
27  import java.util.Map;
28  
29  /** a factory for creating key-values finders. */
30  public final class KeyValuesFinderFactory {
31      private KeyValuesFinderFactory() {
32          throw new UnsupportedOperationException("do not call");
33      }
34  
35      public static KeyValuesFinder fromMap(Map<String, String> map) {
36          if (map == null) {
37              throw new IllegalArgumentException("map is null");
38          }
39  
40          return new MapBased(map);
41      }
42  
43      private static final class MapBased implements KeyValuesFinder {
44          private final Map<String, String> map;
45  
46          private MapBased(Map<String, String> map) {
47              this.map = ImmutableMap.copyOf(map);
48          }
49  
50          @Override
51          public List<KeyValue> getKeyValues() {
52              Collection<KeyValue> kvs = Collections2.transform(map.entrySet(), new Function<Map.Entry<String, String>, KeyValue>() {
53                  @Override
54                  public KeyValue apply(Map.Entry<String, String> input) {
55                      return new ConcreteKeyValue(input);
56                  }
57              });
58              return ImmutableList.copyOf(kvs);
59          }
60  
61          @Override
62          public List<KeyValue> getKeyValues(boolean includeActiveOnly) {
63              return getKeyValues();
64          }
65  
66          @Override
67          public Map<String, String> getKeyLabelMap() {
68              return map;
69          }
70  
71          @Override
72          public String getKeyLabel(String key) {
73              return map.get(key);
74          }
75  
76          @Override
77          public void clearInternalCache() {
78              //do nothing
79          }
80      }
81  }