KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > core > enums > AbstractCachingLabeledEnumResolver


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
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
17 package org.springframework.core.enums;
18
19 import java.util.Collections JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.TreeSet JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29 import org.springframework.util.Assert;
30 import org.springframework.util.CachingMapDecorator;
31
32 /**
33  * Abstract base class for LabeledEnumResolver implementations,
34  * caching all retrieved LabeledEnum instances.
35  *
36  * <p>Subclasses need to implement the template method
37  * <code>findLabeledEnums(type)</code>.
38  *
39  * @author Keith Donald
40  * @author Juergen Hoeller
41  * @since 1.2.2
42  * @see #findLabeledEnums(Class)
43  */

44 public abstract class AbstractCachingLabeledEnumResolver implements LabeledEnumResolver {
45
46     protected transient final Log logger = LogFactory.getLog(getClass());
47
48
49     private CachingMapDecorator labeledEnumCache = new CachingMapDecorator() {
50         protected Object JavaDoc create(Object JavaDoc key) {
51             Set JavaDoc typeEnums = findLabeledEnums((Class JavaDoc) key);
52             if (typeEnums == null || typeEnums.isEmpty()) {
53                 throw new IllegalArgumentException JavaDoc(
54                         "Unsupported labeled enumeration type '" + key + "': "
55                         + "make sure you've properly defined this enumeration: "
56                         + "if it's static, are the class and its fields public/static/final?");
57             }
58             Map JavaDoc typeEnumMap = new HashMap JavaDoc(typeEnums.size());
59             for (Iterator JavaDoc it = typeEnums.iterator(); it.hasNext();) {
60                 LabeledEnum labeledEnum = (LabeledEnum) it.next();
61                 typeEnumMap.put(labeledEnum.getCode(), labeledEnum);
62             }
63             return Collections.unmodifiableMap(typeEnumMap);
64         }
65     };
66
67
68     public Set JavaDoc getLabeledEnumSet(Class JavaDoc type) throws IllegalArgumentException JavaDoc {
69         return new TreeSet JavaDoc(getLabeledEnumMap(type).values());
70     }
71
72     public Map JavaDoc getLabeledEnumMap(Class JavaDoc type) throws IllegalArgumentException JavaDoc {
73         Assert.notNull(type, "No type specified");
74         return (Map JavaDoc) this.labeledEnumCache.get(type);
75     }
76
77     public LabeledEnum getLabeledEnumByCode(Class JavaDoc type, Comparable JavaDoc code) throws IllegalArgumentException JavaDoc {
78         Assert.notNull(code, "No enum code specified");
79         Map JavaDoc typeEnums = getLabeledEnumMap(type);
80         LabeledEnum codedEnum = (LabeledEnum) typeEnums.get(code);
81         if (codedEnum == null) {
82             throw new IllegalArgumentException JavaDoc(
83                     "No enumeration with code '" + code + "'" + " of type [" + type.getName()
84                     + "] exists: this is likely a configuration error; "
85                     + "make sure the code value matches a valid instance's code property");
86         }
87         return codedEnum;
88     }
89
90     public LabeledEnum getLabeledEnumByLabel(Class JavaDoc type, String JavaDoc label) throws IllegalArgumentException JavaDoc {
91         Map JavaDoc typeEnums = getLabeledEnumMap(type);
92         Iterator JavaDoc it = typeEnums.values().iterator();
93         while (it.hasNext()) {
94             LabeledEnum value = (LabeledEnum) it.next();
95             if (value.getLabel().equalsIgnoreCase(label)) {
96                 return value;
97             }
98         }
99         throw new IllegalArgumentException JavaDoc(
100                 "No enumeration with label '" + label + "' of type [" + type
101                 + "] exists: this is likely a configuration error; "
102                 + "make sure the label string matches a valid instance's label property");
103     }
104
105
106     /**
107      * Template method to be implemented by subclasses.
108      * Supposed to find all LabeledEnum instances for the given type.
109      * @param type the enum type
110      * @return the Set of LabeledEnum instances
111      * @see org.springframework.core.enums.LabeledEnum
112      */

113     protected abstract Set JavaDoc findLabeledEnums(Class JavaDoc type);
114
115 }
116
Popular Tags