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

io.github.jonestimd.swing.validation.Validator Maven / Gradle / Ivy

There is a newer version: 1.4.5
Show newest version
// The MIT License (MIT)
//
// Copyright (c) 2017 Timothy D. Jones
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package io.github.jonestimd.swing.validation;

import java.util.Objects;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.google.common.base.Strings;

/**
 * An interface for providing validation of an input value.
 * @param  the class of the input value
 */
public interface Validator {
    static  Validator empty() {
        return bean -> null;
    }

    /**
     * Validate a value.
     * @param value the value to validate
     * @return a description of the validation errors or {@code null}
     */
    String validate(T value);

    /**
     * @return a new {@code Validator} that only reports errors when the condition is true
     */
    default Validator when(Supplier condition) {
        return value -> condition.get() ? validate(value) : null;
    }

    /**
     * @return a new {@code Validator} that only reports errors when the condition is false
     */
    default Validator whenNot(Supplier condition) {
        return value -> condition.get() ? null : validate(value);
    }

    /**
     * Apply another validation if this validation passes.
     * @param other the other validator
     * @return a new {@code Validator} that returns the error from {@code this} or {@code other}
     */
    default Validator then(Validator other) {
        return value -> {
            String message = validate(value);
            return message == null ? other.validate(value) : message;
        };
    }

    /**
     * Apply another validation after this validation.  Uses {@code System.lineSeparator()} as the message separator.
     * @param other the other validator
     * @return a new {@code Validator} that returns the combined messages of {@code this} and {@code other}
     */
    default Validator add(Validator other) {
        return add(other, System.lineSeparator());
    }

    /**
     * Apply another validation after this validation.
     * @param other the other validator
     * @param separator the separator to use when joining the error messages
     * @return a new {@code Validator} that returns the combined messages of {@code this} and {@code other}
     */
    default Validator add(Validator other, String separator) {
        return value -> Strings.emptyToNull(Stream.of(validate(value), other.validate(value))
                .filter(Objects::nonNull)
                .collect(Collectors.joining(separator)));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy