Sunday, May 12, 2019

Lombok Example

Lombok can
• generate the getter and setter boilerplate
• perform null-checking
• provide var and val type inference to Java versions prior to Java 10
<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                      http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mycompany.app</groupId>
  <artifactId>r4j-example</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>Maven Quick Start Archetype</name>
  <url>http://maven.apache.org</url>
  <properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
  </properties>
  
  <dependencies>
      <dependency>
          <groupId>org.projectlombok</groupId>
          <artifactId>lombok</artifactId>
          <version>1.18.8</version>
          <scope>provided</scope>
      </dependency>
  </dependencies>
</project>
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

@Getter @Setter 
public class Account {
    String type;
    public Account() {}
}
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

@Getter @Setter 
public class Employee {
    String name;
    public Employee() {}
}
import java.util.HashMap;
import lombok.val;
import lombok.NonNull;

public class Example {
 Account account = new Account();
 Employee employee = new Employee();
 
    public HashMap valDemo() {
        val valMap = new HashMap();
        valMap.put(0, "zero");
        valMap.put(5, "five");
        valMap.put(30, "thirty");
        return valMap;
    }

    public void notNullDemo(@NonNull Employee employee, @NonNull Account account){
        employee.setName("Stephen");
        account.setType("Super-Saver");
    }
 
    public static void main(String[] args) {
        Example ex = new Example();
        System.out.println(ex.valDemo());
        ex.notNullDemo(ex.employee, ex.account);
  }
}