Sunday, 30 August 2015

Project Lombok

In this post, we are going to learn simple package Lombok, which is used to remove lot of boilerplate code. Boilerplate code is the sections of code that have to be included in many places with little or no alteration.

Usually simple model class Employee contains
a.   Instance/static properties
b.   Getter and setter methods to set and get properties
c.    toString method
d.   hashCode method
e.   equals method

Usually instance/static properties are different for model classes, but don’t you think our classes contains lot of boilerplate code like getters, setters, toString etc.,

Suppose consider simple Employee class with properties id, firstName, lastName, age. It looks like below.

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


After adding getters, setters, toString, hashCode, equals method, it looks like below.
public class Employee {
 private int id;
 private String firstName;
 private String lastName;
 private int age;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  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;
 }

 @Override
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + age;
  result = prime * result
    + ((firstName == null) ? 0 : firstName.hashCode());
  result = prime * result + id;
  result = prime * result
    + ((lastName == null) ? 0 : lastName.hashCode());
  return result;
 }

 @Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())
   return false;
  Employee other = (Employee) obj;
  if (age != other.age)
   return false;
  if (firstName == null) {
   if (other.firstName != null)
    return false;
  } else if (!firstName.equals(other.firstName))
   return false;
  if (id != other.id)
   return false;
  if (lastName == null) {
   if (other.lastName != null)
    return false;
  } else if (!lastName.equals(other.lastName))
   return false;
  return true;
 }

 @Override
 public String toString() {
  StringBuilder builder = new StringBuilder();
  builder.append("Employee [id=").append(id).append(", firstName=")
    .append(firstName).append(", lastName=").append(lastName)
    .append(", age=").append(age).append("]");
  return builder.toString();
 }

}


By using Project Lombok, we can update above model class like below.
@EqualsAndHashCode(callSuper=true)
@ToString
public class Employee {
 @Getter @Setter private int id;
 @Getter @Setter private String firstName;
 @Getter @Setter private String lastName;
 @Getter @Setter private int age;
}


We can remove all the boilerplate code by using annotations provided by Lombok. Following is the complete application

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@EqualsAndHashCode()
@ToString
public class Employee {
 @Getter @Setter private int id;
 @Getter @Setter private String firstName;
 @Getter @Setter private String lastName;
 @Getter @Setter private int age;
}

public class EmployeeTest {

 public static void main(String args[]) {
  Employee emp1 = new Employee();
  Employee emp2 = new Employee();
  Employee emp3 = new Employee();

  emp1.setAge(27);
  emp1.setFirstName("Sudheer");
  emp1.setLastName("Ganji");
  emp1.setId(1);

  emp2.setAge(27);
  emp2.setFirstName("Sudheer");
  emp2.setLastName("Ganji");
  emp2.setId(1);

  emp3.setAge(26);
  emp3.setFirstName("Sudheer");
  emp3.setLastName("Ganji");
  emp3.setId(1);

  System.out.println(emp1.getAge());
  System.out.println(emp1.getFirstName());
  System.out.println(emp1.getId());
  System.out.println(emp1.getLastName());
  System.out.println(emp1.toString());
  System.out.println(emp1.hashCode());
  System.out.println(emp1.equals(emp2));
  System.out.println(emp1.equals(emp3));
 }
}


Output

27
Sudheer
1
Ganji
Employee(id=1, firstName=Sudheer, lastName=Ganji, age=27)
-1385608068
true
false


Reference

Note
a. You need to install Lombok in eclipse, to display getter, setters in auto complete. Following link explains this clearly.

No comments:

Post a Comment