Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lombok Can we use @Builder and @Value together on a single class

Tags:

java

lombok

First of all thanks to Lombok, our java code is now much sleeker and cleaner. My use case is I want to create an immutable class. For that I would use @Value annotation. Also I want to use builder capabilities, for that I would use @Builder annotation. My question is if we can use both @Builder and @Value together on a single class. Is it a good practice recommended by Lombok users/developers.

like image 737
Anil Avatar asked Aug 13 '15 15:08

Anil


People also ask

Can we use @data and @builder together?

Lombok @Data and @Builder together If you use @Data annotation alone, public required-args constructor is generated. If you are using @Data and @Builder annotations together, all-args constructor (Package access level) is generated.

Does @value make class final?

Overview. @Value is the immutable variant of @Data ; all fields are made private and final by default, and setters are not generated. The class itself is also made final by default, because immutability is not something that can be forced onto a subclass.

Does @builder require AllArgsConstructor?

Martin Grajcar. Your @Builder needs an @AllArgsConstructor (add it; feel free to make it private).

What does @builder in Lombok do?

Overview. Project Lombok's @Builder is a helpful mechanism for using the Builder pattern without writing boilerplate code. We can apply this annotation to a Class or a method. In this quick tutorial, we'll look at the different use cases for @Builder.


1 Answers

Of course you can. To check, simply delombok your code and see what it generates. Take this example:

@Builder
@Value
public class Pair {
    private Object left;
    private Object right;
}

After delombokification, this produces:

public class Pair {
    private Object left;
    private Object right;

    @java.beans.ConstructorProperties({ "left", "right" })
    Pair(Object left, Object right) {
        this.left = left;
        this.right = right;
    }

    public static PairBuilder builder() {
        return new PairBuilder();
    }

    public Object getLeft() {
        return this.left;
    }

    public Object getRight() {
        return this.right;
    }

    public boolean equals(Object o) {
        if (o == this) return true;
        if (!(o instanceof Pair)) return false;
        final Pair other = (Pair) o;
        final Object this$left = this.left;
        final Object other$left = other.left;
        if (this$left == null ? other$left != null : !this$left.equals(other$left)) return false;
        final Object this$right = this.right;
        final Object other$right = other.right;
        if (this$right == null ? other$right != null : !this$right.equals(other$right)) return false;
        return true;
    }

    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        final Object $left = this.left;
        result = result * PRIME + ($left == null ? 0 : $left.hashCode());
        final Object $right = this.right;
        result = result * PRIME + ($right == null ? 0 : $right.hashCode());
        return result;
    }

    public String toString() {
        return "Pair(left=" + this.left + ", right=" + this.right + ")";
    }

    public static class PairBuilder {
        private Object left;
        private Object right;

        PairBuilder() {
        }

        public Pair.PairBuilder left(Object left) {
            this.left = left;
            return this;
        }

        public Pair.PairBuilder right(Object right) {
            this.right = right;
            return this;
        }

        public Pair build() {
            return new Pair(left, right);
        }

        public String toString() {
            return "Pair.PairBuilder(left=" + this.left + ", right=" + this.right + ")";
        }
    }
}

So you can clearly use both @Value and @Builder

like image 124
Justin Avatar answered Oct 05 '22 01:10

Justin