java-record

Meaning of “shallowly immutable” in the documentation of Record in Java 14

社会主义新天地 提交于 2021-02-17 21:10:56
问题 I am reading the documentation of Records and don't understand the term "shallowly immutable". What do we mean by shallowly immutable ? And if it's immutable why we need a copy constructor? Why two "Hello Worlds!"? For all record classes, the following invariant must hold: if a record R's components are c1, c2, ... cn, then if a record instance is copied as follows: R copy = new R(r.c1(), r.c2(), ..., r.cn()); // copy constructor ? then it must be the case that r.equals(copy) . 回答1: Shallowly

Java records with nullable components

大兔子大兔子 提交于 2021-02-04 15:39:07
问题 I really like the addition of records in Java 14, at least as a preview feature, as it helps to reduce my need to use lombok for simple, immutable "data holders". But I'm having an issue with the implementation of nullable components. I'm trying to avoid returning null in my codebase to indicate that a value might not be present. Therefore I currently often use something like the following pattern with lombok. @Value public class MyClass { String id; @Nullable String value; Optional<String>

Validating the POJO record with Micronaut not working

那年仲夏 提交于 2021-01-29 09:04:43
问题 Using Micronaut bean validation for the record class is not working compile 'io.micronaut:micronaut-validation:2.2.1' Record class @Introspected public record ProductViewModel ( @JsonProperty("id") String id, @JsonProperty("name") @NotBlank @NotNull String name, @JsonProperty("description") @NotBlank String description, @JsonProperty("price") @NotBlank float price ) { } CURL curl --location --request POST 'http://localhost:8084/api/v1/product' \ --header 'Content-Type: application/json' \ -

“Records” preview feature in IntelliJ 2020.1 with Java 14 fails with compiler error during Maven `install`, but runs otherwise

别说谁变了你拦得住时间么 提交于 2020-06-23 11:01:53
问题 I am trying to use the JEP 359: Records (Preview) feature in Java with IntelliJ 2020.1.1 RC. I defined a class like this: package work.basil.example; import java.time.LocalTime; public record LocalTimeRange(LocalTime start , LocalTime stop) { } When I run a main method in another class using this LocalTimeRange class, no problem. When I do a Maven install I get this error: Error:(6,8) java: records are a preview feature and are disabled by default. ➥ How can I help Maven complete its install

“Records” preview feature in IntelliJ 2020.1 with Java 14 fails with compiler error during Maven `install`, but runs otherwise

依然范特西╮ 提交于 2020-06-23 10:57:45
问题 I am trying to use the JEP 359: Records (Preview) feature in Java with IntelliJ 2020.1.1 RC. I defined a class like this: package work.basil.example; import java.time.LocalTime; public record LocalTimeRange(LocalTime start , LocalTime stop) { } When I run a main method in another class using this LocalTimeRange class, no problem. When I do a Maven install I get this error: Error:(6,8) java: records are a preview feature and are disabled by default. ➥ How can I help Maven complete its install

Define default constructor for record

丶灬走出姿态 提交于 2020-05-24 20:16:56
问题 I have a record and want to add default constructor to it. public record Record(int recordId) { public Record { } } But it created constructor with int param. public final class Record extends java.lang.Record { private final int recordId; public Record(int); //other method } How can we add a default constructor to a record? 回答1: To split hairs, you cannot ever define a default constructor, because a default constructor is generated by the compiler when there are no constructors defined, thus