All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.jsefa.common.converter.EnumConverter Maven / Gradle / Ivy

Go to download

JSefa (Java Simple exchange format api) is a simple library for stream-based serialization of java objects to XML, CSV, FLR or any other format and back again using an iterator-style interface independent of the serialization format. The mapping between java object types and types of the serialization format (e. g. xml complex element types) can be defined either by annotating the java classes or programmatically using a simple API. The current implementation supports XML, CSV and FLR - for XML it is based on JSR 173.

The newest version!
/*
 * Copyright 2007 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 net.sf.jsefa.common.converter;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import net.sf.jsefa.common.util.ReflectionUtil;

/**
 * Converter for JDK 1.5 enums. 
* The format is an array of mappings from the original String representation of an enum value to * the one which shall be used for serialization. Each mapping has the form
* originalRepresentation=newRepresentation
* Example: {"SENIOR_DEVELOPER=senior developer", "JUNIOR_DEVELOPER=junior developer"} *

* These mappings override the display name declarations provided by the {@link EnumConstant} annotations. *

* It is thread safe. * * @author Norman Lahme-Huetig * */ public final class EnumConverter implements SimpleTypeConverter { @SuppressWarnings("unchecked") private final Class enumType; private final Map nameToAliasMap; private final Map aliasToNameMap; /** * Constructs a new EnumConverter. * * @param configuration the configuration * @return a enum converter */ public static EnumConverter create(SimpleTypeConverterConfiguration configuration) { Class> enumType = configuration.getObjectType(); Map nameToAliasMap = new HashMap(); for (Field field : ReflectionUtil.getAllFields(enumType)) { if (field.isAnnotationPresent(EnumConstant.class)) { nameToAliasMap.put(field.getName(), field.getAnnotation(EnumConstant.class).value()); } } if (configuration.getFormat() != null) { for (String mapping : configuration.getFormat()) { String[] tokens = mapping.split("="); nameToAliasMap.put(tokens[0], tokens[1]); } } return new EnumConverter(enumType, nameToAliasMap); } private EnumConverter(Class> enumType, Map nameToAliasMap) { this.enumType = enumType; this.nameToAliasMap = nameToAliasMap; this.aliasToNameMap = new HashMap(); for (Entry entry : this.nameToAliasMap.entrySet()) { this.aliasToNameMap.put(entry.getValue(), entry.getKey()); } } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") public Enum fromString(String value) { if (value == null || value.length() == 0) { return null; } String name = this.aliasToNameMap.get(value); if (name != null) { return Enum.valueOf(this.enumType, name); } else { return Enum.valueOf(this.enumType, value); } } /** * {@inheritDoc} */ @SuppressWarnings("unchecked") public String toString(Object value) { if (value == null) { return null; } String name = ((Enum) value).name(); String alias = this.nameToAliasMap.get(name); if (alias != null) { return alias; } else { return name; } } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy