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

net.anwiba.commons.ensure.Conditions Maven / Gradle / Ivy

There is a newer version: 1.0.185
Show newest version
/*
 * #%L
 * anwiba commons core
 * %%
 * Copyright (C) 2007 - 2016 Andreas W. Bartels ([email protected])
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */
package net.anwiba.commons.ensure;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

public class Conditions {

  public static  ICondition equalTo(final T value) {
    return new EqualToCondition<>(value);
  }

  public static  ICondition notNull() {
    return new NotCondition<>(new IsNullCondition());
  }

  public static  ICondition isNull() {
    return new IsNullCondition<>();
  }

  public static ICondition isTrue() {
    return new IsTrueCondition();
  }

  public static ICondition isFalse() {
    return new NotCondition<>(new IsTrueCondition());
  }

  @SafeVarargs
  public static  ICondition in(final T... values) {
    return in(Arrays.asList(values));
  }

  public static  ICondition in(final Collection collection) {
    return new InCondition<>(collection == null ? new ArrayList() : collection);
  }

  public static  ICondition not(final ICondition validation) {
    Ensure.ensureArgumentNotNull(validation);
    return new NotCondition<>(validation);
  }

  public static ICondition instanceOf(final Class value) {
    Ensure.ensureArgumentNotNull(value);
    return new InstanceOfCondition(value);
  }

  public static ICondition contains(final String value) {
    Ensure.ensureArgumentNotNull(value);
    return new StringContainsCondition(value);
  }

  public static ICondition startsWith(final String value) {
    Ensure.ensureArgumentNotNull(value);
    return new StringStartsWithCondition(value);
  }

  public static > ICondition between(final T minimum, final T maximum) {
    Ensure.ensureArgumentNotNull(minimum);
    Ensure.ensureArgumentNotNull(maximum);
    return new BetweenCondition<>(minimum, maximum);
  }

  public static > ICondition greaterThan(final T value) {
    Ensure.ensureArgumentNotNull(value);
    return new GreaterThanCondition<>(value);
  }

  public static > ICondition lowerThan(final T value) {
    Ensure.ensureArgumentNotNull(value);
    return new LowerThanCondition<>(value);
  }

  public static ICondition endsWith(final String value) {
    Ensure.ensureArgumentNotNull(value);
    return new StringEndsWithCondition(value);
  }

  @SafeVarargs
  public static  ICondition anyOf(final ICondition... conditions) {
    return new AnyOfCondition<>(Arrays.asList(conditions));
  }

  @SafeVarargs
  public static  ICondition allOf(final ICondition... conditions) {
    return new AllOfCondition<>(Arrays.asList(conditions));
  }

  public static > ICondition isEmpty() {
    return new IsEmptyCollectionCondition<>();
  }

  @SafeVarargs
  public static > ICondition containsAll(final V... values) {
    return containsAll(Arrays.asList(values));
  }

  public static > ICondition containsAll(final Collection values) {
    Ensure.ensureArgumentNotNull(values);
    return new CollectionContaintsCondition<>(values);
  }

  public static > ICondition containsNull() {
    return new CollectionContaintsNullCondition<>();
  }

  @SuppressWarnings("rawtypes")
  public static  ICondition size(final int i) {
    return new CollectionSizeCondition<>(i);
  }
}