Many people have written classes in Java that have only fields, constructors, and getters (Setter, equals, toString in some cases). Such classes are called data classes. In some cases, you have to write a lot of similar classes, and no matter how much you use the method generation function of the IDE, it's annoying. If you use lombok, it will be alleviated to some extent, but in many cases you will have to set the IDE, etc., and it will still be troublesome.
Meanwhile, it seems that a function to solve this problem will be added in Java 14.
Java 14 introduces something called Record
.
https://blogs.oracle.com/javamagazine/records-come-to-java
The usage seems to declare the class you want to be a data class as record
instead of class
.
before use
FXOrderClassc.java
public final class FXOrderClassic {
private final int units;
private final CurrencyPair pair;
private final Side side;
private final double price;
private final LocalDateTime sentAt;
private final int ttl;
public FXOrderClassic(int units,
CurrencyPair pair,
Side side,
double price,
LocalDateTime sentAt,
int ttl) {
this.units = units;
this.pair = pair; // CurrencyPair is a simple enum
this.side = side; // Side is a simple enum
this.price = price;
this.sentAt = sentAt;
this.ttl = ttl;
}
public int units() {
return units;
}
public CurrencyPair pair() {
return pair;
}
public Side side() {
return side;
}
public double price() { return price; }
public LocalDateTime sentAt() {
return sentAt;
}
public int ttl() {
return ttl;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass())
return false;
FXOrderClassic that = (FXOrderClassic) o;
if (units != that.units) return false;
if (Double.compare(that.price, price) != 0)
return false;
if (ttl != that.ttl) return false;
if (pair != that.pair) return false;
if (side != that.side) return false;
return sentAt != null ?
sentAt.equals(that.sentAt) : that.sentAt == null;
}
@Override
public int hashCode() {
int result;
long temp;
result = units;
result = 31 * result +
(pair != null ? pair.hashCode() : 0);
result = 31 * result +
(side != null ? side.hashCode() : 0);
temp = Double.doubleToLongBits(price);
result = 31 * result +
(int) (temp ^ (temp >>> 32));
result = 31 * result +
(sentAt != null ? sentAt.hashCode() : 0);
result = 31 * result + ttl;
return result;
}
@Override
public String toString() {
return "FXOrderClassic{" +
"units=" + units +
", pair=" + pair +
", side=" + side +
", price=" + price +
", sentAt=" + sentAt +
", ttl=" + ttl +
'}';
}
}
I'm tired of seeing a lot of getters, constructors, and override methods side by side. This is what Java 14 does.
FXOrder.java
public record FXOrder(int units,
CurrencyPair pair,
Side side,
double price,
LocalDateTime sentAt,
int ttl) {}
~~ Like Kotlin ~~ You will find that you will be able to declare it very neatly. However, it seems that Setter is not prepared, so be careful when using OR mapper etc.
~~ I think Kotlin is good ~~
It seems that Java 14 will introduce a powerful new feature called Record
.
With this, it seems that legacy code can be rewritten concisely.
Personally, I definitely want to use this feature, and I'm thinking of updating from Java 8!
Recommended Posts