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

org.supercsv.ext.cellprocessor.FormatEnum Maven / Gradle / Ivy

Go to download

CSVのJavaライブラリであるSuperCSVに、アノテーション機能を追加したライブラリです。

There is a newer version: 2.3
Show newest version
package org.supercsv.ext.cellprocessor;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.supercsv.cellprocessor.CellProcessorAdaptor;
import org.supercsv.cellprocessor.ift.StringCellProcessor;
import org.supercsv.exception.SuperCsvCellProcessorException;
import org.supercsv.ext.cellprocessor.ift.ValidationCellProcessor;
import org.supercsv.util.CsvContext;


/**
 *
 * @version 1.2
 * @author T.TSUCHIE
 *
 */
public class FormatEnum extends CellProcessorAdaptor implements ValidationCellProcessor {
    
    protected final Class> type;
    
    protected final Map, String> enumValueMap;
    
    protected final Method valueMethod;
    
    public > FormatEnum(final Class type) {
        super();
        checkPreconditions(type);
        this.type = type;
        this.enumValueMap = createEnumMap(type);
        this.valueMethod = null;
    }
    
    public > FormatEnum(final Class type, final StringCellProcessor next) {
        super(next);
        checkPreconditions(type);
        this.type = type;
        this.enumValueMap = createEnumMap(type);
        this.valueMethod = null;
    }
    
    public > FormatEnum(final Class type, final String valueMethodName) {
        super();
        checkPreconditions(type);
        this.type = type;
        this.enumValueMap = createEnumMap(type, valueMethodName);
        this.valueMethod = getEnumValueMethod(type, valueMethodName);
    }
    
    public > FormatEnum(final Class type, final String valueMethodName, final StringCellProcessor next) {
        super(next);
        checkPreconditions(type);
        this.type = type;
        this.enumValueMap = createEnumMap(type, valueMethodName);
        this.valueMethod = getEnumValueMethod(type, valueMethodName);
    }
    
    protected static void checkPreconditions(final Class type) {
        
        if(type == null) {
            throw new NullPointerException("type should be not null");
        }
    }
    
    protected > Method getEnumValueMethod(final Class enumClass, final String valueMethodName) {
        try {
            final Method method =  enumClass.getMethod(valueMethodName);
            method.setAccessible(true);
            return method;
            
        } catch (ReflectiveOperationException e) {
            throw new IllegalArgumentException(String.format("not found method '%s'", valueMethodName), e);
        }
        
    }
    
    protected > Map, String> createEnumMap(final Class enumClass) {
        
        EnumSet set = EnumSet.allOf(enumClass);
        
        final Map, String> map = new LinkedHashMap<>();
        for(T e : set) {
            map.put(e, e.name());            
            
        }
        
        return Collections.unmodifiableMap(map);
    }
    
    protected > Map, String> createEnumMap(final Class enumClass, final String methodName) {
        
        final Method method = getEnumValueMethod(enumClass, methodName);
        
        final Map, String> map = new LinkedHashMap<>();
        try {
            final EnumSet set = EnumSet.allOf(enumClass);
            for(T e : set) {
                Object returnValue = method.invoke(e);
                map.put(e, returnValue.toString());            
                
            }
            
        } catch(ReflectiveOperationException e) {
            throw new RuntimeException("fail get enum value.", e);
        }
        
        return Collections.unmodifiableMap(map);
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public Object execute(final Object value, final CsvContext context) {
        
        validateInputNotNull(value, context);
        
        if(!value.getClass().isAssignableFrom(type)) {
            throw new SuperCsvCellProcessorException(type, value, context, this);
        }
        
        final String result = getEnumValueMap().get((Enum)value);
        return next.execute(result, context);
    }
    
    public Class getType() {
        return type;
    }
    
    public Map, String> getEnumValueMap() {
        return enumValueMap;
    }
    
    public Method getValueMethod() {
        return valueMethod;
    }
    
    @Override
    public Map getMessageVariable() {
        final Map vars = new HashMap();
        vars.put("type", getType().getCanonicalName());
        vars.put("valueMethod", getValueMethod() == null ? "" : getValueMethod().getName());
        
        final List> enumValues = getEnumValueMap().entrySet().stream()
                .map(e -> e.getKey())
                .collect(Collectors.toList());
        
        final String enumsStr = getEnumValueMap().entrySet().stream()
                .map(e -> e.getValue())
                .collect(Collectors.joining(", "));
        
        vars.put("enumValues", enumValues);
        vars.put("enumsStr", enumsStr);
        
        return vars;
    }
    
    @Override
    public String formatValue(final Object value) {
        if(value == null) {
            return "";
        }
        
        if(value.getClass().isAssignableFrom(type)) {
            final Enum enumValue = (Enum) value;
            return getEnumValueMap().get(enumValue);
        }
        
        return value.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy