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

io.github.mmm.validation.main.ObjectValidatorBuilder Maven / Gradle / Ivy

The newest version!
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.validation.main;

import java.util.ArrayList;
import java.util.List;

import io.github.mmm.base.lang.Builder;
import io.github.mmm.validation.AbstractValidator;
import io.github.mmm.validation.ComposedValidator;
import io.github.mmm.validation.Validator;
import io.github.mmm.validation.ValidatorRegistry;

/**
 * This is the base class to create instances of {@link AbstractValidator} using the builder pattern.
 *
 * @param  the generic type of the value to {@link AbstractValidator#validate(Object) validate}.
 * @param  the generic type of the {@link #and() parent builder}.
 * @param  the generic type of this builder itself (this).
 *
 * @since 1.0.0
 */
public abstract class ObjectValidatorBuilder>
    implements Builder>, ValidatorRegistry {

  private final PARENT parent;

  private final List> validators;

  /**
   * The constructor.
   *
   * @param parent the {@link #and() parent} builder.
   */
  public ObjectValidatorBuilder(PARENT parent) {

    super();
    this.validators = new ArrayList<>();
    this.parent = parent;
  }

  /**
   * @param validator the {@link AbstractValidator} to add to this builder.
   * @return this build instance for fluent API calls.
   */
  @Override
  public SELF add(Validator validator) {

    this.validators.add(validator);
    return self();
  }

  /**
   * @param  the generic type of the value to {@link AbstractValidator#validate(Object) validate}.
   * @param builder the {@link ObjectValidatorBuilder}.
   * @return the {@link List} of validators.
   */
  protected  List> getValidators(ObjectValidatorBuilder builder) {

    return builder.validators;
  }

  /**
   * @return the parent {@link Builder} or {@code null} if {@literal } is void.
   */
  public PARENT and() {

    return this.parent;
  }

  /**
   * @return this build instance for fluent API calls.
   */
  @SuppressWarnings("unchecked")
  protected SELF self() {

    return (SELF) this;
  }

  /**
   * @param validator the {@link AbstractValidator} to add to this builder.
   * @return this build instance for fluent API calls.
   */
  public SELF add(AbstractValidator validator) {

    this.validators.add(validator);
    return self();
  }

  /**
   * Value is {@link ValidatorMandatory mandatory}.
   *
   * @return this build instance for fluent API calls.
   */
  public SELF mandatory() {

    return add(ValidatorMandatory.get());
  }

  /**
   * This method allows to define a minimum. Values exceeding this minimum will be invalid. Avoid using
   * {@link #min(String)} and {@link #max(String)} and use {@link #range(String, String)} in such case.
   *
   * @param min the minimum value allowed.
   * @return this build instance for fluent API calls.
   */
  public SELF min(String min) {

    return range(min, null);
  }

  /**
   * This method allows to define a maximum. Values exceeding this maximum will be invalid.
   *
   * @param max the maximum value allowed.
   * @return this build instance for fluent API calls.
   */
  public SELF max(String max) {

    return range(null, max);
  }

  /**
   * This method allows to define a range in a generic way. If you have a properly typed builder please use more
   * specific methods such as {@link ComparableValidatorBuilder#range(io.github.mmm.base.range.Range)} instead.
   *
   * @param min the minimum value allowed or {@code null} for no lower bound.
   * @param max the maximum value allowed or {@code null} for no upper bound.
   * @return this build instance for fluent API calls.
   */
  public abstract SELF range(String min, String max);

  /**
   * @return the {@link AbstractValidator}
   */
  @Override
  public Validator build() {

    int size = this.validators.size();
    if (size == 0) {
      return Validator.none();
    } else if (size == 1) {
      return this.validators.get(0);
    } else {
      AbstractValidator[] array = this.validators.toArray(new AbstractValidator[size]);
      return new ComposedValidator<>(array);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy