Package com.khorn.terraincontrol.configuration.settingType

Source Code of com.khorn.terraincontrol.configuration.settingType.EnumSetting

package com.khorn.terraincontrol.configuration.settingType;

import com.khorn.terraincontrol.exception.InvalidConfigException;

/**
* Reads and writes values of the given enum type.
*
* <p>Valid values are those defined by the given enum type. Values are case
* insensitive, all {@link Enum#name() names} of the values in the enum are
* read and compared to the given value. Values are written using the
* {@link Enum#toString() toString method on the Enum class}.
*
* @param <T> The enum type.
*/
class EnumSetting<T extends Enum<T>> extends Setting<T>
{
    private final T defaultValue;
    private final T[] enumValues;

    public EnumSetting(String name, T defaultValue)
    {
        super(name);
        this.defaultValue = defaultValue;
        this.enumValues = defaultValue.getDeclaringClass().getEnumConstants();
    }

    @Override
    public T getDefaultValue()
    {
        return defaultValue;
    }

    @Override
    public T read(String string) throws InvalidConfigException
    {
        for (T enumValue : enumValues)
        {
            if (enumValue.name().equalsIgnoreCase(string))
            {
                return enumValue;
            }
        }
        throw new InvalidConfigException(string + " is not an acceptable value");
    }

}
TOP

Related Classes of com.khorn.terraincontrol.configuration.settingType.EnumSetting

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.