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

com.github.lontime.exthttp.common.ConfigKey Maven / Gradle / Ivy

package com.github.lontime.exthttp.common;

import java.util.StringJoiner;

import com.github.lontime.base.commonj.utils.StringHelper;

/**
 * ConfigKeyEnum.
 * @author lontime
 * @since 1.0
 */
public class ConfigKey {
    private final String key;
    private final Type type;
    private final Object defaultValue;

    private ConfigKey(String name, Type type, Object defaultValue) {
        this.key = name;
        this.type = type;
        this.defaultValue = defaultValue;
    }

    public String getKey() {
        return key;
    }

    public Type getType() {
        return type;
    }

    public Object getDefaultValue() {
        return defaultValue;
    }

    public Object parseValue(String str) {
        if (StringHelper.isEmpty(str)) {
            return getDefaultValue();
        }
        if (getType() == Type.BOOLEAN || getType() == Type.BOOL) {
            return Boolean.valueOf(str);
        }
        if (getType() == Type.INT) {
            return Integer.valueOf(str);
        }
        if (getType() == Type.LONG) {
            return Long.valueOf(str);
        }
        if (getType() == Type.DOUBLE) {
            return Double.valueOf(str);
        }
        if (getType() == Type.SHORT) {
            return Short.valueOf(str);
        }
        if (getType() == Type.STRING) {
            return str;
        }
        return str;
    }

    public static ConfigKey of(String name, Type type, Object defaultValue) {
        return new ConfigKey(name, type, defaultValue);
    }

    public static ConfigKey of(String name, Type type) {
        return of(name, type, null);
    }

    public static ConfigKey of(String name) {
        return of(name, Type.STRING);
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", ConfigKey.class.getSimpleName() + "[", "]")
                .add("key='" + key + "'")
                .add("type=" + type)
                .add("defaultValue=" + defaultValue)
                .toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy