1 /**
2 * Copyright 2005-2014 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.ArrayList;
19 import java.util.List;
20
21 import org.apache.commons.lang.WordUtils;
22 import org.kuali.rice.core.api.util.ConcreteKeyValue;
23 import org.kuali.rice.core.api.util.KeyValue;
24
25 /**
26 * ValuesFinder that derives values directly from a Java enum.
27 * KeyValues are provided in enum definition order, enum name
28 * is the key, capitalized lowercase enum name is the label.
29 */
30 public class EnumValuesFinder extends KeyValuesBase {
31 private Class<? extends Enum> enumeration;
32
33 public EnumValuesFinder(Class<? extends Enum> enumeration) {
34 this.enumeration = enumeration;
35 }
36
37 @Override
38 public List<KeyValue> getKeyValues() {
39 List<KeyValue> labels = new ArrayList<KeyValue>();
40 for (Enum enumval: enumeration.getEnumConstants()) {
41 labels.add(new ConcreteKeyValue(getEnumKey(enumval), getEnumLabel(enumval)));
42 }
43 return labels;
44 }
45
46 /**
47 * Derives a key value from an enum
48 */
49 protected String getEnumKey(Enum enm) {
50 return enm.name();
51 }
52
53 /**
54 * Derives a label value from an enum
55 */
56 protected String getEnumLabel(Enum enm) {
57 return WordUtils.capitalize(enm.name().toLowerCase());
58 }
59 }