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

com.bluecatcode.common.base.Predicates Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
package com.bluecatcode.common.base;

import com.bluecatcode.common.base.predicates.IsInstancePredicate;
import com.google.common.annotations.Beta;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Dictionary;
import java.util.Map;

import static java.lang.String.format;

/**
 * Additional Predicates as an extension to {@link com.google.common.base.Predicates}
 * 

* In performance critical contexts it is probably better to use hand-written checks, but measure first!. *

*
    *
  • nor - A 'not or' predicate
  • *
  • isEmpty* - Performs emptiness and nullness check for: * String, CharSequence, Optional, Collection, Iterable, Map, Object[], prim[]
  • *
  • isValidURI - Performs URI check against RFC 2396 specification
  • *
  • isValidEmail - Performs email address check against RFC 822 specification
  • *
  • isInstance - Performs a runtime check if the reference is an instance of any of provided class(es)
  • *
* * @see com.google.common.base.Predicates */ public final class Predicates { private static Predicate isEmpty() { return IsEmpty::isEmpty; } public static Predicate isEmptyString() { return String::isEmpty; } public static Predicate isEmptyCharSequence() { return chs -> chs.length() == 0; } public static Predicate isEmptyOptional() { return o -> !o.isPresent(); } public static Predicate isEmptyIterable() { return i -> !i.iterator().hasNext(); } public static Predicate isEmptyCollection() { return Collection::isEmpty; } public static Predicate isEmptyMap() { return Map::isEmpty; } public static Predicate isEmptyObjectArray() { return a -> a.length == 0; } public static Predicate isEmptyBooleanArray() { return a -> a.length == 0; } public static Predicate isEmptyByteArray() { return a -> a.length == 0; } public static Predicate isEmptyCharArray() { return a -> a.length == 0; } public static Predicate isEmptyShortArray() { return a -> a.length == 0; } public static Predicate isEmptyIntArray() { return a -> a.length == 0; } public static Predicate isEmptyLongArray() { return a -> a.length == 0; } public static Predicate isEmptyFloatArray() { return a -> a.length == 0; } public static Predicate isEmptyDoubleArray() { return a -> a.length == 0; } public static Predicate isEmptyDictionary() { return Dictionary::isEmpty; } public static Predicate isEmptyObject() { return reference -> { //noinspection ConstantConditions if (reference == null) { return true; } Predicate isEmpty; if (reference instanceof IsEmpty) { isEmpty = isEmpty(); } else if (reference instanceof String) { isEmpty = isEmptyString(); } else if (reference instanceof Optional) { isEmpty = isEmptyOptional(); } else if (reference instanceof Collection) { isEmpty = isEmptyCollection(); } else if (reference instanceof Iterable) { isEmpty = isEmptyIterable(); } else if (reference instanceof Map) { isEmpty = isEmptyMap(); } else if (reference instanceof Dictionary) { isEmpty = isEmptyDictionary(); } else if (reference instanceof CharSequence) { isEmpty = isEmptyCharSequence(); } else if (reference instanceof Object[]) { isEmpty = isEmptyObjectArray(); } else if (reference instanceof boolean[]) { isEmpty = isEmptyBooleanArray(); } else if (reference instanceof byte[]) { isEmpty = isEmptyByteArray(); } else if (reference instanceof short[]) { isEmpty = isEmptyShortArray(); } else if (reference instanceof char[]) { isEmpty = isEmptyCharArray(); } else if (reference instanceof int[]) { isEmpty = isEmptyIntArray(); } else if (reference instanceof long[]) { isEmpty = isEmptyLongArray(); } else if (reference instanceof float[]) { isEmpty = isEmptyFloatArray(); } else if (reference instanceof double[]) { isEmpty = isEmptyDoubleArray(); } else { throw new IllegalArgumentException(format( "Expected a supported type instead of %s, supported types: %s", reference.getClass().getCanonicalName(), "String, CharSequence, Optional, Iterable, Collection, Map, Object[], primitive[]")); } //noinspection unchecked return ((Predicate) isEmpty).apply(reference); }; } /** * NOT OR Predicate * * Truth table: *
     * B  A  Q
     * -------
     * 0  0  1
     * 0  1  0
     * 1  0  0
     * 1  1  0
     * 
* * @param first the first argument * @param second the second argument * @return the predicate */ @Beta public static Predicate nor(Predicate first, Predicate second) { return com.google.common.base.Predicates.not(com.google.common.base.Predicates.or(first, second)); } @Beta @SafeVarargs public static Predicate nor(Predicate... components) { return com.google.common.base.Predicates.not(com.google.common.base.Predicates.or(components)); } /** * NOT AND Predicate * * Truth table: *
     * B  A  Q
     * -------
     * 0  0  1
     * 0  1  1
     * 1  0  1
     * 1  1  0
     * 
* * @param first the first argument * @param second the second argument * @return the predicate */ @Beta public static Predicate nand(Predicate first, Predicate second) { return com.google.common.base.Predicates.not(com.google.common.base.Predicates.and(first, second)); } /** * NOT AND Predicate * * @see #nand(com.google.common.base.Predicate, com.google.common.base.Predicate) * @param components the operator arguments * @return the predicate */ @Beta @SafeVarargs public static Predicate nand(Predicate... components) { return com.google.common.base.Predicates.not(com.google.common.base.Predicates.and(components)); } @Beta public static Predicate isValidURI() { return uri -> { //noinspection ConstantConditions if (uri == null) return false; try { new URI(uri); } catch (URISyntaxException ex) { return false; } return true; }; } @Beta public static Predicate isValidEmail() { return email -> { //noinspection ConstantConditions if (email == null) return false; try { new InternetAddress(email).validate(); } catch (AddressException ex) { return false; } return true; }; } @Beta public static Predicate isInstance(Class... types) { return new IsInstancePredicate(types); } private Predicates() { throw new UnsupportedOperationException(); } }