View Javadoc

1   /**
2    * Copyright 2005-2013 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 java.util.Collection;
19  import java.util.List;
20  import java.util.Map;
21  
22  import org.kuali.rice.core.api.util.ConcreteKeyValue;
23  import org.kuali.rice.core.api.util.KeyValue;
24  
25  import com.google.common.base.Function;
26  import com.google.common.collect.Collections2;
27  import com.google.common.collect.ImmutableList;
28  import com.google.common.collect.ImmutableMap;
29  
30  /** a factory for creating key-values finders. */
31  public final class KeyValuesFinderFactory {
32      private KeyValuesFinderFactory() {
33          throw new UnsupportedOperationException("do not call");
34      }
35  
36      public static KeyValuesFinder fromMap(Map<String, String> map) {
37          if (map == null) {
38              throw new IllegalArgumentException("map is null");
39          }
40  
41          return new MapBased(map);
42      }
43  
44      private static final class MapBased implements KeyValuesFinder {
45          private final Map<String, String> map;
46  
47          private MapBased(Map<String, String> map) {
48              this.map = ImmutableMap.copyOf(map);
49          }
50  
51          @Override
52          public List<KeyValue> getKeyValues() {
53              Collection<KeyValue> kvs = Collections2.transform(map.entrySet(), new Function<Map.Entry<String, String>, KeyValue>() {
54                  @Override
55                  public KeyValue apply(Map.Entry<String, String> input) {
56                      return new ConcreteKeyValue(input);
57                  }
58              });
59              return ImmutableList.copyOf(kvs);
60          }
61  
62          @Override
63          public List<KeyValue> getKeyValues(boolean includeActiveOnly) {
64              return getKeyValues();
65          }
66  
67          @Override
68          public Map<String, String> getKeyLabelMap() {
69              return map;
70          }
71  
72          @Override
73          public String getKeyLabel(String key) {
74              return map.get(key);
75          }
76  
77          @Override
78          public void clearInternalCache() {
79              //do nothing
80          }
81      }
82  }