You should consider using Google's AutoValue library: https://github.com/google/auto/blob/master/value/userguide/index.md
@AutoValue
public abstract class Card {
@Nullable
public abstract Integer localId();
public abstract Long utcStart();
public abstract Integer paramA();
public abstract Integer paramB();
@Nullable
public abstract Boolean paramC();
public static Builder builder() {
return new AutoValue_Card.Builder();
}
@AutoValue.Builder
public abstract static class Builder {
public abstract Builder setLocalId(final Integer localId);
public abstract Builder setUtcStart(final Long utcStart);
public abstract Builder setParamA(final Integer paramA);
public abstract Builder setParamB(final Integer paramB);
public abstract Builder setParamC(final Boolean paramC);
public abstract Card build();
}
}
All the no-mandatory fields can be annotated with @Nullable
.
To create an immutable Card object you just use this:
Card myCard = Card.builder()
.setLocalId(123456) // this line can be omitted
.setUtcStart(158632478000)
.setParamA(5)
.setParamB(58)
.setParamC(true) // this line can be omitted
.build();
"AutoValue is a great tool for eliminating the drudgery of writing mundane value classes in Java. It encapsulates much of the advice in Effective Java Chapter 2, and frees you to concentrate on the more interesting aspects of your program. The resulting program is likely to be shorter, clearer, and freer of bugs. Two thumbs up."
-- Joshua Bloch, author, Effective Java