Total Pageviews

2016/05/06

Using Project Lombok to eliminate boilerplate code

What is boilerplate code
Boilerplate is the term used to describe sections of code that have to be included in many places with little or no alteration. It is more often used when referring to languages which are considered verbose, i.e. the programmer must write a lot of code to do minimal jobs.

How-to
We can use @Data to generate getters for all fields:
Before:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package albert.practice.xstream.beans;

import java.io.Serializable;

public class Doc implements Serializable {

    private static final long serialVersionUID = 1L;

    // 訂單編號
    private String regId;

    // 要保人ID
    private String ownerId;

    // 保單號碼
    private String policyNumber;

    // 被保人 ID
    private String insuredId;

    // 表單代碼
    private String formId;

    public String getRegId() {
        return regId;
    }

    public void setRegId(String regId) {
        this.regId = regId;
    }

    public String getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(String ownerId) {
        this.ownerId = ownerId;
    }

    public String getPolicyNumber() {
        return policyNumber;
    }

    public void setPolicyNumber(String policyNumber) {
        this.policyNumber = policyNumber;
    }

    public String getInsuredId() {
        return insuredId;
    }

    public void setInsuredId(String insuredId) {
        this.insuredId = insuredId;
    }

    public String getFormId() {
        return formId;
    }

    public void setFormId(String formId) {
        this.formId = formId;
    }

}

After using @Data:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package albert.practice.xstream.beans;

import java.io.Serializable;

import lombok.Data;

@Data
public class Doc implements Serializable {

    private static final long serialVersionUID = 1L;

    // 訂單編號
    private String regId;

    // 要保人ID
    private String ownerId;

    // 保單號碼
    private String policyNumber;

    // 被保人 ID
    private String insuredId;

    // 表單代碼
    private String formId;

}


We can use @NoArgsConstructor to generate a no-args constructor.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package albert.practice.xstream.beans;

import java.io.Serializable;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class Doc implements Serializable {

    private static final long serialVersionUID = 1L;

    // 訂單編號
    private String regId;

    // 要保人ID
    private String ownerId;

    // 保單號碼
    private String policyNumber;

    // 被保人 ID
    private String insuredId;

    // 表單代碼
    private String formId;

}

We can use @AllArgsConstructor to generate an all-args constructor
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package albert.practice.xstream.beans;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Doc implements Serializable {

    private static final long serialVersionUID = 1L;

    // 訂單編號
    private String regId;

    // 要保人ID
    private String ownerId;

    // 保單號碼
    private String policyNumber;

    // 被保人 ID
    private String insuredId;

    // 表單代碼
    private String formId;

}

We can use @ToString generate an implementation for the toString method inherited by all objects, consisting of printing the values of relevant fields. 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package albert.practice.xstream.beans;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Doc implements Serializable {

    private static final long serialVersionUID = 1L;

    // 訂單編號
    private String regId;

    // 要保人ID
    private String ownerId;

    // 保單號碼
    private String policyNumber;

    // 被保人 ID
    private String insuredId;

    // 表單代碼
    private String formId;

}

Reference
[1] http://jnb.ociweb.com/jnb/jnbJan2010.html
[2] https://en.wikipedia.org/wiki/Boilerplate_%28text%29

No comments: