Juergen Hoeller
12 years ago
11 changed files with 0 additions and 821 deletions
@ -1,139 +0,0 @@
@@ -1,139 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.enums; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
import java.util.TreeSet; |
||||
|
||||
import org.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
|
||||
import org.springframework.util.Assert; |
||||
import org.springframework.util.CachingMapDecorator; |
||||
import org.springframework.util.ClassUtils; |
||||
|
||||
/** |
||||
* Abstract base class for {@link LabeledEnumResolver} implementations, |
||||
* caching all retrieved {@link LabeledEnum} instances. |
||||
* |
||||
* <p>Subclasses need to implement the template method |
||||
* {@link #findLabeledEnums(Class)}. |
||||
* |
||||
* @author Keith Donald |
||||
* @author Juergen Hoeller |
||||
* @since 1.2.2 |
||||
* @see #findLabeledEnums(Class) |
||||
* @deprecated as of Spring 3.0, in favor of Java 5 enums. |
||||
*/ |
||||
@Deprecated |
||||
public abstract class AbstractCachingLabeledEnumResolver implements LabeledEnumResolver { |
||||
|
||||
protected transient final Log logger = LogFactory.getLog(getClass()); |
||||
|
||||
private final LabeledEnumCache labeledEnumCache = new LabeledEnumCache(); |
||||
|
||||
|
||||
public Set<LabeledEnum> getLabeledEnumSet(Class type) throws IllegalArgumentException { |
||||
return new TreeSet<LabeledEnum>(getLabeledEnumMap(type).values()); |
||||
} |
||||
|
||||
public Map<Comparable, LabeledEnum> getLabeledEnumMap(Class type) throws IllegalArgumentException { |
||||
Assert.notNull(type, "No type specified"); |
||||
return this.labeledEnumCache.get(type); |
||||
} |
||||
|
||||
public LabeledEnum getLabeledEnumByCode(Class type, Comparable code) throws IllegalArgumentException { |
||||
Assert.notNull(code, "No enum code specified"); |
||||
Map<Comparable, LabeledEnum> typeEnums = getLabeledEnumMap(type); |
||||
LabeledEnum codedEnum = typeEnums.get(code); |
||||
if (codedEnum == null) { |
||||
throw new IllegalArgumentException( |
||||
"No enumeration with code '" + code + "'" + " of type [" + type.getName() + |
||||
"] exists: this is likely a configuration error. " + |
||||
"Make sure the code value matches a valid instance's code property!"); |
||||
} |
||||
return codedEnum; |
||||
} |
||||
|
||||
public LabeledEnum getLabeledEnumByLabel(Class type, String label) throws IllegalArgumentException { |
||||
Map<Comparable, LabeledEnum> typeEnums = getLabeledEnumMap(type); |
||||
for (LabeledEnum value : typeEnums.values()) { |
||||
if (value.getLabel().equalsIgnoreCase(label)) { |
||||
return value; |
||||
} |
||||
} |
||||
throw new IllegalArgumentException( |
||||
"No enumeration with label '" + label + "' of type [" + type + |
||||
"] exists: this is likely a configuration error. " + |
||||
"Make sure the label string matches a valid instance's label property!"); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Template method to be implemented by subclasses. |
||||
* Supposed to find all LabeledEnum instances for the given type. |
||||
* @param type the enum type |
||||
* @return the Set of LabeledEnum instances |
||||
* @see org.springframework.core.enums.LabeledEnum |
||||
*/ |
||||
protected abstract Set<LabeledEnum> findLabeledEnums(Class type); |
||||
|
||||
|
||||
/** |
||||
* Inner cache class that implements lazy building of LabeledEnum Maps. |
||||
*/ |
||||
@SuppressWarnings("serial") |
||||
private class LabeledEnumCache extends CachingMapDecorator<Class, Map<Comparable, LabeledEnum>> { |
||||
|
||||
public LabeledEnumCache() { |
||||
super(true); |
||||
} |
||||
|
||||
@Override |
||||
protected Map<Comparable, LabeledEnum> create(Class key) { |
||||
Set<LabeledEnum> typeEnums = findLabeledEnums(key); |
||||
if (typeEnums == null || typeEnums.isEmpty()) { |
||||
throw new IllegalArgumentException( |
||||
"Unsupported labeled enumeration type '" + key + "': " + |
||||
"make sure you've properly defined this enumeration! " + |
||||
"If it is static, are the class and its fields public/static/final?"); |
||||
} |
||||
Map<Comparable, LabeledEnum> typeEnumMap = new HashMap<Comparable, LabeledEnum>(typeEnums.size()); |
||||
for (LabeledEnum labeledEnum : typeEnums) { |
||||
typeEnumMap.put(labeledEnum.getCode(), labeledEnum); |
||||
} |
||||
return Collections.unmodifiableMap(typeEnumMap); |
||||
} |
||||
|
||||
@Override |
||||
protected boolean useWeakValue(Class key, Map<Comparable, LabeledEnum> value) { |
||||
if (!ClassUtils.isCacheSafe(key, AbstractCachingLabeledEnumResolver.this.getClass().getClassLoader())) { |
||||
if (logger != null && logger.isDebugEnabled()) { |
||||
logger.debug("Not strongly caching class [" + key.getName() + "] because it is not cache-safe"); |
||||
} |
||||
return true; |
||||
} |
||||
else { |
||||
return false; |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
@ -1,55 +0,0 @@
@@ -1,55 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.enums; |
||||
|
||||
/** |
||||
* Base class for labeled enum instances that aren't static. |
||||
* |
||||
* @author Keith Donald |
||||
* @since 1.2.6 |
||||
* @deprecated as of Spring 3.0, in favor of Java 5 enums. |
||||
*/ |
||||
@Deprecated |
||||
@SuppressWarnings("serial") |
||||
public abstract class AbstractGenericLabeledEnum extends AbstractLabeledEnum { |
||||
|
||||
/** |
||||
* A descriptive label for the enum. |
||||
*/ |
||||
private final String label; |
||||
|
||||
|
||||
/** |
||||
* Create a new StaticLabeledEnum instance. |
||||
* @param label the label; if {@code null}), the enum's code |
||||
* will be used as label |
||||
*/ |
||||
protected AbstractGenericLabeledEnum(String label) { |
||||
this.label = label; |
||||
} |
||||
|
||||
|
||||
public String getLabel() { |
||||
if (this.label != null) { |
||||
return label; |
||||
} |
||||
else { |
||||
return getCode().toString(); |
||||
} |
||||
} |
||||
|
||||
} |
@ -1,77 +0,0 @@
@@ -1,77 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.enums; |
||||
|
||||
/** |
||||
* Abstract base superclass for LabeledEnum implementations. |
||||
* |
||||
* @author Keith Donald |
||||
* @author Juergen Hoeller |
||||
* @author Sam Brannen |
||||
* @since 1.2.2 |
||||
* @deprecated as of Spring 3.0, in favor of Java 5 enums. |
||||
*/ |
||||
@Deprecated |
||||
@SuppressWarnings("serial") |
||||
public abstract class AbstractLabeledEnum implements LabeledEnum { |
||||
|
||||
/** |
||||
* Create a new AbstractLabeledEnum instance. |
||||
*/ |
||||
protected AbstractLabeledEnum() { |
||||
} |
||||
|
||||
public Class getType() { |
||||
// Could be coded as getClass().isAnonymousClass() on JDK 1.5
|
||||
boolean isAnonymous = (getClass().getDeclaringClass() == null && getClass().getName().indexOf('$') != -1); |
||||
return (isAnonymous ? getClass().getSuperclass() : getClass()); |
||||
} |
||||
|
||||
public int compareTo(Object obj) { |
||||
if (!(obj instanceof LabeledEnum)) { |
||||
throw new ClassCastException("You may only compare LabeledEnums"); |
||||
} |
||||
LabeledEnum that = (LabeledEnum) obj; |
||||
if (!this.getType().equals(that.getType())) { |
||||
throw new ClassCastException("You may only compare LabeledEnums of the same type"); |
||||
} |
||||
return this.getCode().compareTo(that.getCode()); |
||||
} |
||||
|
||||
@Override |
||||
public boolean equals(Object obj) { |
||||
if (this == obj) { |
||||
return true; |
||||
} |
||||
if (!(obj instanceof LabeledEnum)) { |
||||
return false; |
||||
} |
||||
LabeledEnum other = (LabeledEnum) obj; |
||||
return (this.getType().equals(other.getType()) && this.getCode().equals(other.getCode())); |
||||
} |
||||
|
||||
@Override |
||||
public int hashCode() { |
||||
return (getType().hashCode() * 29 + getCode().hashCode()); |
||||
} |
||||
|
||||
@Override |
||||
public String toString() { |
||||
return getLabel(); |
||||
} |
||||
|
||||
} |
@ -1,96 +0,0 @@
@@ -1,96 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.enums; |
||||
|
||||
import java.io.Serializable; |
||||
import java.util.Comparator; |
||||
|
||||
import org.springframework.util.comparator.CompoundComparator; |
||||
import org.springframework.util.comparator.NullSafeComparator; |
||||
|
||||
/** |
||||
* An interface for objects that represent a labeled enumeration. |
||||
* Each such enum instance has the following characteristics: |
||||
* |
||||
* <ul> |
||||
* <li>A type that identifies the enum's class. |
||||
* For example: {@code com.mycompany.util.FileFormat}.</li> |
||||
* |
||||
* <li>A code that uniquely identifies the enum within the context of its type. |
||||
* For example: "CSV". Different classes of codes are possible |
||||
* (e.g., Character, Integer, String).</li> |
||||
* |
||||
* <li>A descriptive label. For example: "the CSV File Format".</li> |
||||
* </ul> |
||||
* |
||||
* @author Keith Donald |
||||
* @since 1.2.2 |
||||
* @deprecated as of Spring 3.0, in favor of Java 5 enums. |
||||
*/ |
||||
@Deprecated |
||||
public interface LabeledEnum extends Comparable, Serializable { |
||||
|
||||
/** |
||||
* Return this enumeration's type. |
||||
*/ |
||||
Class getType(); |
||||
|
||||
/** |
||||
* Return this enumeration's code. |
||||
* <p>Each code should be unique within enumerations of the same type. |
||||
*/ |
||||
Comparable getCode(); |
||||
|
||||
/** |
||||
* Return a descriptive, optional label. |
||||
*/ |
||||
String getLabel(); |
||||
|
||||
|
||||
// Constants for standard enum ordering (Comparator implementations)
|
||||
|
||||
/** |
||||
* Shared Comparator instance that sorts enumerations by {@code CODE_ORDER}. |
||||
*/ |
||||
Comparator CODE_ORDER = new Comparator() { |
||||
public int compare(Object o1, Object o2) { |
||||
Comparable c1 = ((LabeledEnum) o1).getCode(); |
||||
Comparable c2 = ((LabeledEnum) o2).getCode(); |
||||
return c1.compareTo(c2); |
||||
} |
||||
}; |
||||
|
||||
/** |
||||
* Shared Comparator instance that sorts enumerations by {@code LABEL_ORDER}. |
||||
*/ |
||||
Comparator LABEL_ORDER = new Comparator() { |
||||
public int compare(Object o1, Object o2) { |
||||
LabeledEnum e1 = (LabeledEnum) o1; |
||||
LabeledEnum e2 = (LabeledEnum) o2; |
||||
Comparator comp = new NullSafeComparator(String.CASE_INSENSITIVE_ORDER, true); |
||||
return comp.compare(e1.getLabel(), e2.getLabel()); |
||||
} |
||||
}; |
||||
|
||||
/** |
||||
* Shared Comparator instance that sorts enumerations by {@code LABEL_ORDER}, |
||||
* then {@code CODE_ORDER}. |
||||
*/ |
||||
Comparator DEFAULT_ORDER = |
||||
new CompoundComparator(new Comparator[] { LABEL_ORDER, CODE_ORDER }); |
||||
|
||||
} |
@ -1,71 +0,0 @@
@@ -1,71 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.enums; |
||||
|
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
|
||||
/** |
||||
* Interface for looking up {@code LabeledEnum} instances. |
||||
* |
||||
* @author Keith Donald |
||||
* @author Juergen Hoeller |
||||
* @since 1.2.2 |
||||
* @deprecated as of Spring 3.0, in favor of Java 5 enums. |
||||
*/ |
||||
@Deprecated |
||||
public interface LabeledEnumResolver { |
||||
|
||||
/** |
||||
* Return a set of enumerations of a particular type. Each element in the |
||||
* set should be an instance of LabeledEnum. |
||||
* @param type the enum type |
||||
* @return a set of localized enumeration instances for the provided type |
||||
* @throws IllegalArgumentException if the type is not supported |
||||
*/ |
||||
public Set getLabeledEnumSet(Class type) throws IllegalArgumentException; |
||||
|
||||
/** |
||||
* Return a map of enumerations of a particular type. Each element in the |
||||
* map should be a key/value pair, where the key is the enum code, and the |
||||
* value is the {@code LabeledEnum} instance. |
||||
* @param type the enum type |
||||
* @return a Map of localized enumeration instances, |
||||
* with enum code as key and {@code LabeledEnum} instance as value |
||||
* @throws IllegalArgumentException if the type is not supported |
||||
*/ |
||||
public Map getLabeledEnumMap(Class type) throws IllegalArgumentException; |
||||
|
||||
/** |
||||
* Resolve a single {@code LabeledEnum} by its identifying code. |
||||
* @param type the enum type |
||||
* @param code the enum code |
||||
* @return the enum |
||||
* @throws IllegalArgumentException if the code did not map to a valid instance |
||||
*/ |
||||
public LabeledEnum getLabeledEnumByCode(Class type, Comparable code) throws IllegalArgumentException; |
||||
|
||||
/** |
||||
* Resolve a single {@code LabeledEnum} by its identifying code. |
||||
* @param type the enum type |
||||
* @param label the enum label |
||||
* @return the enum |
||||
* @throws IllegalArgumentException if the label did not map to a valid instance |
||||
*/ |
||||
public LabeledEnum getLabeledEnumByLabel(Class type, String label) throws IllegalArgumentException; |
||||
|
||||
} |
@ -1,66 +0,0 @@
@@ -1,66 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.enums; |
||||
|
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* Implementation of LabeledEnum which uses a letter as the code type. |
||||
* |
||||
* <p>Should almost always be subclassed, but for some simple situations it may be |
||||
* used directly. Note that you will not be able to use unique type-based functionality |
||||
* like {@code LabeledEnumResolver.getLabeledEnumSet(type)} in this case. |
||||
* |
||||
* @author Keith Donald |
||||
* @since 1.2.2 |
||||
* @deprecated as of Spring 3.0, in favor of Java 5 enums. |
||||
*/ |
||||
@Deprecated |
||||
@SuppressWarnings("serial") |
||||
public class LetterCodedLabeledEnum extends AbstractGenericLabeledEnum { |
||||
|
||||
/** |
||||
* The unique code of this enum. |
||||
*/ |
||||
private final Character code; |
||||
|
||||
|
||||
/** |
||||
* Create a new LetterCodedLabeledEnum instance. |
||||
* @param code the letter code |
||||
* @param label the label (can be {@code null}) |
||||
*/ |
||||
public LetterCodedLabeledEnum(char code, String label) { |
||||
super(label); |
||||
Assert.isTrue(Character.isLetter(code), |
||||
"The code '" + code + "' is invalid: it must be a letter"); |
||||
this.code = new Character(code); |
||||
} |
||||
|
||||
|
||||
public Comparable getCode() { |
||||
return code; |
||||
} |
||||
|
||||
/** |
||||
* Return the letter code of this LabeledEnum instance. |
||||
*/ |
||||
public char getLetterCode() { |
||||
return ((Character) getCode()).charValue(); |
||||
} |
||||
|
||||
} |
@ -1,63 +0,0 @@
@@ -1,63 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.enums; |
||||
|
||||
/** |
||||
* Implementation of LabeledEnum which uses Short as the code type. |
||||
* |
||||
* <p>Should almost always be subclassed, but for some simple situations it may be |
||||
* used directly. Note that you will not be able to use unique type-based functionality |
||||
* like {@code LabeledEnumResolver.getLabeledEnumSet(type)} in this case. |
||||
* |
||||
* @author Keith Donald |
||||
* @since 1.2.2 |
||||
* @deprecated as of Spring 3.0, in favor of Java 5 enums. |
||||
*/ |
||||
@Deprecated |
||||
public class ShortCodedLabeledEnum extends AbstractGenericLabeledEnum { |
||||
|
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
/** |
||||
* The unique code of this enum. |
||||
*/ |
||||
private final Short code; |
||||
|
||||
|
||||
/** |
||||
* Create a new ShortCodedLabeledEnum instance. |
||||
* @param code the short code |
||||
* @param label the label (can be {@code null}) |
||||
*/ |
||||
public ShortCodedLabeledEnum(int code, String label) { |
||||
super(label); |
||||
this.code = new Short((short) code); |
||||
} |
||||
|
||||
|
||||
public Comparable getCode() { |
||||
return code; |
||||
} |
||||
|
||||
/** |
||||
* Return the short code of this LabeledEnum instance. |
||||
*/ |
||||
public short getShortCode() { |
||||
return ((Short) getCode()).shortValue(); |
||||
} |
||||
|
||||
} |
@ -1,103 +0,0 @@
@@ -1,103 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.enums; |
||||
|
||||
/** |
||||
* Base class for static type-safe labeled enum instances. |
||||
* |
||||
* Usage example: |
||||
* |
||||
* <pre> |
||||
* public class FlowSessionStatus extends StaticLabeledEnum { |
||||
* |
||||
* // public static final instances!
|
||||
* public static FlowSessionStatus CREATED = new FlowSessionStatus(0, "Created"); |
||||
* public static FlowSessionStatus ACTIVE = new FlowSessionStatus(1, "Active"); |
||||
* public static FlowSessionStatus PAUSED = new FlowSessionStatus(2, "Paused"); |
||||
* public static FlowSessionStatus SUSPENDED = new FlowSessionStatus(3, "Suspended"); |
||||
* public static FlowSessionStatus ENDED = new FlowSessionStatus(4, "Ended"); |
||||
* |
||||
* // private constructor!
|
||||
* private FlowSessionStatus(int code, String label) { |
||||
* super(code, label); |
||||
* } |
||||
* |
||||
* // custom behavior
|
||||
* }</pre> |
||||
* |
||||
* @author Keith Donald |
||||
* @since 1.2.6 |
||||
* @deprecated as of Spring 3.0, in favor of Java 5 enums. |
||||
*/ |
||||
@Deprecated |
||||
@SuppressWarnings("serial") |
||||
public abstract class StaticLabeledEnum extends AbstractLabeledEnum { |
||||
|
||||
/** |
||||
* The unique code of the enum. |
||||
*/ |
||||
private final Short code; |
||||
|
||||
/** |
||||
* A descriptive label for the enum. |
||||
*/ |
||||
private final transient String label; |
||||
|
||||
|
||||
/** |
||||
* Create a new StaticLabeledEnum instance. |
||||
* @param code the short code |
||||
* @param label the label (can be {@code null}) |
||||
*/ |
||||
protected StaticLabeledEnum(int code, String label) { |
||||
this.code = new Short((short) code); |
||||
if (label != null) { |
||||
this.label = label; |
||||
} |
||||
else { |
||||
this.label = this.code.toString(); |
||||
} |
||||
} |
||||
|
||||
public Comparable getCode() { |
||||
return code; |
||||
} |
||||
|
||||
public String getLabel() { |
||||
return label; |
||||
} |
||||
|
||||
/** |
||||
* Return the code of this LabeledEnum instance as a short. |
||||
*/ |
||||
public short shortValue() { |
||||
return ((Number) getCode()).shortValue(); |
||||
} |
||||
|
||||
|
||||
//---------------------------------------------------------------------
|
||||
// Serialization support
|
||||
//---------------------------------------------------------------------
|
||||
|
||||
/** |
||||
* Return the resolved type safe static enum instance. |
||||
*/ |
||||
protected Object readResolve() { |
||||
return StaticLabeledEnumResolver.instance().getLabeledEnumByCode(getType(), getCode()); |
||||
} |
||||
|
||||
} |
@ -1,75 +0,0 @@
@@ -1,75 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.enums; |
||||
|
||||
import java.lang.reflect.Field; |
||||
import java.lang.reflect.Modifier; |
||||
import java.util.Set; |
||||
import java.util.TreeSet; |
||||
|
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* {@link LabeledEnumResolver} that resolves statically defined enumerations. |
||||
* Static implies all enum instances were defined within Java code, |
||||
* implementing the type-safe enum pattern. |
||||
* |
||||
* @author Keith Donald |
||||
* @author Juergen Hoeller |
||||
* @since 1.2.2 |
||||
* @deprecated as of Spring 3.0, in favor of Java 5 enums. |
||||
*/ |
||||
@Deprecated |
||||
public class StaticLabeledEnumResolver extends AbstractCachingLabeledEnumResolver { |
||||
|
||||
/** |
||||
* Shared {@code StaticLabeledEnumResolver} singleton instance. |
||||
*/ |
||||
private static final StaticLabeledEnumResolver INSTANCE = new StaticLabeledEnumResolver(); |
||||
|
||||
|
||||
/** |
||||
* Return the shared {@code StaticLabeledEnumResolver} singleton instance. |
||||
* Mainly for resolving unique StaticLabeledEnum references on deserialization. |
||||
* @see StaticLabeledEnum |
||||
*/ |
||||
public static StaticLabeledEnumResolver instance() { |
||||
return INSTANCE; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
protected Set<LabeledEnum> findLabeledEnums(Class type) { |
||||
Set<LabeledEnum> typeEnums = new TreeSet<LabeledEnum>(); |
||||
for (Field field : type.getFields()) { |
||||
if (Modifier.isStatic(field.getModifiers()) && Modifier.isPublic(field.getModifiers())) { |
||||
if (type.isAssignableFrom(field.getType())) { |
||||
try { |
||||
Object value = field.get(null); |
||||
Assert.isTrue(value instanceof LabeledEnum, "Field value must be a LabeledEnum instance"); |
||||
typeEnums.add((LabeledEnum) value); |
||||
} |
||||
catch (IllegalAccessException ex) { |
||||
logger.warn("Unable to access field value: " + field, ex); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
return typeEnums; |
||||
} |
||||
|
||||
} |
@ -1,67 +0,0 @@
@@ -1,67 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2012 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.core.enums; |
||||
|
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* Implementation of LabeledEnum which uses a String as the code type. |
||||
* |
||||
* <p>Should almost always be subclassed, but for some simple situations it may be |
||||
* used directly. Note that you will not be able to use unique type-based |
||||
* functionality like {@code LabeledEnumResolver.getLabeledEnumSet(type)} in this case. |
||||
* |
||||
* @author Keith Donald |
||||
* @author Juergen Hoeller |
||||
* @since 1.2.2 |
||||
* @see org.springframework.core.enums.LabeledEnumResolver#getLabeledEnumSet(Class) |
||||
* @deprecated as of Spring 3.0, in favor of Java 5 enums. |
||||
*/ |
||||
@Deprecated |
||||
@SuppressWarnings("serial") |
||||
public class StringCodedLabeledEnum extends AbstractGenericLabeledEnum { |
||||
|
||||
/** |
||||
* The unique code of this enum. |
||||
*/ |
||||
private final String code; |
||||
|
||||
|
||||
/** |
||||
* Create a new StringCodedLabeledEnum instance. |
||||
* @param code the String code |
||||
* @param label the label (can be {@code null}) |
||||
*/ |
||||
public StringCodedLabeledEnum(String code, String label) { |
||||
super(label); |
||||
Assert.notNull(code, "'code' must not be null"); |
||||
this.code = code; |
||||
} |
||||
|
||||
|
||||
public Comparable getCode() { |
||||
return this.code; |
||||
} |
||||
|
||||
/** |
||||
* Return the String code of this LabeledEnum instance. |
||||
*/ |
||||
public String getStringCode() { |
||||
return (String) getCode(); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue