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

org.guppy4j.config.SettingImpl Maven / Gradle / Ivy

There is a newer version: 0.0.7
Show newest version
package org.guppy4j.config;

import org.guppy4j.util.StringSplitter;
import org.guppy4j.util.StringSplitterImpl;

import static java.lang.String.format;

/**
 * Setting implementation
 */
public class SettingImpl implements Setting {

    private final StringSplitter stringSplitter = new StringSplitterImpl();

    private final String name;
    private final String value;

    public SettingImpl(String name, String value) {
        this.name = name;
        this.value = value;
    }

    @Override
    public String name() {
        return name;
    }

    @Override
    public String value() {
        return value;
    }

    @Override
    public String valueNotNull() {
        if (value != null) {
            return value;
        } else {
            throw new IllegalStateException(format("Property '%s' is null", name));
        }
    }

    @Override
    public char asChar() {
        if (value.length() == 1) {
            return value.charAt(0);
        } else {
            throw new IllegalStateException(format(
                    "Value of property '%s' is not a single char: '%s'", name, value));
        }
    }

    @Override
    public int asInt() {
        return Integer.parseInt(value);
    }

    @Override
    public String[] valueSplitBy(char separator) {
        return value == null ? null : stringSplitter.split(value, separator);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy