Comparaison des enregistrements Java, des classes de données Lombok @Data et Kotlin

Malgré le fait que les trois solutions vous permettent de traiter le code passe-partout, il y a trÚs peu de points communs entre elles. Les enregistrements ont une sémantique plus forte, dont ils ont des avantages importants. Ce qui en fait souvent le meilleur choix, mais pas toujours.





Je suis sûr que vous avez déjà vu des exemples sur la façon de transformer un POJO régulier à l'aide d'enregistrements ...





class Range {

	private final int low;
	private final int high;

	public Range(int low, int high) {
		this.low = low;
		this.high = high;
	}

	public int getLow() {
		return low;
	}

	public int getHigh() {
		return high;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || getClass() != o.getClass())
			return false;
		Range range = (Range) o;
		return low == range.low &&
				high == range.high;
	}

	@Override
	public int hashCode() {
		return Objects.hash(low, high);
	}

	@Override
	public String toString() {
		return "[" + low + "; " + high + "]";
	}

}
      
      



... en une ligne de code :





//             (components)
record Range (int low, int hight) { }
      
      



, @Data



@Value



Lombok , :





@Data
class Range {

	private final int low;
	private final int high;

}
      
      



Kotlin, , , data-:





data class Range(val low: Int, val high: Int)
      
      



, ? . , .





, . , ,   . — . JEP 395 , - , . .





(records)

JEP 395 :





(records) — , .





, , , , , . , () . — . 





, . (, , @Data / @Value



data-), , , . .





( , .)





(transparency). ( Project Amber):





API , , .





:





  • ( ) , , , ( API )





  • , ( ; API )





  • ( API )





  • ( API , )





Lombok data- Kotlin , "" ( Java, Kotlin ). Java ? , .





(set) — . , , C — {, , ...}, N —  {0, 1, ...}. {-2147483648, ..., 0, ..., 2147483647} — , Java int



. null



, Integer



. ( null



) — String



.





, , — , . , — " , " ( ), — — " " (), — .





class Pair {
	private final int first;
	private final int second;
}
      
      



Pair



, . , . , , . int × int ( ).





, . , , , , , (, . .) .





// given: bijective function from int to int
IntUnaryOperator increment =
	i -> i == Integer.MAX_VALUE ? Integer.MIN_VALUE : ++i;

// then: combining two `increment`s yields a bijective function
//       (this requires no additional proof or consideration)
UnaryOperator<Pair> incrementPair =
	pair -> new Pair(
		increment.applyAsInt(pair.first()),
		increment.applyAsInt(pair.second()));
      
      



Pair::first



Pair::second



? , . / , Pair



. , , , , pair



.





, , , . , "-" ( ), .





. JEP 395 :





.





"" , , . , , int × int



, , Pair(int first, int second)



Range(int low, int high)



. (range.get1()



), (record.low()



).





: - , , . , , . .





, :





  • ( ) .





  • .





  • .





  • .





  • .





, , ,   .





if (range instanceof Range(int low, int high) && high < low)

    return new Range(high, low);
      
      



, . , range — , : low  high — .





with

Range range = new Range(5, 10);
// SYNTAX IS MADE UP!

Range newRange = range with { low = 0; }
// range: [5; 10]
// newRange: [0; 10]
      
      



, , , newRange



, range



low



: , . :





  • (, low



    , high



    )





  • with











( , .)





, JSON / XML- , , . , . , , Reflection API .





, , Inside Java Podcast, episode 14 ( Spotify). , .





. , -, :









  • ( )









, , (0, 0) = (0, 0), equals



, hashCode



.





( toString



) , , , .





. , , , , . , final



. ( ).





, ? . 10% , 90% , .





Lombok @Data/@Value

Lombok . , . , , Lombok, , .





( Lombok. API , , , , , Java. , , .)





data- Kotlin

data-:





, . .





, , , , . , data- , ( "", , ...), Lombok ( , copy, ...). , data- , Kotlin . . 





@JvmRecord Kotlin : ", data- — " ( , ). , . ?





Data- , , , . Kotlin @JvmRecord



data- , data-. , data- —  .





@JvmRecord



? . proposal:





Kotlin JVM-, :





  • Java- Kotlin ABI;





  • Kotlin - , Java reflection .





, case- Scala. , , , , , , , .





. , .






"Java Developer. Professional" : « ».





  • . 1





  • . 2








All Articles