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

com.phloc.validation.validator.string.StringValidatorChainAND Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
/**
 * Copyright (C) 2006-2014 phloc systems
 * http://www.phloc.com
 * office[at]phloc[dot]com
 *
 * 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 com.phloc.validation.validator.string;

import java.util.List;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.phloc.commons.annotations.Nonempty;
import com.phloc.commons.collections.ArrayHelper;
import com.phloc.commons.collections.ContainerHelper;
import com.phloc.commons.hash.HashCodeGenerator;
import com.phloc.commons.string.ToStringGenerator;
import com.phloc.validation.result.IValidationResult;
import com.phloc.validation.result.ValidationResultSuccess;
import com.phloc.validation.validator.IBaseValidator;
import com.phloc.validation.validator.IStringValidator;

/**
 * A collection of simple {@link IBaseValidator} objects that are executed
 * together. Only if all validators indicate success, the overall validation
 * succeeds. In case of a validation error, the first error is returned.
 * 
 * @author Philip Helger
 */
public class StringValidatorChainAND extends AbstractStringValidator
{
  private final List  m_aValidators;

  public StringValidatorChainAND (@Nonnull @Nonempty final IStringValidator... aValidators)
  {
    if (ArrayHelper.isEmpty (aValidators))
      throw new IllegalArgumentException ("validators");
    m_aValidators = ContainerHelper.newList (aValidators);
  }

  @Nonnull
  public IValidationResult validate (@Nullable final String aValue)
  {
    for (final IStringValidator aValidator : m_aValidators)
    {
      final IValidationResult aRes = aValidator.validate (aValue);
      if (aRes.isInvalid ())
        return aRes;
    }
    return ValidationResultSuccess.getInstance ();
  }

  @Override
  public boolean equals (final Object o)
  {
    if (o == this)
      return true;
    if (!super.equals (o))
      return false;
    final StringValidatorChainAND rhs = (StringValidatorChainAND) o;
    return m_aValidators.equals (rhs.m_aValidators);
  }

  @Override
  public int hashCode ()
  {
    return HashCodeGenerator.getDerived (super.hashCode ()).append (m_aValidators).getHashCode ();
  }

  @Override
  public String toString ()
  {
    return ToStringGenerator.getDerived (super.toString ()).append ("validators", m_aValidators).toString ();
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy