Skip to content

Commit 7a722c6

Browse files
authored
set of small code-style improvements (spring-attic#2156)
* code-style improvements
1 parent 2c78357 commit 7a722c6

File tree

8 files changed

+69
-70
lines changed

8 files changed

+69
-70
lines changed

spring-cloud-gcp-data-spanner/src/main/java/org/springframework/cloud/gcp/data/spanner/core/SpannerOperations.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public interface SpannerOperations {
6666
*
6767
* @param entityClass the type of the entity
6868
* @param key the key of the object
69-
* @param <T> the type of the object to retrieve.
69+
* @param <T> the type of the object to check.
7070
* @return {@literal true} if an entity with the given key exists, {@literal false} otherwise.
7171
* @throws IllegalArgumentException if {@literal id} is {@literal null}.
7272
*/
@@ -160,8 +160,9 @@ <T> List<T> query(Class<T> entityClass, Statement statement,
160160
* Deletes an object based on a key.
161161
* @param entityClass the type of the object to delete.
162162
* @param key the key of the object to delete from storage.
163+
* @param <T> the type of the object to delete.
163164
*/
164-
void delete(Class entityClass, Key key);
165+
<T> void delete(Class<T> entityClass, Key key);
165166

166167
/**
167168
* Deletes an object from storage.
@@ -173,14 +174,15 @@ <T> List<T> query(Class<T> entityClass, Statement statement,
173174
* Deletes objects from storage in a batch.
174175
* @param objects the objects to delete from storage.
175176
*/
176-
void deleteAll(Iterable objects);
177+
void deleteAll(Iterable<?> objects);
177178

178179
/**
179180
* Deletes objects given a set of keys.
180181
* @param entityClass the type of object to delete.
181182
* @param keys the keys of the objects to delete.
183+
* @param <T> the type of the object to delete.
182184
*/
183-
void delete(Class entityClass, KeySet keys);
185+
<T> void delete(Class<T> entityClass, KeySet keys);
184186

185187
/**
186188
* Insert an object into storage.
@@ -192,7 +194,7 @@ <T> List<T> query(Class<T> entityClass, Statement statement,
192194
* Insert objects into storage in batch.
193195
* @param objects the objects to insert.
194196
*/
195-
void insertAll(Iterable objects);
197+
void insertAll(Iterable<?> objects);
196198

197199
/**
198200
* Update an object already in storage.
@@ -204,7 +206,7 @@ <T> List<T> query(Class<T> entityClass, Statement statement,
204206
* Update objects in batch.
205207
* @param objects the objects to update.
206208
*/
207-
void updateAll(Iterable objects);
209+
void updateAll(Iterable<?> objects);
208210

209211
/**
210212
* Update an object in storage.
@@ -233,7 +235,7 @@ <T> List<T> query(Class<T> entityClass, Statement statement,
233235
* Update or insert objects into storage in batch.
234236
* @param objects the objects to update or insert.
235237
*/
236-
void upsertAll(Iterable objects);
238+
void upsertAll(Iterable<?> objects);
237239

238240
/**
239241
* Update or insert an object into storage.
@@ -255,9 +257,10 @@ <T> List<T> query(Class<T> entityClass, Statement statement,
255257
/**
256258
* Count how many objects are stored of the given type.
257259
* @param entityClass the type of object to count.
260+
* @param <T> the type of the object to count.
258261
* @return the number of stored objects.
259262
*/
260-
long count(Class entityClass);
263+
<T> long count(Class<T> entityClass);
261264

262265
/**
263266
* Performs multiple read and write operations in a single transaction.

spring-cloud-gcp-data-spanner/src/main/java/org/springframework/cloud/gcp/data/spanner/core/SpannerTemplate.java

+23-25
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerDataException;
5555
import org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerMappingContext;
5656
import org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentEntity;
57-
import org.springframework.cloud.gcp.data.spanner.core.mapping.SpannerPersistentProperty;
5857
import org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterDeleteEvent;
5958
import org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterExecuteDmlEvent;
6059
import org.springframework.cloud.gcp.data.spanner.core.mapping.event.AfterQueryEvent;
@@ -68,7 +67,6 @@
6867
import org.springframework.context.ApplicationEventPublisher;
6968
import org.springframework.context.ApplicationEventPublisherAware;
7069
import org.springframework.data.mapping.PersistentPropertyAccessor;
71-
import org.springframework.data.mapping.PropertyHandler;
7270
import org.springframework.transaction.support.TransactionSynchronizationManager;
7371
import org.springframework.util.Assert;
7472

@@ -274,7 +272,7 @@ public void insert(Object object) {
274272

275273

276274
@Override
277-
public void insertAll(Iterable objects) {
275+
public void insertAll(Iterable<?> objects) {
278276
applySaveMutations(() -> getMutationsForMultipleObjects(objects, this.mutationFactory::insert), objects, null);
279277
}
280278

@@ -284,7 +282,7 @@ public void update(Object object) {
284282
}
285283

286284
@Override
287-
public void updateAll(Iterable objects) {
285+
public void updateAll(Iterable<?> objects) {
288286
applySaveMutations(() -> getMutationsForMultipleObjects(objects,
289287
(x) -> this.mutationFactory.update(x, null)), objects, null);
290288
}
@@ -309,7 +307,7 @@ public void upsert(Object object) {
309307
}
310308

311309
@Override
312-
public void upsertAll(Iterable objects) {
310+
public void upsertAll(Iterable<?> objects) {
313311
applySaveMutations(() -> getMutationsForMultipleObjects(objects,
314312
(x) -> this.mutationFactory.upsert(x, null)), objects, null);
315313
}
@@ -327,7 +325,7 @@ public void upsert(Object object, Set<String> includeProperties) {
327325
includeProperties);
328326
}
329327

330-
private void applySaveMutations(Supplier<List<Mutation>> mutationsSupplier, Iterable entities,
328+
private void applySaveMutations(Supplier<List<Mutation>> mutationsSupplier, Iterable<?> entities,
331329
Set<String> includeProperties) {
332330
maybeEmitEvent(new BeforeSaveEvent(entities, includeProperties));
333331
List<Mutation> mutations = mutationsSupplier.get();
@@ -342,12 +340,12 @@ public void delete(Object entity) {
342340
}
343341

344342
@Override
345-
public void deleteAll(Iterable objects) {
346-
applyDeleteMutations(objects, (List<Mutation>) StreamSupport.stream(objects.spliterator(), false)
343+
public void deleteAll(Iterable<?> objects) {
344+
applyDeleteMutations(objects, StreamSupport.stream(objects.spliterator(), false)
347345
.map(this.mutationFactory::delete).collect(Collectors.toList()));
348346
}
349347

350-
private void applyDeleteMutations(Iterable objects, List<Mutation> mutations) {
348+
private void applyDeleteMutations(Iterable<?> objects, List<Mutation> mutations) {
351349
maybeEmitEvent(new BeforeDeleteEvent(mutations, objects, null, null));
352350
applyMutations(mutations);
353351
maybeEmitEvent(new AfterDeleteEvent(mutations, objects, null, null));
@@ -452,10 +450,10 @@ private String getQueryLogMessageWithOptions(Statement statement, SpannerQueryOp
452450
String message;
453451
StringBuilder logSb = new StringBuilder("Executing query");
454452
if (options.getTimestampBound() != null) {
455-
logSb.append(" at timestamp" + options.getTimestampBound());
453+
logSb.append(" at timestamp ").append(options.getTimestampBound());
456454
}
457-
for (QueryOption queryOption : (QueryOption[]) options.getOptions()) {
458-
logSb.append(" with option: " + queryOption);
455+
for (QueryOption queryOption : options.getOptions()) {
456+
logSb.append(" with option: ").append(queryOption);
459457
}
460458
logSb.append(" : ").append(statement);
461459
message = logSb.toString();
@@ -470,7 +468,7 @@ private ResultSet performQuery(Statement statement, SpannerQueryOptions options)
470468
else {
471469
resultSet = ((options.getTimestampBound() != null)
472470
? getReadContext(options.getTimestampBound())
473-
: getReadContext()).executeQuery(statement, (QueryOption[]) options.getOptions());
471+
: getReadContext()).executeQuery(statement, options.getOptions());
474472
}
475473
return resultSet;
476474
}
@@ -521,22 +519,22 @@ private void logReadOptions(SpannerReadOptions options, StringBuilder logs) {
521519
return;
522520
}
523521
if (options.getTimestampBound() != null) {
524-
logs.append(" at timestamp " + options.getTimestampBound());
522+
logs.append(" at timestamp ").append(options.getTimestampBound());
525523
}
526524
for (ReadOption readOption : options.getOptions()) {
527-
logs.append(" with option: " + readOption);
525+
logs.append(" with option: ").append(readOption);
528526
}
529527
if (options.getIndex() != null) {
530-
logs.append(" secondary index: " + options.getIndex());
528+
logs.append(" secondary index: ").append(options.getIndex());
531529
}
532530
}
533531

534532
private StringBuilder logColumns(String tableName, KeySet keys,
535533
Iterable<String> columns) {
536-
StringBuilder logSb = new StringBuilder("Executing read on table " + tableName
537-
+ " with keys: " + keys + " and columns: ");
538-
StringJoiner sj = new StringJoiner(",");
539-
columns.forEach((col) -> sj.add(col));
534+
StringBuilder logSb = new StringBuilder();
535+
logSb.append("Executing read on table ").append(tableName).append(" with keys: ").append(keys).append(" and columns: ");
536+
StringJoiner sj = new StringJoiner(", ");
537+
columns.forEach(sj::add);
540538
logSb.append(sj.toString());
541539
return logSb;
542540
}
@@ -575,12 +573,12 @@ private <T> List<T> resolveChildEntities(List<T> entities,
575573
}
576574

577575
private void resolveChildEntity(Object entity, Set<String> includeProperties) {
578-
SpannerPersistentEntity spannerPersistentEntity = this.mappingContext
576+
SpannerPersistentEntity<?> spannerPersistentEntity = this.mappingContext
579577
.getPersistentEntity(entity.getClass());
580-
PersistentPropertyAccessor accessor = spannerPersistentEntity
578+
PersistentPropertyAccessor<?> accessor = spannerPersistentEntity
581579
.getPropertyAccessor(entity);
582580
spannerPersistentEntity.doWithInterleavedProperties(
583-
(PropertyHandler<SpannerPersistentProperty>) (spannerPersistentProperty) -> {
581+
(spannerPersistentProperty) -> {
584582
if (includeProperties != null && !includeProperties
585583
.contains(spannerPersistentEntity.getName())) {
586584
return;
@@ -609,9 +607,9 @@ private void resolveChildEntity(Object entity, Set<String> includeProperties) {
609607
});
610608
}
611609

612-
private List<Mutation> getMutationsForMultipleObjects(Iterable it,
610+
private List<Mutation> getMutationsForMultipleObjects(Iterable<?> it,
613611
Function<Object, Collection<Mutation>> individualEntityMutationFunc) {
614-
return (List<Mutation>) StreamSupport.stream(it.spliterator(), false)
612+
return StreamSupport.stream(it.spliterator(), false)
615613
.flatMap((x) -> individualEntityMutationFunc.apply(x).stream())
616614
.collect(Collectors.toList());
617615
}

spring-cloud-gcp-data-spanner/src/main/java/org/springframework/cloud/gcp/data/spanner/core/admin/SpannerDatabaseAdminTemplate.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,16 @@ public boolean databaseExists() {
149149
*/
150150
public Map<String, String> getChildParentTablesMap() {
151151
Map<String, String> relationships = new HashMap<>();
152-
ResultSet results = this.databaseClientProvider.get().singleUse()
153-
.executeQuery(TABLE_AND_PARENT_QUERY);
154-
while (results.next()) {
155-
Struct row = results.getCurrentRowAsStruct();
156-
relationships.put(row.getString(TABLE_NAME_COL_NAME),
157-
row.isNull(PARENT_TABLE_NAME_COL_NAME) ? null
158-
: row.getString(PARENT_TABLE_NAME_COL_NAME));
152+
try (ResultSet results = this.databaseClientProvider.get().singleUse()
153+
.executeQuery(TABLE_AND_PARENT_QUERY)) {
154+
while (results.next()) {
155+
Struct row = results.getCurrentRowAsStruct();
156+
relationships.put(row.getString(TABLE_NAME_COL_NAME),
157+
row.isNull(PARENT_TABLE_NAME_COL_NAME) ? null
158+
: row.getString(PARENT_TABLE_NAME_COL_NAME));
159+
}
160+
return relationships;
159161
}
160-
results.close();
161-
return relationships;
162162
}
163163

164164
/**

spring-cloud-gcp-data-spanner/src/main/java/org/springframework/cloud/gcp/data/spanner/core/convert/ConversionUtils.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ static <T> Iterable<T> convertIterable(
6161
return result;
6262
}
6363

64+
@SuppressWarnings("unchecked")
6465
public static <T> T wrapSimpleLazyProxy(Supplier<T> supplierFunc, Class<T> type) {
6566
return (T) Proxy.newProxyInstance(type.getClassLoader(), new Class[] { type },
6667
new SimpleLazyDynamicInvocationHandler<>(supplierFunc));
@@ -69,7 +70,7 @@ public static <T> T wrapSimpleLazyProxy(Supplier<T> supplierFunc, Class<T> type)
6970
public static boolean ignoreForWriteLazyProxy(Object object) {
7071
if (Proxy.isProxyClass(object.getClass())
7172
&& (Proxy.getInvocationHandler(object) instanceof SimpleLazyDynamicInvocationHandler)) {
72-
SimpleLazyDynamicInvocationHandler handler = (SimpleLazyDynamicInvocationHandler) Proxy
73+
SimpleLazyDynamicInvocationHandler<?> handler = (SimpleLazyDynamicInvocationHandler<?>) Proxy
7374
.getInvocationHandler(object);
7475
return !handler.isEvaluated();
7576
}

spring-cloud-gcp-data-spanner/src/main/java/org/springframework/cloud/gcp/data/spanner/core/convert/SpannerEntityProcessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public interface SpannerEntityProcessor extends SpannerEntityReader, SpannerEnti
4444
<T> List<T> mapToList(ResultSet resultSet, Class<T> entityClass);
4545

4646
/**
47-
* Converts a set of Spanner {@link ResultSet} into a list of objects.
47+
* Converts a Spanner {@link ResultSet} into a list of objects.
4848
* @param resultSet the Spanner results to convert. The ResultSet will be exhausted
4949
* and closed.
5050
* @param entityClass the type of the objects the Spanner results represent.

spring-cloud-gcp-data-spanner/src/main/java/org/springframework/cloud/gcp/data/spanner/core/mapping/event/ExecuteDmlEvent.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class ExecuteDmlEvent extends ApplicationEvent {
2929

3030
/**
3131
* Constructor.
32-
* @param statement @param statement the DML statement which is (never {@code null})
32+
* @param statement the DML statement which is (never {@code null})
3333
*/
3434
public ExecuteDmlEvent(Statement statement) {
3535
super(statement);

spring-cloud-gcp-data-spanner/src/main/java/org/springframework/cloud/gcp/data/spanner/repository/query/SpannerStatementQueryExecutor.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public static <T> String applySortingPagingQueryOptions(Class<T> entityClass,
163163
}
164164

165165
/**
166-
* Gets a query that returns the rows associated with a parent entity. This function is
166+
* Gets a {@link Statement} that returns the rows associated with a parent entity. This function is
167167
* intended to be used with parent-child interleaved tables, so that the retrieval of all
168168
* child rows having the parent's key values is efficient.
169169
* @param parentKey the parent key whose children to get.
@@ -252,7 +252,6 @@ private static <C, P> String getChildrenStructsQuery(
252252
* @throws IllegalArgumentException if the number of tags does not match the number of
253253
* params, or if a param of an unsupported type is given.
254254
*/
255-
@SuppressWarnings("unchecked")
256255
public static Statement buildStatementFromSqlWithArgs(String sql, List<String> tags,
257256
Function<Object, Struct> paramStructConvertFunc, SpannerCustomConverter spannerCustomConverter,
258257
Object[] params, Map<String, Parameter> queryMethodParams) {
@@ -271,6 +270,7 @@ public static Statement buildStatementFromSqlWithArgs(String sql, List<String> t
271270
return builder.build();
272271
}
273272

273+
@SuppressWarnings("unchecked")
274274
private static void bindParameter(ValueBinder<Statement.Builder> bind,
275275
Function<Object, Struct> paramStructConvertFunc, SpannerCustomConverter spannerCustomConverter,
276276
Object originalParam, Parameter paramMetadata) {
@@ -301,14 +301,15 @@ private static void bindParameter(ValueBinder<Statement.Builder> bind,
301301
}
302302
}
303303

304-
public static String getColumnsStringForSelect(SpannerPersistentEntity spannerPersistentEntity, SpannerMappingContext mappingContext) {
304+
public static String getColumnsStringForSelect(
305+
SpannerPersistentEntity<?> spannerPersistentEntity, SpannerMappingContext mappingContext) {
305306
StringJoiner joiner = new StringJoiner(", ");
306-
spannerPersistentEntity.doWithInterleavedProperties(persistentProperty -> {
307-
SpannerPersistentProperty spannerPersistentProperty = (SpannerPersistentProperty) persistentProperty;
307+
spannerPersistentEntity.doWithInterleavedProperties(spannerPersistentProperty -> {
308308
if (spannerPersistentProperty.isEagerInterleaved()) {
309309
Class childType = spannerPersistentProperty.getColumnInnerType();
310310
SpannerPersistentEntity childPersistentEntity = mappingContext.getPersistentEntity(childType);
311-
joiner.add(getChildrenStructsQuery(childPersistentEntity, spannerPersistentEntity, mappingContext, spannerPersistentProperty.getColumnName()));
311+
joiner.add(getChildrenStructsQuery(
312+
childPersistentEntity, spannerPersistentEntity, mappingContext, spannerPersistentProperty.getColumnName()));
312313
}
313314
});
314315
String childrenSubquery = joiner.toString();
@@ -344,17 +345,16 @@ else if (tree.isExistsProjection()) {
344345
return Pair.of(finalSql, tags);
345346
}
346347

347-
private static StringBuilder buildSelect(
348-
SpannerPersistentEntity spannerPersistentEntity, PartTree tree,
348+
private static void buildSelect(
349+
SpannerPersistentEntity<?> spannerPersistentEntity, PartTree tree,
349350
StringBuilder stringBuilder, SpannerMappingContext mappingContext) {
350-
stringBuilder.append("SELECT " + (tree.isDistinct() ? "DISTINCT " : "")
351-
+ getColumnsStringForSelect(spannerPersistentEntity, mappingContext) + " ");
352-
return stringBuilder;
351+
stringBuilder.append("SELECT ").append(tree.isDistinct() ? "DISTINCT " : "")
352+
.append(getColumnsStringForSelect(spannerPersistentEntity, mappingContext)).append(" ");
353353
}
354354

355355
private static void buildFrom(SpannerPersistentEntity<?> persistentEntity,
356356
StringBuilder stringBuilder) {
357-
stringBuilder.append("FROM " + persistentEntity.tableName() + " ");
357+
stringBuilder.append("FROM ").append(persistentEntity.tableName()).append(" ");
358358
}
359359

360360
public static StringBuilder applySort(Sort sort, StringBuilder sql,
@@ -484,7 +484,7 @@ private static void buildLimit(PartTree tree, StringBuilder stringBuilder) {
484484
stringBuilder.append(" LIMIT 1");
485485
}
486486
else if (tree.isLimiting()) {
487-
stringBuilder.append(" LIMIT " + tree.getMaxResults());
487+
stringBuilder.append(" LIMIT ").append(tree.getMaxResults());
488488
}
489489
}
490490
}

0 commit comments

Comments
 (0)