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

io.github.theangrydev.businessflows.FieldValidator Maven / Gradle / Ivy

/*
 * Copyright 2016 Liam Williams .
 *
 * This file is part of business-flows.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.github.theangrydev.businessflows;

/**
 * A {@link Validator} for a {@link Field} of a {@link Happy} object.
 *
 * @param  The type that contains the {@link Field}
 * @param    The type of validation failure
 * @param  The type of the field to validate
 */
public class FieldValidator implements Validator {
    private final Mapping fieldExtractor;
    private final Validator validator;

    private FieldValidator(Mapping fieldExtractor, Validator validator) {
        this.fieldExtractor = fieldExtractor;
        this.validator = validator;
    }

    /**
     * Produces a {@link FieldValidator} that will validate the extracted field using the given validator.
     *
     * @param fieldExtractor Extracts the {@link Field} from the {@link Happy}
     * @param fieldValidator The {@link Validator} will validate the {@link Field}, possibly producing a {@link Sad} failure
     * @param         The type that contains the {@link Field}
     * @param           The type of validation failure
     * @param         The type of the field to validate
     * @return The validator
     */
    public static  FieldValidator fieldValidator(Mapping fieldExtractor, Validator fieldValidator) {
        return new FieldValidator<>(fieldExtractor, fieldValidator);
    }

    @Override
    public PotentialFailure attempt(Happy happy) throws Exception {
        Field field = fieldExtractor.map(happy);
        return validator.attempt(field);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy