package io.github.benas.xstream;

import java.util.Objects;
import java.util.function.Predicate;

/**
 * This class consists of {@code static} factory methods to create commons predicates.
 *
 * @author Mahmoud Ben Hassine (mahmoud@benhassine.fr)
 */
public class Predicates {

    /**
     * Create a new predicate returning true when the input is not null.
     *
     * @param <T> the type of object on which the predicate will be applied.
     * @return an instance of the predicate
     */
    public static <T> Predicate<T> nonNull() {
        return Objects::nonNull;
    }

    /**
     * Create a new predicate returning true when the input is null.
     * @param <T> the type of object on which the predicate will be applied.
     * @return an instance of the predicate
     */
    public static <T> Predicate<T> isNull() {
        return Objects::isNull;
    }

    /**
     * Create a new predicate returning true when the input String is empty.
     * @return a new predicate returning true when the input String is empty.
     */
    public static Predicate<String> empty() {
        return String::isEmpty;
    }

    /**
     * Create a new predicate returning true when the input String contains the given pattern.
     * @return a new predicate returning true when the input String contains the given pattern.
     */
    public static Predicate<String> contains(final String pattern) {
        Objects.requireNonNull(pattern, "The pattern must not be null");
        return s -> s.contains(pattern);
    }

    /**
     * Create a new predicate returning true when the input String starts with the given pattern.
     * @return a new predicate returning true when the input String starts with the given pattern.
     */
    public static Predicate<String> startsWith(final String pattern) {
        Objects.requireNonNull(pattern, "The pattern must not be null");
        return s -> s.startsWith(pattern);
    }

    /**
     * Create a new predicate returning true when the input String ends with the given pattern.
     * @return a new predicate returning true when the input String ends with the given pattern.
     */
    public static Predicate<String> endsWith(final String pattern) {
        Objects.requireNonNull(pattern, "The pattern must not be null");
        return s -> s.endsWith(pattern);
    }

    /**
     * Create a new predicate returning true when the input String matches the given regexp.
     * @return a new predicate returning true when the input String matches the given regexp.
     */
    public static Predicate<String> matches(final String regexp) {
        Objects.requireNonNull(regexp, "The regexp must not be null");
        return s -> s.matches(regexp);
    }

    /**
     * Create a new predicate returning true when the input number is even.
     * @return a new predicate returning true when the input number is even.
     */
    public static Predicate<Number> even() {
        return s -> s.shortValue() % 2 == 0;
    }

    /**
     * Create a new predicate returning true when the input number is odd.
     * @return a new predicate returning true when the input number is odd.
     */
    public static Predicate<Number> odd() {
        return even().negate();
    }

    /**
     * Create a new predicate returning true when the input number is equal to zero.
     * @return a new predicate returning true when the input number is equal to zero.
     */
    public static Predicate<Number> isZero() {
        return s -> s.shortValue() == 0;
    }
}

@@ -648,24 +648,31 @@
 ut is null.%0A
+     *%0A
      * @para
@@ -728,32 +728,32 @@
 ill be applied.%0A
-
      * @return a
@@ -954,24 +954,31 @@
 g is empty.%0A
+     *%0A
      * @retu
@@ -1231,32 +1231,101 @@
  given pattern.%0A
+     *%0A     * @param pattern the pattern to look for in each element%0A
      * @return a
@@ -1700,32 +1700,101 @@
  given pattern.%0A
+     *%0A     * @param pattern the pattern to look for in each element%0A
      * @return a
@@ -2178,24 +2178,93 @@
 en pattern.%0A
+     *%0A     * @param pattern the pattern to look for in each element%0A
      * @retu
@@ -2643,24 +2643,100 @@
 ven regexp.%0A
+     *%0A     * @param regexp the regular expression to match in each element%0A
      * @retu
@@ -3089,24 +3089,31 @@
 er is even.%0A
+     *%0A
      * @retu
@@ -3361,24 +3361,31 @@
 ber is odd.%0A
+     *%0A
      * @retu
@@ -3539,32 +3539,32 @@
 %0A    %7D%0A%0A    /**%0A
-
      * Create a 
@@ -3628,24 +3628,31 @@
 al to zero.%0A
+     *%0A
      * @retu

update javadoc