Skip to content

Commit 0c6ec65

Browse files
committed
LastName can be a record
1 parent eb1a704 commit 0c6ec65

File tree

4 files changed

+49
-63
lines changed

4 files changed

+49
-63
lines changed

Diff for: src/test/java/examples/simple/LastName.java

+1-39
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,4 @@
1515
*/
1616
package examples.simple;
1717

18-
public class LastName {
19-
private String name;
20-
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-
}
18+
public record LastName (String name) {}

Diff for: src/test/java/examples/simple/LastNameTypeHandler.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@
2222

2323
import org.apache.ibatis.type.JdbcType;
2424
import org.apache.ibatis.type.TypeHandler;
25+
import org.jspecify.annotations.Nullable;
2526

2627
public class LastNameTypeHandler implements TypeHandler<LastName> {
2728

2829
@Override
29-
public void setParameter(PreparedStatement ps, int i, LastName parameter, JdbcType jdbcType) throws SQLException {
30-
ps.setString(i, parameter == null ? null : parameter.getName());
30+
public void setParameter(PreparedStatement ps, int i, @Nullable LastName parameter, JdbcType jdbcType) throws SQLException {
31+
if (parameter == null) {
32+
ps.setNull(i, jdbcType.TYPE_CODE);
33+
} else {
34+
ps.setString(i, parameter.name());
35+
}
3136
}
3237

3338
@Override
@@ -45,7 +50,7 @@ public LastName getResult(CallableStatement cs, int columnIndex) throws SQLExcep
4550
return toLastName(cs.getString(columnIndex));
4651
}
4752

48-
private LastName toLastName(String s) {
49-
return s == null ? null : LastName.of(s);
53+
private @Nullable LastName toLastName(@Nullable String s) {
54+
return s == null ? null : new LastName(s);
5055
}
5156
}

Diff for: src/test/java/examples/simple/PersonMapperTest.java

+20-20
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ void testFirstNameIn() {
245245
c.where(firstName, isIn("Fred", "Barney")));
246246

247247
assertThat(rows).hasSize(2);
248-
assertThat(rows.get(0).getLastName().getName()).isEqualTo("Flintstone");
249-
assertThat(rows.get(1).getLastName().getName()).isEqualTo("Rubble");
248+
assertThat(rows.get(0).getLastName().name()).isEqualTo("Flintstone");
249+
assertThat(rows.get(1).getLastName().name()).isEqualTo("Rubble");
250250
}
251251
}
252252

@@ -263,8 +263,8 @@ void testOrderByCollection() {
263263
);
264264

265265
assertThat(rows).hasSize(2);
266-
assertThat(rows.get(0).getLastName().getName()).isEqualTo("Rubble");
267-
assertThat(rows.get(1).getLastName().getName()).isEqualTo("Flintstone");
266+
assertThat(rows.get(0).getLastName().name()).isEqualTo("Rubble");
267+
assertThat(rows.get(1).getLastName().name()).isEqualTo("Flintstone");
268268
}
269269
}
270270

@@ -321,7 +321,7 @@ void testInsert() {
321321
PersonRecord row = new PersonRecord();
322322
row.setId(100);
323323
row.setFirstName("Joe");
324-
row.setLastName(LastName.of("Jones"));
324+
row.setLastName(new LastName("Jones"));
325325
row.setBirthDate(new Date());
326326
row.setEmployed(true);
327327
row.setOccupation("Developer");
@@ -339,7 +339,7 @@ void testGeneralInsert() {
339339
int rows = mapper.generalInsert(c ->
340340
c.set(id).toValue(100)
341341
.set(firstName).toValue("Joe")
342-
.set(lastName).toValue(LastName.of("Jones"))
342+
.set(lastName).toValue(new LastName("Jones"))
343343
.set(birthDate).toValue(new Date())
344344
.set(employed).toValue(true)
345345
.set(occupation).toValue("Developer")
@@ -360,7 +360,7 @@ void testInsertMultiple() {
360360
PersonRecord row = new PersonRecord();
361361
row.setId(100);
362362
row.setFirstName("Joe");
363-
row.setLastName(LastName.of("Jones"));
363+
row.setLastName(new LastName("Jones"));
364364
row.setBirthDate(new Date());
365365
row.setEmployed(true);
366366
row.setOccupation("Developer");
@@ -370,7 +370,7 @@ void testInsertMultiple() {
370370
row = new PersonRecord();
371371
row.setId(101);
372372
row.setFirstName("Sarah");
373-
row.setLastName(LastName.of("Smith"));
373+
row.setLastName(new LastName("Smith"));
374374
row.setBirthDate(new Date());
375375
row.setEmployed(true);
376376
row.setOccupation("Architect");
@@ -389,7 +389,7 @@ void testInsertSelective() {
389389
PersonRecord row = new PersonRecord();
390390
row.setId(100);
391391
row.setFirstName("Joe");
392-
row.setLastName(LastName.of("Jones"));
392+
row.setLastName(new LastName("Jones"));
393393
row.setBirthDate(new Date());
394394
row.setEmployed(false);
395395
row.setAddressId(1);
@@ -406,7 +406,7 @@ void testUpdateByPrimaryKey() {
406406
PersonRecord row = new PersonRecord();
407407
row.setId(100);
408408
row.setFirstName("Joe");
409-
row.setLastName(LastName.of("Jones"));
409+
row.setLastName(new LastName("Jones"));
410410
row.setBirthDate(new Date());
411411
row.setEmployed(true);
412412
row.setOccupation("Developer");
@@ -432,7 +432,7 @@ void testUpdateByPrimaryKeySelective() {
432432
PersonRecord row = new PersonRecord();
433433
row.setId(100);
434434
row.setFirstName("Joe");
435-
row.setLastName(LastName.of("Jones"));
435+
row.setLastName(new LastName("Jones"));
436436
row.setBirthDate(new Date());
437437
row.setEmployed(true);
438438
row.setOccupation("Developer");
@@ -462,7 +462,7 @@ void testUpdate() {
462462
PersonRecord row = new PersonRecord();
463463
row.setId(100);
464464
row.setFirstName("Joe");
465-
row.setLastName(LastName.of("Jones"));
465+
row.setLastName(new LastName("Jones"));
466466
row.setBirthDate(new Date());
467467
row.setEmployed(true);
468468
row.setOccupation("Developer");
@@ -493,7 +493,7 @@ void testUpdateOneField() {
493493
PersonRecord row = new PersonRecord();
494494
row.setId(100);
495495
row.setFirstName("Joe");
496-
row.setLastName(LastName.of("Jones"));
496+
row.setLastName(new LastName("Jones"));
497497
row.setBirthDate(new Date());
498498
row.setEmployed(true);
499499
row.setOccupation("Developer");
@@ -521,7 +521,7 @@ void testUpdateAll() {
521521
PersonRecord row = new PersonRecord();
522522
row.setId(100);
523523
row.setFirstName("Joe");
524-
row.setLastName(LastName.of("Jones"));
524+
row.setLastName(new LastName("Jones"));
525525
row.setBirthDate(new Date());
526526
row.setEmployed(true);
527527
row.setOccupation("Developer");
@@ -550,7 +550,7 @@ void testUpdateSelective() {
550550
PersonRecord row = new PersonRecord();
551551
row.setId(100);
552552
row.setFirstName("Joe");
553-
row.setLastName(LastName.of("Jones"));
553+
row.setLastName(new LastName("Jones"));
554554
row.setBirthDate(new Date());
555555
row.setEmployed(true);
556556
row.setOccupation("Developer");
@@ -620,7 +620,7 @@ void testTypeHandledLike() {
620620
PersonMapper mapper = session.getMapper(PersonMapper.class);
621621

622622
List<PersonRecord> rows = mapper.select(c ->
623-
c.where(lastName, isLike(LastName.of("Fl%")))
623+
c.where(lastName, isLike(new LastName("Fl%")))
624624
.orderBy(id));
625625

626626
assertThat(rows).hasSize(3);
@@ -634,7 +634,7 @@ void testTypeHandledNotLike() {
634634
PersonMapper mapper = session.getMapper(PersonMapper.class);
635635

636636
List<PersonRecord> rows = mapper.select(c ->
637-
c.where(lastName, isNotLike(LastName.of("Fl%")))
637+
c.where(lastName, isNotLike(new LastName("Fl%")))
638638
.orderBy(id));
639639

640640
assertThat(rows).hasSize(3);
@@ -654,7 +654,7 @@ void testJoinAllRows() {
654654
assertThat(records.get(0).getId()).isEqualTo(1);
655655
assertThat(records.get(0).getEmployed()).isTrue();
656656
assertThat(records.get(0).getFirstName()).isEqualTo("Fred");
657-
assertThat(records.get(0).getLastName()).isEqualTo(LastName.of("Flintstone"));
657+
assertThat(records.get(0).getLastName()).isEqualTo(new LastName("Flintstone"));
658658
assertThat(records.get(0).getOccupation()).isEqualTo("Brontosaurus Operator");
659659
assertThat(records.get(0).getBirthDate()).isNotNull();
660660
assertThat(records.get(0).getAddress().getId()).isEqualTo(1);
@@ -677,7 +677,7 @@ void testJoinOneRow() {
677677
assertThat(records.get(0).getId()).isEqualTo(1);
678678
assertThat(records.get(0).getEmployed()).isTrue();
679679
assertThat(records.get(0).getFirstName()).isEqualTo("Fred");
680-
assertThat(records.get(0).getLastName()).isEqualTo(LastName.of("Flintstone"));
680+
assertThat(records.get(0).getLastName()).isEqualTo(new LastName("Flintstone"));
681681
assertThat(records.get(0).getOccupation()).isEqualTo("Brontosaurus Operator");
682682
assertThat(records.get(0).getBirthDate()).isNotNull();
683683
assertThat(records.get(0).getAddress().getId()).isEqualTo(1);
@@ -697,7 +697,7 @@ void testJoinPrimaryKey() {
697697
assertThat(r.getId()).isEqualTo(1);
698698
assertThat(r.getEmployed()).isTrue();
699699
assertThat(r.getFirstName()).isEqualTo("Fred");
700-
assertThat(r.getLastName()).isEqualTo(LastName.of("Flintstone"));
700+
assertThat(r.getLastName()).isEqualTo(new LastName("Flintstone"));
701701
assertThat(r.getOccupation()).isEqualTo("Brontosaurus Operator");
702702
assertThat(r.getBirthDate()).isNotNull();
703703
assertThat(r.getAddress().getId()).isEqualTo(1);

Diff for: src/test/java/examples/simple/package-info.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Copyright 2016-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
@NullMarked
17+
package examples.simple;
18+
19+
import org.jspecify.annotations.NullMarked;

0 commit comments

Comments
 (0)