Sunday 9 August 2015

Predicates class

Predicates class provides useful methods for working with Predicate instances.
Following are the simple methods to create true, false, check null and negate a predicate
Method
Description
public static <T> Predicate<T> alwaysTrue()
Returns a predicate that always evaluates to true.
public static <T> Predicate<T> alwaysFalse()
Returns a predicate that always evaluates to false.
public static <T> Predicate<T> isNull()
Returns a predicate that evaluates to true, if the object being tested is null, else false.
public static <T> Predicate<T> notNull()
Returns a predicate that evaluates to true, if the object being tested is not null, else false.
public static <T> Predicate<T> not(Predicate<T> predicate)
It negates another predicate.
Returns a predicate that evaluates to true if the given predicate evaluates to false.


import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

public class PredicatesEx {

  public static Predicate<Object> truePredicate = Predicates.alwaysTrue();
  public static Predicate<Object> falsePredicate = Predicates.alwaysFalse();
  public static Predicate<Object> nullPredicate = Predicates.isNull();
  public static Predicate<Object> notNullPredicate = Predicates.notNull();
  public static Predicate<Object> negatePrediacate = Predicates
      .not(truePredicate);

  public static void main(String args[]) {
    Object obj = new Object();

    System.out.println(truePredicate.apply(obj));
    System.out.println(falsePredicate.apply(obj));
    System.out.println(nullPredicate.apply(obj));
    System.out.println(notNullPredicate.apply(obj));
    System.out.println(negatePrediacate.apply(obj));

  }
}


Output
true
false
false
true
false

Logically combining predicates
Predicates class provides and, or methods to logically combine predicates.

Method
Description
public static <T> Predicate<T> and(Iterable<? extends Predicate<? super T>> components)

Returns a predicate that evaluates to true if each of its components evaluates to true.
public static <T> Predicate<T> and(Predicate<? super T>... components)

Returns a predicate that evaluates to true if each of its components evaluates to true.
public static <T> Predicate<T> and(Predicate<? super T> first, Predicate<? super T> second)

Returns a predicate that evaluates to true if each of its components evaluates to true.
public static <T> Predicate<T> or(Iterable<? extends Predicate<? super T>> components)
Returns a predicate that evaluates to true if any of its components evaluates to true.
public static <T> Predicate<T> or(Predicate<? super T>... components)
Returns a predicate that evaluates to true if any of its components evaluates to true.
public static <T> Predicate<T> or(Predicate<? super T> first, Predicate<? super T> second)
Returns a predicate that evaluates to true if any of its components evaluates to true.


import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

public class PredicatesEx {

  public static Predicate<String> lengthMinPredicate = new Predicate<String>() {

    @Override
    public boolean apply(String input) {
      return input.length() > 6;
    }

  };

  public static Predicate<String> lengthMaxPredicate = new Predicate<String>() {

    @Override
    public boolean apply(String input) {
      return input.length() < 16;
    }

  };

  public static Predicate<String> andPredicate = Predicates.and(
      lengthMinPredicate, lengthMaxPredicate);
  public static Predicate<String> orPredicate = Predicates.or(
      lengthMinPredicate, lengthMaxPredicate);

  public static void main(String args[]) {
    System.out.println(andPredicate.apply("Krishna"));
    System.out.println(andPredicate.apply("Phalgungarimella"));
    System.out.println(andPredicate.apply("abc"));

    System.out.println(orPredicate.apply("Krishna"));
    System.out.println(orPredicate.apply("Phalgungarimella"));
    System.out.println(orPredicate.apply("abc"));
  }
}


Output
true
false
false
true
true
true

Search for a string
Predicates class provides following methods to search for a string in given sequence.

public static Predicate<CharSequence> containsPattern(String pattern)
public static Predicate<CharSequence> contains(Pattern pattern)

Returns a predicate that evaluates to true if the CharSequence being tested contains any match for the given regular expression pattern.
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

public class PredicatesEx {

  public static Predicate<CharSequence> HelloPattern = Predicates
      .containsPattern("hello");

  public static void main(String args[]) {
    System.out.println(HelloPattern.apply("hello, how are you"));
    System.out.println(HelloPattern.apply("I love india"));
    System.out.println(HelloPattern.apply("hello there"));
  }
}


Output
true
false
true

Composition of a function
public static <A, B> Predicate<A> compose(Predicate<B> predicate, Function<A, ? extends B> function)
For every x, the generated predicate returns predicate(function(x)).

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

public class PredicatesEx {

  public static Function<Integer, Integer> square = new Function<Integer, Integer>() {

    @Override
    public Integer apply(Integer input) {
      return input * input;
    }

  };

  public static Predicate<Integer> predicate = new Predicate<Integer>() {

    @Override
    public boolean apply(Integer input) {
      if (input > 1000)
        return true;
      return false;
    }

  };

  public static Predicate<Integer> composed = Predicates.compose(predicate,
      square);

  public static void main(String args[]) {
    System.out.println(composed.apply(10)); // 10*10<10000
    System.out.println(composed.apply(40)); // 40*40 > 1000
  }
}


Output

false
true







Prevoius                                                 Next                                                 Home

No comments:

Post a Comment