Skip to content

Instantly share code, notes, and snippets.

@upangka
Created December 18, 2019 07:08
Show Gist options
  • Save upangka/eca7d26f2861916322b1a6efe110e608 to your computer and use it in GitHub Desktop.
Save upangka/eca7d26f2861916322b1a6efe110e608 to your computer and use it in GitHub Desktop.
lomlok使用builder时的默认值与手动设置的默认值

仔细看,builder为默认值生成了一个函数

private static BigDecimal $default$account() {
  return BigDecimal.ZERO;
}

以及在builder内部生成了一个标记

private boolean account$set;

该标记在值被更新时记录

  public AccountNumberVoBuilder account(final BigDecimal account) {
    this.account = account;
    account$set = true;
    return this;
  }

并在build时进行检测,是否使用默认值

		@java.lang.SuppressWarnings("all")
		public AccountNumberVo build() {
			BigDecimal account = this.account;
			if (!account$set) account = AccountNumberVo.$default$account();
			return new AccountNumberVo(account, score);
		}
@Data
@Builder
@AllArgsConstructor
public class AccountNumberVo {
@Builder.Default
private BigDecimal account = BigDecimal.ZERO;
private Integer score = 0;
public AccountNumberVo(){
this.account = BigDecimal.ZERO;
this.score = 0;
}
}
// Generated by delombok at Wed Dec 18 14:55:29 CST 2019
import java.math.BigDecimal;
public class AccountNumberVo {
private BigDecimal account;
private Integer score = 0;
public AccountNumberVo() {
this.account = BigDecimal.ZERO;
this.score = 0;
}
@java.lang.SuppressWarnings("all")
private static BigDecimal $default$account() {
return BigDecimal.ZERO;
}
@java.lang.SuppressWarnings("all")
public static class AccountNumberVoBuilder {
@java.lang.SuppressWarnings("all")
private boolean account$set;
@java.lang.SuppressWarnings("all")
private BigDecimal account;
@java.lang.SuppressWarnings("all")
private Integer score;
@java.lang.SuppressWarnings("all")
AccountNumberVoBuilder() {
}
@java.lang.SuppressWarnings("all")
public AccountNumberVoBuilder account(final BigDecimal account) {
this.account = account;
account$set = true;
return this;
}
@java.lang.SuppressWarnings("all")
public AccountNumberVoBuilder score(final Integer score) {
this.score = score;
return this;
}
@java.lang.SuppressWarnings("all")
public AccountNumberVo build() {
BigDecimal account = this.account;
if (!account$set) account = AccountNumberVo.$default$account();
return new AccountNumberVo(account, score);
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "AccountNumberVo.AccountNumberVoBuilder(account=" + this.account + ", score=" + this.score + ")";
}
}
@java.lang.SuppressWarnings("all")
public static AccountNumberVoBuilder builder() {
return new AccountNumberVoBuilder();
}
@java.lang.SuppressWarnings("all")
public BigDecimal getAccount() {
return this.account;
}
@java.lang.SuppressWarnings("all")
public Integer getScore() {
return this.score;
}
@java.lang.SuppressWarnings("all")
public void setAccount(final BigDecimal account) {
this.account = account;
}
@java.lang.SuppressWarnings("all")
public void setScore(final Integer score) {
this.score = score;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public boolean equals(final java.lang.Object o) {
if (o == this) return true;
if (!(o instanceof AccountNumberVo)) return false;
final AccountNumberVo other = (AccountNumberVo) o;
if (!other.canEqual((java.lang.Object) this)) return false;
final java.lang.Object this$account = this.getAccount();
final java.lang.Object other$account = other.getAccount();
if (this$account == null ? other$account != null : !this$account.equals(other$account)) return false;
final java.lang.Object this$score = this.getScore();
final java.lang.Object other$score = other.getScore();
if (this$score == null ? other$score != null : !this$score.equals(other$score)) return false;
return true;
}
@java.lang.SuppressWarnings("all")
protected boolean canEqual(final java.lang.Object other) {
return other instanceof AccountNumberVo;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public int hashCode() {
final int PRIME = 59;
int result = 1;
final java.lang.Object $account = this.getAccount();
result = result * PRIME + ($account == null ? 43 : $account.hashCode());
final java.lang.Object $score = this.getScore();
result = result * PRIME + ($score == null ? 43 : $score.hashCode());
return result;
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "AccountNumberVo(account=" + this.getAccount() + ", score=" + this.getScore() + ")";
}
@java.lang.SuppressWarnings("all")
public AccountNumberVo(final BigDecimal account, final Integer score) {
this.account = account;
this.score = score;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment