Skip to content

Commit ccd2fba

Browse files
committed
DATACASS-594 Polishing
1 parent e950084 commit ccd2fba

File tree

7 files changed

+114
-91
lines changed

7 files changed

+114
-91
lines changed

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/cql/CassandraAccessor.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public class CassandraAccessor implements InitializingBean {
8585
*/
8686
private @Nullable ConsistencyLevel serialConsistencyLevel;
8787

88-
private @Nullable SessionFactory sessionFactory;
88+
private SessionFactory sessionFactory;
8989

9090
/**
9191
* Ensures the Cassandra {@link CqlSession} and exception translator has been propertly set.
@@ -314,7 +314,6 @@ public void setSessionFactory(SessionFactory sessionFactory) {
314314
* @since 2.0
315315
* @see SessionFactory
316316
*/
317-
@Nullable
318317
public SessionFactory getSessionFactory() {
319318
return this.sessionFactory;
320319
}
@@ -361,11 +360,10 @@ protected Statement<?> applyStatementSettings(Statement<?> statement) {
361360
}
362361

363362
if (keyspace != null) {
364-
if (statementToUse instanceof BatchStatement) {
365-
statementToUse = ((BatchStatement) statementToUse).setKeyspace(keyspace);
366-
}
367-
if (statementToUse instanceof SimpleStatement) {
368-
statementToUse = ((SimpleStatement) statementToUse).setKeyspace(keyspace);
363+
if (statementToUse instanceof BatchStatement bs) {
364+
statementToUse = bs.setKeyspace(keyspace);
365+
} else if (statementToUse instanceof SimpleStatement ss) {
366+
statementToUse = ss.setKeyspace(keyspace);
369367
}
370368
}
371369

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/cql/CqlTemplate.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ private CqlSession getCurrentSession() {
620620

621621
SessionFactory sessionFactory = getSessionFactory();
622622

623-
Assert.state(sessionFactory != null, "SessionFactory is null");
623+
Assert.notNull(sessionFactory, "SessionFactory is null");
624624

625625
return sessionFactory.getSession();
626626
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.springframework.data.cassandra.core.cql.util;
2+
3+
import java.util.Collection;
4+
import java.util.Collections;
5+
import java.util.Optional;
6+
7+
import org.jetbrains.annotations.NotNull;
8+
import org.springframework.lang.NonNull;
9+
10+
import com.datastax.oss.driver.api.querybuilder.term.Term;
11+
import com.datastax.oss.driver.internal.querybuilder.CqlHelper;
12+
13+
/**
14+
* Represents an abstract collection like {@link Term} such as Set, List, Tuple in CQL
15+
*
16+
* @author Mikhail Polivakha
17+
*/
18+
public abstract class AbstractCollectionTerm implements Term {
19+
20+
@NonNull
21+
private final Collection<? extends Term> components;
22+
23+
/**
24+
* @return EnclosingLiterals that are used to render the collection of terms
25+
*/
26+
public abstract EnclosingLiterals enclosingLiterals();
27+
28+
public AbstractCollectionTerm(Collection<? extends Term> components) {
29+
this.components = Optional.ofNullable(components).orElse(Collections.emptySet());
30+
}
31+
32+
@Override
33+
public boolean isIdempotent() {
34+
return components.stream().allMatch(Term::isIdempotent);
35+
}
36+
37+
@Override
38+
public void appendTo(@NotNull StringBuilder builder) {
39+
EnclosingLiterals literals = this.enclosingLiterals();
40+
41+
if (components.isEmpty()) {
42+
builder.append(literals.prefix).append(literals.postfix);
43+
} else {
44+
CqlHelper.append(components, builder, literals.prefix, ",", literals.postfix);
45+
}
46+
}
47+
48+
protected static class EnclosingLiterals {
49+
50+
private final String prefix;
51+
private final String postfix;
52+
53+
protected EnclosingLiterals(String prefix, String postfix) {
54+
this.prefix = prefix;
55+
this.postfix = postfix;
56+
}
57+
58+
protected static EnclosingLiterals of(String prefix, String postfix) {
59+
return new EnclosingLiterals(prefix, postfix);
60+
}
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.springframework.data.cassandra.core.cql.util;
2+
3+
import java.util.Collection;
4+
5+
import com.datastax.oss.driver.api.querybuilder.term.Term;
6+
7+
public class ListTerm extends AbstractCollectionTerm {
8+
9+
public ListTerm(Collection<? extends Term> components) {
10+
super(components);
11+
}
12+
13+
@Override
14+
public EnclosingLiterals enclosingLiterals() {
15+
return EnclosingLiterals.of("[", "]");
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.springframework.data.cassandra.core.cql.util;
2+
3+
import java.util.Collection;
4+
5+
import com.datastax.oss.driver.api.querybuilder.term.Term;
6+
7+
public class SetTerm extends AbstractCollectionTerm {
8+
9+
public SetTerm(Collection<? extends Term> components) {
10+
super(components);
11+
}
12+
13+
@Override
14+
public EnclosingLiterals enclosingLiterals() {
15+
return EnclosingLiterals.of("{", "}");
16+
}
17+
}

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/cql/util/StatementBuilder.java

+10-82
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.function.Consumer;
2525
import java.util.function.Function;
2626
import java.util.function.UnaryOperator;
27+
import java.util.stream.Collectors;
2728

2829
import org.springframework.lang.NonNull;
2930
import org.springframework.lang.Nullable;
@@ -40,7 +41,7 @@
4041
/**
4142
* Functional builder for Cassandra {@link BuildableQuery statements}. Statements are built by applying
4243
* {@link UnaryOperator builder functions} that get applied when {@link #build() building} the actual
43-
* {@link SimpleStatement statement}. The {@code StatementBuilder} provides a mutable container for statement creation
44+
* {@link SimpleStatement statement}. The {@link StatementBuilder} provides a mutable container for statement creation
4445
* allowing a functional declaration of actions that are necessary to build a statement. This class helps building CQL
4546
* statements as a {@link BuildableQuery} classes are typically immutable and require return value tracking across
4647
* methods that want to apply modifications to a statement.
@@ -271,13 +272,13 @@ public boolean canBindCollection() {
271272

272273
private SimpleStatement build(SimpleStatementBuilder builder) {
273274

274-
SimpleStatement statmentToUse = onBuild(builder).build();
275+
SimpleStatement statementToUse = onBuild(builder).build();
275276

276277
for (UnaryOperator<SimpleStatement> operator : onBuilt) {
277-
statmentToUse = operator.apply(statmentToUse);
278+
statementToUse = operator.apply(statementToUse);
278279
}
279280

280-
return statmentToUse;
281+
return statementToUse;
281282
}
282283

283284
private SimpleStatementBuilder onBuild(SimpleStatementBuilder statementBuilder) {
@@ -290,26 +291,13 @@ private SimpleStatementBuilder onBuild(SimpleStatementBuilder statementBuilder)
290291
@SuppressWarnings("unchecked")
291292
private static Term toLiteralTerms(@Nullable Object value, CodecRegistry codecRegistry) {
292293

293-
if (value instanceof List) {
294+
if (value instanceof Collection<?> c) {
294295

295-
List<Term> terms = new ArrayList<>();
296+
List<Term> mappedTerms = c.stream()
297+
.map(o -> toLiteralTerms(o, codecRegistry))
298+
.toList();
296299

297-
for (Object o : (List<Object>) value) {
298-
terms.add(toLiteralTerms(o, codecRegistry));
299-
}
300-
301-
return new ListTerm(terms);
302-
}
303-
304-
if (value instanceof Set) {
305-
306-
List<Term> terms = new ArrayList<>();
307-
308-
for (Object o : (Set<Object>) value) {
309-
terms.add(toLiteralTerms(o, codecRegistry));
310-
}
311-
312-
return new SetTerm(terms);
300+
return c instanceof Set ? new SetTerm(mappedTerms) : new ListTerm(mappedTerms);
313301
}
314302

315303
if (value instanceof Map) {
@@ -369,66 +357,6 @@ public enum ParameterHandling {
369357
BY_NAME
370358
}
371359

372-
static class ListTerm implements Term {
373-
374-
private final Collection<? extends Term> components;
375-
376-
public ListTerm(@NonNull Collection<? extends Term> components) {
377-
this.components = components;
378-
}
379-
380-
@Override
381-
public void appendTo(@NonNull StringBuilder builder) {
382-
383-
if (components.isEmpty()) {
384-
builder.append("[]");
385-
return;
386-
}
387-
388-
CqlHelper.append(components, builder, "[", ",", "]");
389-
}
390-
391-
@Override
392-
public boolean isIdempotent() {
393-
for (Term component : components) {
394-
if (!component.isIdempotent()) {
395-
return false;
396-
}
397-
}
398-
return true;
399-
}
400-
}
401-
402-
static class SetTerm implements Term {
403-
404-
private final Collection<? extends Term> components;
405-
406-
public SetTerm(@NonNull Collection<? extends Term> components) {
407-
this.components = components;
408-
}
409-
410-
@Override
411-
public void appendTo(@NonNull StringBuilder builder) {
412-
413-
if (components.isEmpty()) {
414-
builder.append("{}");
415-
return;
416-
}
417-
418-
CqlHelper.append(components, builder, "{", ",", "}");
419-
}
420-
421-
@Override
422-
public boolean isIdempotent() {
423-
for (Term component : components) {
424-
if (!component.isIdempotent()) {
425-
return false;
426-
}
427-
}
428-
return true;
429-
}
430-
}
431-
432360
static class MapTerm implements Term {
433361

434362
private final Map<? extends Term, ? extends Term> components;

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/cql/util/TermFactory.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717

1818
import org.springframework.lang.Nullable;
1919

20+
import com.datastax.oss.driver.api.querybuilder.BindMarker;
2021
import com.datastax.oss.driver.api.querybuilder.term.Term;
2122

2223
/**
2324
* Factory for {@link Term} objects encapsulating a binding {@code value}. Classes implementing this factory interface
24-
* may return inline terms to render values as part of the query string, or bind markers to supply parameters on
25+
* may return inline {@link Term terms} to render values as part of the query string, or {@link BindMarker bind markers} to supply parameters on
2526
* statement creation/parameter binding.
2627
* <p>
2728
* A {@link TermFactory} is typically used with {@link StatementBuilder}.

0 commit comments

Comments
 (0)