Skip to content

Commit 65d0c77

Browse files
committed
Convert to records in the Spring example
1 parent c0d769c commit 65d0c77

8 files changed

+164
-399
lines changed

src/test/java/examples/spring/AddressRecord.java

+1-38
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,4 @@
1515
*/
1616
package examples.spring;
1717

18-
public class AddressRecord {
19-
private Integer id;
20-
private String streetAddress;
21-
private String city;
22-
private String state;
23-
24-
public Integer getId() {
25-
return id;
26-
}
27-
28-
public void setId(Integer id) {
29-
this.id = id;
30-
}
31-
32-
public String getStreetAddress() {
33-
return streetAddress;
34-
}
35-
36-
public void setStreetAddress(String streetAddress) {
37-
this.streetAddress = streetAddress;
38-
}
39-
40-
public String getCity() {
41-
return city;
42-
}
43-
44-
public void setCity(String city) {
45-
this.city = city;
46-
}
47-
48-
public String getState() {
49-
return state;
50-
}
51-
52-
public void setState(String state) {
53-
this.state = state;
54-
}
55-
}
18+
public record AddressRecord (Integer id, String streetAddress, String city, String state) { }

src/test/java/examples/spring/LastName.java

+2-38
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,6 @@
1515
*/
1616
package examples.spring;
1717

18-
public class LastName {
19-
private String name;
18+
import org.jspecify.annotations.Nullable;
2019

21-
public String getName() {
22-
return name;
23-
}
24-
25-
public void setName(String name) {
26-
this.name = name;
27-
}
28-
29-
public static LastName of(String name) {
30-
LastName lastName = new LastName();
31-
lastName.setName(name);
32-
return lastName;
33-
}
34-
35-
@Override
36-
public int hashCode() {
37-
final int prime = 31;
38-
int result = 1;
39-
result = prime * result + ((name == null) ? 0 : name.hashCode());
40-
return result;
41-
}
42-
43-
@Override
44-
public boolean equals(Object obj) {
45-
if (this == obj)
46-
return true;
47-
if (obj == null)
48-
return false;
49-
if (getClass() != obj.getClass())
50-
return false;
51-
LastName other = (LastName) obj;
52-
if (name == null) {
53-
return other.name == null;
54-
} else return name.equals(other.name);
55-
}
56-
}
20+
public record LastName(@Nullable String name) { }

src/test/java/examples/spring/LastNameParameterConverter.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,18 @@
1515
*/
1616
package examples.spring;
1717

18-
import org.jspecify.annotations.NullMarked;
1918
import org.jspecify.annotations.Nullable;
2019
import org.mybatis.dynamic.sql.ParameterTypeConverter;
2120
import org.springframework.core.convert.converter.Converter;
2221

23-
@NullMarked
2422
public class LastNameParameterConverter implements ParameterTypeConverter<LastName, String>,
2523
Converter<LastName, String> {
2624
@Override
2725
public @Nullable String convert(LastName source) {
28-
if ("Slate".equals(source.getName())) {
26+
if ("Slate".equals(source.name())) {
2927
return null;
3028
} else {
31-
return source.getName();
29+
return source.name();
3230
}
3331
}
3432
}

src/test/java/examples/spring/PersonRecord.java

+9-64
Original file line numberDiff line numberDiff line change
@@ -15,78 +15,23 @@
1515
*/
1616
package examples.spring;
1717

18-
import java.util.Date;
19-
20-
public class PersonRecord {
21-
private Integer id;
22-
private String firstName;
23-
private LastName lastName;
24-
private Date birthDate;
25-
private Boolean employed;
26-
private String occupation;
27-
private Integer addressId;
28-
29-
public Integer getId() {
30-
return id;
31-
}
32-
33-
public void setId(Integer id) {
34-
this.id = id;
35-
}
36-
37-
public String getFirstName() {
38-
return firstName;
39-
}
40-
41-
public void setFirstName(String firstName) {
42-
this.firstName = firstName;
43-
}
44-
45-
public LastName getLastName() {
46-
return lastName;
47-
}
48-
49-
public String getLastNameAsString() {
50-
return lastName == null ? null : lastName.getName();
51-
}
18+
import org.jspecify.annotations.Nullable;
5219

53-
public void setLastName(LastName lastName) {
54-
this.lastName = lastName;
55-
}
56-
57-
public Date getBirthDate() {
58-
return birthDate;
59-
}
60-
61-
public void setBirthDate(Date birthDate) {
62-
this.birthDate = birthDate;
63-
}
64-
65-
public String getOccupation() {
66-
return occupation;
67-
}
20+
import java.util.Date;
6821

69-
public void setOccupation(String occupation) {
70-
this.occupation = occupation;
71-
}
22+
public record PersonRecord (Integer id, @Nullable String firstName, @Nullable LastName lastName,
23+
@Nullable Date birthDate, @Nullable Boolean employed,
24+
@Nullable String occupation, @Nullable Integer addressId) {
7225

73-
public Boolean getEmployed() {
74-
return employed;
26+
public @Nullable String getLastNameAsString() {
27+
return lastName == null ? null : lastName.name();
7528
}
7629

7730
public String getEmployedAsString() {
7831
return employed == null ? "No" : employed ? "Yes" : "No";
7932
}
8033

81-
public void setEmployed(Boolean employed) {
82-
this.employed = employed;
83-
}
84-
85-
public Integer getAddressId() {
86-
return addressId;
87-
}
88-
89-
public void setAddressId(Integer addressId) {
90-
this.addressId = addressId;
34+
public PersonRecord withOccupation(String occupation) {
35+
return new PersonRecord(id, firstName, lastName, birthDate, employed, occupation, addressId);
9136
}
9237
}

0 commit comments

Comments
 (0)