com.cnd.greencube.web.base.EnumConverter.java Source code

Java tutorial

Introduction

Here is the source code for com.cnd.greencube.web.base.EnumConverter.java

Source

/*
 * Copyright 2005-2020 Top Team All rights reserved.
 * Support: 
 * License: top team license
 */
package com.cnd.greencube.web.base;

import org.apache.commons.beanutils.converters.AbstractConverter;

/**
 * ?
 * 
 * @author Top Team 
 * @version 1.0
 */
public class EnumConverter extends AbstractConverter {

    /**  */
    private final Class<?> enumClass;

    /**
     * @param enumClass
     *            
     */
    public EnumConverter(Class<?> enumClass) {
        this(enumClass, null);
    }

    /**
     * @param enumClass
     *            
     * @param defaultValue
     *            
     */
    public EnumConverter(Class<?> enumClass, Object defaultValue) {
        super(defaultValue);
        this.enumClass = enumClass;
    }

    /**
     * ?
     * 
     * @return 
     */
    @Override
    protected Class<?> getDefaultType() {
        return this.enumClass;
    }

    /**
     * ?
     * 
     * @param type
     *            
     * @param value
     *            
     * @return 
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    protected Object convertToType(Class type, Object value) {
        String stringValue = value.toString().trim();
        return Enum.valueOf(type, stringValue);
    }

    /**
     * ?
     * 
     * @param value
     *            
     * @return 
     */
    protected String convertToString(Object value) {
        return value.toString();
    }

}