1 /* 2 * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH 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 package de.smartics.properties.spi.core.value; 17 18 import java.lang.reflect.Method; 19 import java.lang.reflect.Modifier; 20 21 /** 22 * Translates enumeration values from their string representation. If the 23 * enumeration provides a static fromString method, this method will be used. 24 * Otherwise the name of the enumeration element is used for the translation 25 * task. 26 * 27 * @param <T> the type of the enumeration translated by this helper. 28 */ 29 final class EnumIdTranslator<T extends Enum<T>> 30 { 31 // ********************************* Fields ********************************* 32 33 // --- constants ------------------------------------------------------------ 34 35 // --- members -------------------------------------------------------------- 36 37 /** 38 * The wrapped enumeration type. 39 */ 40 private final Class<T> enumType; 41 42 /** 43 * The method used to create an enumeration element from its string 44 * representation. The value is <code>null</code>, if no static 45 * <code>fromString</code> method is provided. 46 */ 47 private final Method fromStringMethod; 48 49 // ****************************** Initializer ******************************* 50 51 // ****************************** Constructors ****************************** 52 53 EnumIdTranslator(final Class<T> enumType) 54 { 55 this.enumType = enumType; 56 this.fromStringMethod = calcMethod(enumType); 57 } 58 59 // ****************************** Inner Classes ***************************** 60 61 // ********************************* Methods ******************************** 62 63 // --- init ----------------------------------------------------------------- 64 65 private static Method calcMethod(final Class<?> enumType) 66 { 67 try 68 { 69 final Method method = enumType.getMethod("fromString", String.class); 70 if (Modifier.isStatic(method.getModifiers())) 71 { 72 return method; 73 } 74 } 75 catch (final Exception e) 76 { 77 // return null at the end. 78 } 79 return null; 80 } 81 82 // --- get&set -------------------------------------------------------------- 83 84 // --- business ------------------------------------------------------------- 85 86 @SuppressWarnings("unchecked") 87 T fromString(final String valueAsString) 88 { 89 if (fromStringMethod != null) 90 { 91 try 92 { 93 final T value = (T) fromStringMethod.invoke(enumType, valueAsString); 94 return value; 95 } 96 catch (final Exception e) 97 { 98 // return enum valueOf at the end. 99 } 100 } 101 return Enum.valueOf(enumType, valueAsString); 102 } 103 104 // --- object basics -------------------------------------------------------- 105 106 }