Sunday, 9 August 2015

Guava : Functions class

Functions class provides useful methods to work with Function instances.
Using the Functions.forMap method
‘forMap’ method takes a map as input and returns a Function instance, which performs map look up operation.

There are two overloaded versions of forMap method available.

public static <K,V> Function<K,V> forMap(Map<K,V> map)
Takes a map as input and returns a Function instance, which performs map look up operation. If key doesn’t exist in map, then this function throws IllegalArgumentException.

public static <K,V> Function<K,V> forMap(Map<K,? extends V> map, @Nullable V defaultValue)

Takes a map as input and returns a Function instance, which performs map look up operation. If key doesn’t exist in map, then this function returns default value.
import java.util.Objects;

import com.google.common.base.MoreObjects;

public class Employee {
  private String firstName;
  private String lastName;
  private int age;
  private int id;

  Employee() {
    this(-1, "no data", "no data", -1);
  }

  Employee(int id, String firstName, String lastName, int age) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    this.id = id;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  @Override
  public boolean equals(Object employee) {
    if (Objects.isNull(employee))
      return false;

    if (!(employee instanceof Employee))
      return false;

    Employee emp = (Employee) employee;

    return id == emp.id;
  }

  @Override
  public int hashCode() {
    return Objects.hash(firstName, lastName, age);
  }

  @Override
  public String toString() {
    return MoreObjects.toStringHelper(this).omitNullValues().add("id", id)
        .add("firstName", firstName).add("lastName", lastName)
        .add("age", age).toString();

  }
}

import java.util.HashMap;
import java.util.Map;

import com.google.common.base.Functions;
import com.google.common.base.Function;

public class FunctionsForMapEx {
  public static void main(String args[]) {
    Map<Integer, Employee> employees = new HashMap<>();

    employees.put(1, new Employee(1, "Hari Krishna", "Gurram", 26));
    employees.put(2, new Employee(2, "Sankalp", "Dubey", 31));

    Function<Integer, Employee> emps = Functions.forMap(employees,
        new Employee());

    System.out.println(emps.apply(1));
    System.out.println(emps.apply(3));
  }
}


Output
Employee{id=1, firstName=Hari Krishna, lastName=Gurram, age=26}
Employee{id=-1, firstName=no data, lastName=no data, age=-1}


Using Functions.compose method
Functions class provides compose method, which returns composition of two functions.

public static <A,B,C> Function<A,C> compose(Function<B,C> g, Function<A,? extends B> f)
Returns the composition of two functions. For f: A->B and g: B->C, composition is defined as the function h such that h(a) == g(f(a)) for each a.

Lets say I had one map where employee id is mapped to manager id. I had another map where manager id is mapped to manager details. My task is to get manager details from employee id.


empId ----->Manager Id -----> Manager Details
import java.util.Objects;

import com.google.common.base.MoreObjects;

public class Manager {
  private String firstName;
  private String lastName;
  private int age;
  private int id;

  Manager() {
    this(-1, "no data", "no data", -1);
  }

  Manager(int id, String firstName, String lastName, int age) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    this.id = id;
  }

  public String getFirstName() {
    return firstName;
  }

  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  public int getAge() {
    return age;
  }

  public void setAge(int age) {
    this.age = age;
  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  @Override
  public boolean equals(Object manager) {
    if (Objects.isNull(manager))
      return false;

    if (!(manager instanceof Manager))
      return false;

    Manager mng = (Manager) manager;

    return id == mng.id;
  }

  @Override
  public int hashCode() {
    return Objects.hash(firstName, lastName, age);
  }

  @Override
  public String toString() {
    return MoreObjects.toStringHelper(this).omitNullValues().add("id", id)
        .add("firstName", firstName).add("lastName", lastName)
        .add("age", age).toString();

  }
}

import java.util.HashMap;
import java.util.Map;

import com.google.common.base.Function;
import com.google.common.base.Functions;

public class CompositionTest {
  public static void main(String args[]) {
    Map<Integer, Manager> managers = new HashMap<>();

    managers.put(1, new Manager(1, "Hari Krishna", "Gurram", 36));
    managers.put(2, new Manager(2, "Sankalp", "Dubey", 41));

    Function<Integer, Manager> managerDetails = Functions.forMap(managers,
        new Manager());

    Map<Integer, Integer> empToManager = new HashMap<>();
    empToManager.put(1, 1);
    empToManager.put(2, 1);
    empToManager.put(3, 1);
    empToManager.put(4, 1);
    empToManager.put(5, 2);
    empToManager.put(6, 2);

    Function<Integer, Integer> empToManagerMap = Functions.forMap(
        empToManager, 1);

    Function<Integer, Manager> details = Functions.compose(managerDetails,
        empToManagerMap);

    System.out.println("Manager for employee 1 " + details.apply(1));
    System.out.println("Manager for employee 5 " + details.apply(5));
    System.out.println("Manager for employee 7 " + details.apply(7));

  }
}


Output

Manager for employee 1 Manager{id=1, firstName=Hari Krishna, lastName=Gurram, age=36}
Manager for employee 5 Manager{id=2, firstName=Sankalp, lastName=Dubey, age=41}
Manager for employee 7 Manager{id=1, firstName=Hari Krishna, lastName=Gurram, age=36}




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment