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

nbbrd.io.text.BooleanProperty Maven / Gradle / Ivy

package nbbrd.io.text;

import lombok.NonNull;

import java.util.Map;
import java.util.Properties;
import java.util.function.BiConsumer;
import java.util.function.Function;

@lombok.RequiredArgsConstructor(staticName = "of")
public final class BooleanProperty extends BaseProperty {

    @NonNull
    @lombok.Getter
    private final String key;

    @lombok.Getter
    private final boolean defaultValue;

    public boolean get(@NonNull Function properties) {
        CharSequence value = properties.apply(key);
        if (value == null) return defaultValue;
        Boolean result = Parser.onBoolean().parse(value);
        return result != null ? result : defaultValue;
    }

    public boolean get(@NonNull Properties properties) {
        return get(properties::getProperty);
    }

    public boolean get(@NonNull Map properties) {
        return get(properties::get);
    }

    public void set(@NonNull BiConsumer properties, boolean value) {
        if (value != defaultValue) {
            String valueAsString = Formatter.onBoolean().formatAsString(value);
            if (valueAsString != null) properties.accept(key, valueAsString);
        }
    }

    public void set(@NonNull Properties properties, boolean value) {
        set(properties::setProperty, value);
    }

    public void set(@NonNull Map properties, boolean value) {
        set(properties::put, value);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy