Skip to content

Commit 7938ee4

Browse files
committed
Java 16 migration for Cassandra examples.
See #606.
1 parent 23dcae3 commit 7938ee4

23 files changed

+109
-167
lines changed

cassandra/example/src/main/java/example/springdata/cassandra/auditing/BasicConfiguration.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ AuditorAware<String> auditorAware() {
5757
public MappingCassandraConverter cassandraConverter(CassandraMappingContext mapping,
5858
CassandraCustomConversions conversions, CqlSession session) {
5959

60-
MappingCassandraConverter converter = new MappingCassandraConverter(mapping);
60+
var converter = new MappingCassandraConverter(mapping);
6161

6262
converter.setCodecRegistry(session.getContext().getCodecRegistry());
6363
converter.setCustomConversions(conversions);

cassandra/example/src/main/java/example/springdata/cassandra/convert/Address.java

+1-11
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
*/
1616
package example.springdata.cassandra.convert;
1717

18-
import lombok.AllArgsConstructor;
19-
import lombok.Data;
20-
import lombok.NoArgsConstructor;
21-
2218
import org.springframework.data.cassandra.core.mapping.Element;
2319
import org.springframework.data.cassandra.core.mapping.Tuple;
2420

@@ -27,13 +23,7 @@
2723
*
2824
* @author Mark Paluch
2925
*/
30-
@Data
31-
@AllArgsConstructor
32-
@NoArgsConstructor
3326
@Tuple
34-
public class Address {
27+
public record Address(@Element(0) String address, @Element(1) String city, @Element(2) String zip) {
3528

36-
@Element(0) String address;
37-
@Element(1) String city;
38-
@Element(2) String zip;
3929
}

cassandra/example/src/main/java/example/springdata/cassandra/convert/Contact.java

+1-10
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,11 @@
1515
*/
1616
package example.springdata.cassandra.convert;
1717

18-
import lombok.AllArgsConstructor;
19-
import lombok.Data;
20-
import lombok.NoArgsConstructor;
21-
2218
/**
2319
* Sample Contact class.
2420
*
2521
* @author Mark Paluch
2622
*/
27-
@Data
28-
@AllArgsConstructor
29-
@NoArgsConstructor
30-
public class Contact {
23+
public record Contact(String firstname, String lastname) {
3124

32-
String firstname;
33-
String lastname;
3425
}

cassandra/example/src/main/java/example/springdata/cassandra/convert/ConverterConfiguration.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,7 @@ static class CustomAddressbookReadConverter implements Converter<Row, CustomAddr
9393

9494
public CustomAddressbook convert(Row source) {
9595

96-
CustomAddressbook result = new CustomAddressbook();
97-
98-
result.setTheId(source.getString("id"));
99-
result.setMyDetailsAsJson(source.getString("me"));
100-
101-
return result;
96+
return new CustomAddressbook(source.getString("id"), source.getString("me"));
10297
}
10398
}
10499

cassandra/example/src/main/java/example/springdata/cassandra/convert/CustomAddressbook.java

+1-6
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,9 @@
1515
*/
1616
package example.springdata.cassandra.convert;
1717

18-
import lombok.Data;
19-
2018
/**
2119
* @author Mark Paluch
2220
*/
23-
@Data
24-
public class CustomAddressbook {
21+
public record CustomAddressbook(String theId, String myDetailsAsJson) {
2522

26-
String theId;
27-
String myDetailsAsJson;
2823
}

cassandra/example/src/main/java/example/springdata/cassandra/events/User.java

+1-12
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515
*/
1616
package example.springdata.cassandra.events;
1717

18-
import lombok.AllArgsConstructor;
19-
import lombok.Data;
20-
import lombok.NoArgsConstructor;
21-
2218
import org.springframework.data.cassandra.core.mapping.PrimaryKey;
2319
import org.springframework.data.cassandra.core.mapping.Table;
2420

@@ -27,14 +23,7 @@
2723
*
2824
* @author Mark Paluch
2925
*/
30-
@Data
31-
@NoArgsConstructor
32-
@AllArgsConstructor
3326
@Table(value = "users")
34-
public class User {
35-
36-
private @PrimaryKey long id;
27+
public record User(@PrimaryKey long id, String firstname, String lastname) {
3728

38-
private String firstname;
39-
private String lastname;
4029
}

cassandra/example/src/main/java/example/springdata/cassandra/optimisticlocking/OptimisticPerson.java

+4-10
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
*/
1616
package example.springdata.cassandra.optimisticlocking;
1717

18-
import lombok.Data;
19-
2018
import org.springframework.data.annotation.Id;
2119
import org.springframework.data.annotation.Version;
2220
import org.springframework.data.cassandra.core.mapping.Table;
@@ -27,14 +25,10 @@
2725
*
2826
* @author Mark Paluch
2927
*/
30-
@Data
3128
@Table
32-
public class OptimisticPerson {
33-
34-
@Id Long id;
35-
36-
@Version long version;
37-
38-
String name;
29+
public record OptimisticPerson(@Id Long id, @Version long version, String name) {
3930

31+
public OptimisticPerson withName(String name) {
32+
return new OptimisticPerson(id, version, name);
33+
}
4034
}

cassandra/example/src/main/java/example/springdata/cassandra/projection/Customer.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,7 @@
2323
/**
2424
* @author Mark Paluch
2525
*/
26-
@Value
2726
@Table
28-
class Customer {
27+
record Customer(@Id String id, String firstname, String lastname) {
2928

30-
@Id String id;
31-
String firstname, lastname;
3229
}

cassandra/example/src/main/java/example/springdata/cassandra/udt/Address.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,12 @@
1515
*/
1616
package example.springdata.cassandra.udt;
1717

18-
import lombok.AllArgsConstructor;
19-
import lombok.Data;
20-
2118
import org.springframework.data.cassandra.core.mapping.UserDefinedType;
2219

2320
/**
2421
* @author Mark Paluch
2522
*/
26-
@Data
2723
@UserDefinedType
28-
@AllArgsConstructor
29-
public class Address {
24+
public record Address(String street, String zip, String city) {
3025

31-
String street, zip, city;
3226
}

cassandra/example/src/test/java/example/springdata/cassandra/auditing/AuditedPersonRepositoryTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ void setUp() {
5050
@Test
5151
void insertShouldSetCreatedDate() {
5252

53-
AuditedPerson person = new AuditedPerson();
53+
var person = new AuditedPerson();
5454
person.setId(42L);
5555
person.setNew(true); // Cassandra does not support auto-generation hence we need
5656
// to supply whether our object is a new one.
57-
AuditedPerson saved = repository.save(person);
57+
var saved = repository.save(person);
5858

5959
assertThat(saved.getCreatedBy()).isEqualTo("Some user");
6060
assertThat(saved.getLastModifiedBy()).isEqualTo("Some user");
@@ -70,15 +70,15 @@ void insertShouldSetCreatedDate() {
7070
@Test
7171
void updateShouldSetLastModifiedDate() {
7272

73-
AuditedPerson person = new AuditedPerson();
73+
var person = new AuditedPerson();
7474
person.setId(42L);
7575
person.setNew(true); // Cassandra does not support auto-generation hence we need
7676
// to supply whether our object is a new one.
7777
repository.save(person);
7878

7979
person.setNew(false);
8080

81-
AuditedPerson modified = repository.save(person);
81+
var modified = repository.save(person);
8282

8383
assertThat(modified.getCreatedBy()).isEqualTo("Some user");
8484
assertThat(modified.getLastModifiedBy()).isEqualTo("Some user");

cassandra/example/src/test/java/example/springdata/cassandra/basic/CassandraOperationsIntegrationTests.java

+19-19
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@
4646
*/
4747
@SpringBootTest(classes = BasicConfiguration.class)
4848
@CassandraKeyspace
49-
public class CassandraOperationsIntegrationTests {
49+
class CassandraOperationsIntegrationTests {
5050

5151

5252
@Autowired CqlSession session;
5353
@Autowired CassandraOperations template;
5454

5555
@BeforeEach
56-
public void setUp() throws Exception {
56+
void setUp() throws Exception {
5757
template.getCqlOperations().execute("TRUNCATE users");
5858
}
5959

@@ -62,20 +62,20 @@ public void setUp() throws Exception {
6262
* mapping layer.
6363
*/
6464
@Test
65-
public void insertAndSelect() {
65+
void insertAndSelect() {
6666

67-
Insert insert = QueryBuilder.insertInto("users").value("user_id", QueryBuilder.literal(42L)) //
67+
var insert = QueryBuilder.insertInto("users").value("user_id", QueryBuilder.literal(42L)) //
6868
.value("uname", QueryBuilder.literal("heisenberg")) //
6969
.value("fname", QueryBuilder.literal("Walter")) //
7070
.value("lname", QueryBuilder.literal("White")) //
7171
.ifNotExists(); //
7272

7373
template.getCqlOperations().execute(insert.asCql());
7474

75-
User user = template.selectOneById(42L, User.class);
75+
var user = template.selectOneById(42L, User.class);
7676
assertThat(user.getUsername()).isEqualTo("heisenberg");
7777

78-
List<User> users = template.select(QueryBuilder.selectFrom("users").all().asCql(), User.class);
78+
var users = template.select(QueryBuilder.selectFrom("users").all().asCql(), User.class);
7979
assertThat(users).hasSize(1);
8080
assertThat(users.get(0)).isEqualTo(user);
8181
}
@@ -85,9 +85,9 @@ public void insertAndSelect() {
8585
* {@code select}.
8686
*/
8787
@Test
88-
public void insertAndUpdate() {
88+
void insertAndUpdate() {
8989

90-
User user = new User();
90+
var user = new User();
9191
user.setId(42L);
9292
user.setUsername("heisenberg");
9393
user.setFirstname("Walter");
@@ -98,7 +98,7 @@ public void insertAndUpdate() {
9898
user.setFirstname(null);
9999
template.update(user);
100100

101-
User loaded = template.selectOneById(42L, User.class);
101+
var loaded = template.selectOneById(42L, User.class);
102102
assertThat(loaded.getUsername()).isEqualTo("heisenberg");
103103
assertThat(loaded.getFirstname()).isNull();
104104
}
@@ -107,25 +107,25 @@ public void insertAndUpdate() {
107107
* Asynchronous query execution using callbacks.
108108
*/
109109
@Test
110-
public void insertAsynchronously() throws InterruptedException {
110+
void insertAsynchronously() throws InterruptedException {
111111

112-
User user = new User();
112+
var user = new User();
113113
user.setId(42L);
114114
user.setUsername("heisenberg");
115115
user.setFirstname("Walter");
116116
user.setLastname("White");
117117

118-
final CountDownLatch countDownLatch = new CountDownLatch(1);
118+
final var countDownLatch = new CountDownLatch(1);
119119

120-
AsyncCassandraTemplate asyncTemplate = new AsyncCassandraTemplate(session);
120+
var asyncTemplate = new AsyncCassandraTemplate(session);
121121

122-
ListenableFuture<User> future = asyncTemplate.insert(user);
122+
var future = asyncTemplate.insert(user);
123123

124124
future.addCallback(it -> countDownLatch.countDown(), throwable -> countDownLatch.countDown());
125125

126126
countDownLatch.await(5, TimeUnit.SECONDS);
127127

128-
User loaded = template.selectOneById(user.getId(), User.class);
128+
var loaded = template.selectOneById(user.getId(), User.class);
129129
assertThat(loaded).isEqualTo(user);
130130
}
131131

@@ -135,20 +135,20 @@ public void insertAsynchronously() throws InterruptedException {
135135
*/
136136
@Test
137137
@SuppressWarnings("unchecked")
138-
public void selectProjections() {
138+
void selectProjections() {
139139

140-
User user = new User();
140+
var user = new User();
141141
user.setId(42L);
142142
user.setUsername("heisenberg");
143143
user.setFirstname("Walter");
144144
user.setLastname("White");
145145

146146
template.insert(user);
147147

148-
Long id = template.selectOne(QueryBuilder.selectFrom("users").column("user_id").asCql(), Long.class);
148+
var id = template.selectOne(QueryBuilder.selectFrom("users").column("user_id").asCql(), Long.class);
149149
assertThat(id).isEqualTo(user.getId());
150150

151-
Row row = template.selectOne(QueryBuilder.selectFrom("users").column("user_id").asCql(), Row.class);
151+
var row = template.selectOne(QueryBuilder.selectFrom("users").column("user_id").asCql(), Row.class);
152152
assertThat(row.getLong(0)).isEqualTo(user.getId());
153153

154154
Map<String, Object> map = template.selectOne(QueryBuilder.selectFrom("users").all().asCql(), Map.class);

0 commit comments

Comments
 (0)