Lombok

Posted by Bruce Tsai
9/19/2016

Lombok 是一個有趣的 Java 開發輔助工具,它的主要功能是利用 annotation 的標註,達成自動生成 getter 與 setter 等方法,同時自動生成的 getter/setter 等也能夠被 IDE 所識別。對於在開發時,需要不斷地手動或利用 IDE 來產生建構子或 getter/setter 等的動作,可以極大的簡化這些步驟。雖然 Lombok 提供了不少便利,但仍要注意它的使用限制,如在父類別的的建構子識別等問題,相關的問題可以參考專案的 Issue 清單。

使用範例

package com.ociweb.jnb.lombok;

import java.util.Date;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NonNull;

@Data
@EqualsAndHashCode(exclude={"address","city","state","zip"})
public class Person {
    enum Gender { Male, Female }

    @NonNull private String firstName;
    @NonNull private String lastName;
    @NonNull private final Gender gender;
    @NonNull private final Date dateOfBirth;

    private String ssn;
    private String address;
    private String city;
    private String state;
    private String zip;
}
package com.ociweb.jnb.lombok;

import java.util.Date;
import lombok.NonNull;

public class Person {

    enum Gender {
        /*public static final*/ Male /* = new Gender() */,
        /*public static final*/ Female /* = new Gender() */;
    }
    @NonNull
    private String firstName;
    @NonNull
    private String lastName;
    @NonNull
    private final Gender gender;
    @NonNull
    private final Date dateOfBirth;
    private String ssn;
    private String address;
    private String city;
    private String state;
    private String zip;

    public Person(@NonNull final String firstName, @NonNull final String lastName,
            @NonNull final Gender gender, @NonNull final Date dateOfBirth) {
        if (firstName == null)
            throw new java.lang.NullPointerException("firstName");
        if (lastName == null)
            throw new java.lang.NullPointerException("lastName");
        if (gender == null)
            throw new java.lang.NullPointerException("gender");
        if (dateOfBirth == null)
            throw new java.lang.NullPointerException("dateOfBirth");
        this.firstName = firstName;
        this.lastName = lastName;
        this.gender = gender;
        this.dateOfBirth = dateOfBirth;
    }

    @NonNull
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(@NonNull final String firstName) {
        if (firstName == null)
            throw new java.lang.NullPointerException("firstName");
        this.firstName = firstName;
    }

    @NonNull
    public String getLastName() {
        return lastName;
    }

    public void setLastName(@NonNull final String lastName) {
        if (lastName == null)
            throw new java.lang.NullPointerException("lastName");
        this.lastName = lastName;
    }

    @NonNull
    public Gender getGender() {
        return gender;
    }

    @NonNull
    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public String getSsn() {
        return ssn;
    }

    public void setSsn(final String ssn) {
        this.ssn = ssn;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(final String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(final String city) {
        this.city = city;
    }

    public String getState() {
        return state;
    }

    public void setState(final String state) {
        this.state = state;
    }

    public String getZip() {
        return zip;
    }

    public void setZip(final String zip) {
        this.zip = zip;
    }

    @java.lang.Override
    public java.lang.String toString() {
        return "Person(firstName=" + firstName + ", lastName=" + lastName + 
            ", gender=" + gender + ", dateOfBirth=" + dateOfBirth +
            ", ssn=" + ssn + ", address=" + address + ", city=" + city +
            ", state=" + state + ", zip=" + zip + ")";
    }

    @java.lang.Override
    public boolean equals(final java.lang.Object o) {
        if (o == this) return true;
        if (o == null) return false;
        if (o.getClass() != this.getClass()) return false;
        final Person other = (Person)o;
        if (this.firstName == null
                ? other.firstName != null
                : !this.firstName.equals(other.firstName))
            return false;
        if (this.lastName == null
                ? other.lastName != null
                : !this.lastName.equals(other.lastName))
            return false;
        if (this.gender == null
                ? other.gender != null
                : !this.gender.equals(other.gender))
            return false;
        if (this.dateOfBirth == null
                ? other.dateOfBirth != null
                : !this.dateOfBirth.equals(other.dateOfBirth))
            return false;
        if (this.ssn == null
                ? other.ssn != null
                : !this.ssn.equals(other.ssn))
            return false;
        return true;
    }

    @java.lang.Override
    public int hashCode() {
        final int PRIME = 31;
        int result = 1;
        result = result * PRIME +
            (this.firstName == null ? 0 : this.firstName.hashCode());
        result = result * PRIME +
            (this.lastName == null ? 0 : this.lastName.hashCode());
        result = result * PRIME +
            (this.gender == null ? 0 : this.gender.hashCode());
        result = result * PRIME +
            (this.dateOfBirth == null ? 0 : this.dateOfBirth.hashCode());
        result = result * PRIME +
            (this.ssn == null ? 0 : this.ssn.hashCode());
        return result;
    }
}

results matching ""

    No results matching ""