Skip to content

Commit 9295e9e

Browse files
committed
Refactor to instanceof pattern variable
Closes #2018
1 parent 3e295a9 commit 9295e9e

24 files changed

+93
-96
lines changed

src/main/java/org/springframework/data/couchbase/core/AbstractTemplateSupport.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,8 @@ public Long getCas(final Object entity) {
180180
long cas = 0;
181181
if (versionProperty != null) {
182182
Object casObject = accessor.getProperty(versionProperty);
183-
if (casObject instanceof Number) {
184-
cas = ((Number) casObject).longValue();
183+
if (casObject instanceof Number number) {
184+
cas = number.longValue();
185185
}
186186
}
187187
return cas;

src/main/java/org/springframework/data/couchbase/core/CouchbaseExceptionTranslator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,11 @@ public final DataAccessException translateExceptionIfPossible(final RuntimeExcep
126126
return new DataRetrievalFailureException(ex.getMessage(), ex);
127127
}
128128

129-
if (ex instanceof TransactionOperationFailedException) {
129+
if (ex instanceof TransactionOperationFailedException transactionOperationFailedException) {
130130
// Replace the TransactionOperationFailedException, since we want the Spring operation to fail with a
131131
// Spring error. Internal state has already been set in the AttemptContext so the retry, rollback etc.
132132
// will get respected regardless of what gets propagated (or not) from the lambda.
133-
return new UncategorizedTransactionDataAccessException((TransactionOperationFailedException) ex);
133+
return new UncategorizedTransactionDataAccessException(transactionOperationFailedException);
134134
}
135135

136136
// Unable to translate exception, therefore just throw the original!

src/main/java/org/springframework/data/couchbase/core/CouchbaseTemplate.java

+6-8
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,8 @@ public CouchbaseTemplate(final CouchbaseClientFactory clientFactory, final Couch
7676
this.scanConsistency = scanConsistency;
7777

7878
this.mappingContext = this.converter.getMappingContext();
79-
if (mappingContext instanceof CouchbaseMappingContext) {
80-
CouchbaseMappingContext cmc = (CouchbaseMappingContext) mappingContext;
81-
if (cmc.isAutoIndexCreation()) {
79+
if (mappingContext instanceof CouchbaseMappingContext cmc) {
80+
if (cmc.isAutoIndexCreation()) {
8281
indexCreator = new CouchbasePersistentEntityIndexCreator(cmc, this);
8382
}
8483
}
@@ -264,11 +263,10 @@ private void prepareIndexCreator(final ApplicationContext context) {
264263
}
265264
}
266265

267-
if (context instanceof ConfigurableApplicationContext && indexCreator != null) {
268-
((ConfigurableApplicationContext) context).addApplicationListener(indexCreator);
269-
if (mappingContext instanceof CouchbaseMappingContext) {
270-
CouchbaseMappingContext cmc = (CouchbaseMappingContext) mappingContext;
271-
cmc.setIndexCreator(indexCreator);
266+
if (context instanceof ConfigurableApplicationContext configurableApplicationContext && indexCreator != null) {
267+
configurableApplicationContext.addApplicationListener(indexCreator);
268+
if (mappingContext instanceof CouchbaseMappingContext cmc) {
269+
cmc.setIndexCreator(indexCreator);
272270
}
273271
}
274272
}

src/main/java/org/springframework/data/couchbase/core/ReactiveExistsByIdOperationSupport.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public Mono<Boolean> one(final String id) {
8686
.getCollection(pArgs.getCollection()).reactive().exists(id, buildOptions(pArgs.getOptions()))
8787
.map(ExistsResult::exists))
8888
.onErrorMap(throwable -> {
89-
if (throwable instanceof RuntimeException) {
90-
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
89+
if (throwable instanceof RuntimeException e) {
90+
return template.potentiallyConvertRuntimeException(e);
9191
} else {
9292
return throwable;
9393
}

src/main/java/org/springframework/data/couchbase/core/ReactiveFindByAnalyticsOperationSupport.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ public Flux<T> all() {
119119
}
120120
return TransactionalSupport.verifyNotInTransaction("findByAnalytics").then(template.getCouchbaseClientFactory()
121121
.getCluster().reactive().analyticsQuery(statement, buildAnalyticsOptions())).onErrorMap(throwable -> {
122-
if (throwable instanceof RuntimeException) {
123-
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
122+
if (throwable instanceof RuntimeException e) {
123+
return template.potentiallyConvertRuntimeException(e);
124124
} else {
125125
return throwable;
126126
}
@@ -153,8 +153,8 @@ public Mono<Long> count() {
153153
}
154154
return TransactionalSupport.verifyNotInTransaction("findByAnalytics").then(template.getCouchbaseClientFactory()
155155
.getCluster().reactive().analyticsQuery(statement, buildAnalyticsOptions())).onErrorMap(throwable -> {
156-
if (throwable instanceof RuntimeException) {
157-
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
156+
if (throwable instanceof RuntimeException e) {
157+
return template.potentiallyConvertRuntimeException(e);
158158
} else {
159159
return throwable;
160160
}

src/main/java/org/springframework/data/couchbase/core/ReactiveFindByIdOperationSupport.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ public Mono<T> one(final Object id) {
132132
}
133133
return Mono.error(throwable);
134134
}).onErrorMap(throwable -> {
135-
if (throwable instanceof RuntimeException) {
136-
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
135+
if (throwable instanceof RuntimeException e) {
136+
return template.potentiallyConvertRuntimeException(e);
137137
} else {
138138
return throwable;
139139
}

src/main/java/org/springframework/data/couchbase/core/ReactiveFindByQueryOperationSupport.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -205,12 +205,12 @@ public Flux<T> all() {
205205
});
206206

207207
return allResult.onErrorMap(throwable -> {
208-
if (throwable instanceof RuntimeException) {
209-
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
208+
if (throwable instanceof RuntimeException e) {
209+
return template.potentiallyConvertRuntimeException(e);
210210
} else {
211211
return throwable;
212212
}
213-
}).flatMapMany(o -> o instanceof ReactiveQueryResult ? ((ReactiveQueryResult) o).rowsAsObject()
213+
}).flatMapMany(o -> o instanceof ReactiveQueryResult reactiveQueryResult ? reactiveQueryResult.rowsAsObject()
214214
: Flux.fromIterable(((TransactionQueryResult) o).rowsAsObject())).flatMap(row -> {
215215
String id = "";
216216
Long cas = Long.valueOf(0);
@@ -273,12 +273,12 @@ public Mono<Long> count() {
273273
});
274274

275275
return allResult.onErrorMap(throwable -> {
276-
if (throwable instanceof RuntimeException) {
277-
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
276+
if (throwable instanceof RuntimeException e) {
277+
return template.potentiallyConvertRuntimeException(e);
278278
} else {
279279
return throwable;
280280
}
281-
}).flatMapMany(o -> o instanceof ReactiveQueryResult ? ((ReactiveQueryResult) o).rowsAsObject()
281+
}).flatMapMany(o -> o instanceof ReactiveQueryResult reactiveQueryResult ? reactiveQueryResult.rowsAsObject()
282282
: Flux.fromIterable(((TransactionQueryResult) o).rowsAsObject()))
283283
.map(row -> row.getLong(row.getNames().iterator().next())).next();
284284
}

src/main/java/org/springframework/data/couchbase/core/ReactiveFindFromReplicasByIdOperationSupport.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ public Mono<T> any(final String id) {
8989
.flatMap(result -> support.decodeEntity(id, result.contentAs(String.class), result.cas(), returnType,
9090
pArgs.getScope(), pArgs.getCollection(), null, null))
9191
.onErrorMap(throwable -> {
92-
if (throwable instanceof RuntimeException) {
93-
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
92+
if (throwable instanceof RuntimeException e) {
93+
return template.potentiallyConvertRuntimeException(e);
9494
} else {
9595
return throwable;
9696
}

src/main/java/org/springframework/data/couchbase/core/ReactiveInsertByIdOperationSupport.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,8 @@ public Mono<T> one(T object) {
124124
null, null));
125125
}
126126
})).onErrorMap(throwable -> {
127-
if (throwable instanceof RuntimeException) {
128-
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
127+
if (throwable instanceof RuntimeException e) {
128+
return template.potentiallyConvertRuntimeException(e);
129129
} else {
130130
return throwable;
131131
}

src/main/java/org/springframework/data/couchbase/core/ReactiveMutateInByIdOperationSupport.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ public Mono<T> one(T object) {
116116
});
117117

118118
return reactiveEntity.onErrorMap(throwable -> {
119-
if (throwable instanceof RuntimeException) {
120-
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
119+
if (throwable instanceof RuntimeException e) {
120+
return template.potentiallyConvertRuntimeException(e);
121121
} else {
122122
return throwable;
123123
}

src/main/java/org/springframework/data/couchbase/core/ReactiveRangeScanOperationSupport.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ Flux<T> rangeScan(String lower, String upper, boolean isSamplingScan, Long limit
167167
pArgs.getScope(), pArgs.getCollection(), null, null)));
168168

169169
return reactiveEntities.onErrorMap(throwable -> {
170-
if (throwable instanceof RuntimeException) {
171-
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
170+
if (throwable instanceof RuntimeException e) {
171+
return template.potentiallyConvertRuntimeException(e);
172172
} else {
173173
return throwable;
174174
}
@@ -213,8 +213,8 @@ Flux<String> rangeScanIds(String lower, String upper, boolean isSamplingScan, Lo
213213
.thenMany(rc.scan(scanType, buildScanOptions(pArgs.getOptions(), true)).map(result -> result.id()));
214214

215215
return reactiveEntities.onErrorMap(throwable -> {
216-
if (throwable instanceof RuntimeException) {
217-
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
216+
if (throwable instanceof RuntimeException e) {
217+
return template.potentiallyConvertRuntimeException(e);
218218
} else {
219219
return throwable;
220220
}

src/main/java/org/springframework/data/couchbase/core/ReactiveRemoveByIdOperationSupport.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ public Mono<RemoveResult> one(final Object id) {
131131

132132
}
133133
}).onErrorMap(throwable -> {
134-
if (throwable instanceof RuntimeException) {
135-
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
134+
if (throwable instanceof RuntimeException e) {
135+
return template.potentiallyConvertRuntimeException(e);
136136
} else {
137137
return throwable;
138138
}

src/main/java/org/springframework/data/couchbase/core/ReactiveReplaceByIdOperationSupport.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ public Mono<T> one(T object) {
141141
result -> support.applyResult(object, converted, converted.getId(), result.cas(), null, null));
142142
}
143143
})).onErrorMap(throwable -> {
144-
if (throwable instanceof RuntimeException) {
145-
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
144+
if (throwable instanceof RuntimeException e) {
145+
return template.potentiallyConvertRuntimeException(e);
146146
} else {
147147
return throwable;
148148
}

src/main/java/org/springframework/data/couchbase/core/ReactiveUpsertByIdOperationSupport.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,8 @@ public Mono<T> one(T object) {
103103
});
104104

105105
return reactiveEntity.onErrorMap(throwable -> {
106-
if (throwable instanceof RuntimeException) {
107-
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
106+
if (throwable instanceof RuntimeException e) {
107+
return template.potentiallyConvertRuntimeException(e);
108108
} else {
109109
return throwable;
110110
}

src/main/java/org/springframework/data/couchbase/core/convert/DateConverters.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ public Date convert(Object source) {
100100
if (source == null) {
101101
return null;
102102
}
103-
if (source instanceof Number) {
103+
if (source instanceof Number number) {
104104
Date date = new Date();
105-
date.setTime(((Number) source).longValue());
105+
date.setTime(number.longValue());
106106
return date;
107-
} else if (source instanceof String) {
108-
return Date.from(Instant.parse((String) source).atZone(systemDefault()).toInstant());
107+
} else if (source instanceof String str) {
108+
return Date.from(Instant.parse(str).atZone(systemDefault()).toInstant());
109109
} else {
110110
// Unsupported serialized object
111111
return null;

src/main/java/org/springframework/data/couchbase/core/convert/MappingCouchbaseConverter.java

+20-20
Original file line numberDiff line numberDiff line change
@@ -381,10 +381,10 @@ protected Map<Object, Object> readMap(final TypeInformation<?> type, final Couch
381381
}
382382

383383
TypeInformation<?> valueType = type.getMapValueType();
384-
if (value instanceof CouchbaseDocument) {
385-
map.put(key, read(valueType, (CouchbaseDocument) value, parent));
386-
} else if (value instanceof CouchbaseList) {
387-
map.put(key, readCollection(valueType, (CouchbaseList) value, parent));
384+
if (value instanceof CouchbaseDocument couchbaseDocument) {
385+
map.put(key, read(valueType, couchbaseDocument, parent));
386+
} else if (value instanceof CouchbaseList couchbaseList) {
387+
map.put(key, readCollection(valueType, couchbaseList, parent));
388388
} else {
389389
Class<?> valueClass = valueType == null ? null : valueType.getType();
390390
map.put(key, getPotentiallyConvertedSimpleRead(value, valueClass));
@@ -511,8 +511,8 @@ protected void copyCouchbaseDocument(final CouchbaseDocument source, final Couch
511511
}
512512

513513
private String convertToString(Object propertyObj) {
514-
if (propertyObj instanceof String) {
515-
return (String) propertyObj;
514+
if (propertyObj instanceof String str) {
515+
return str;
516516
} else if (propertyObj instanceof Number) {
517517
return new StringBuffer().append(propertyObj).toString();
518518
} else {
@@ -821,10 +821,10 @@ private Object readCollection(final TypeInformation<?> targetType, final Couchba
821821

822822
Object dbObjItem = source.get(i);
823823

824-
if (dbObjItem instanceof CouchbaseDocument) {
825-
items.add(read(componentType, (CouchbaseDocument) dbObjItem, parent));
826-
} else if (dbObjItem instanceof CouchbaseList) {
827-
items.add(readCollection(componentType != null ? componentType :TypeInformation.of(dbObjItem.getClass()), (CouchbaseList) dbObjItem, parent));
824+
if (dbObjItem instanceof CouchbaseDocument couchbaseDocument) {
825+
items.add(read(componentType, couchbaseDocument, parent));
826+
} else if (dbObjItem instanceof CouchbaseList couchbaseList) {
827+
items.add(readCollection(componentType != null ? componentType :TypeInformation.of(dbObjItem.getClass()), couchbaseList, parent));
828828
} else {
829829
items.add(getPotentiallyConvertedSimpleRead(dbObjItem, rawComponentType));
830830
}
@@ -915,8 +915,8 @@ public void setApplicationContext(ApplicationContext applicationContext) {
915915
setEntityCallbacks(EntityCallbacks.create(applicationContext));
916916
}
917917
ClassLoader classLoader = applicationContext.getClassLoader();
918-
if (this.typeMapper instanceof BeanClassLoaderAware && classLoader != null) {
919-
((BeanClassLoaderAware) this.typeMapper).setBeanClassLoader(classLoader);
918+
if (this.typeMapper instanceof BeanClassLoaderAware beanClassLoaderAware && classLoader != null) {
919+
beanClassLoaderAware.setBeanClassLoader(classLoader);
920920
}
921921
}
922922

@@ -949,10 +949,10 @@ private <R> R readValue(Object value, TypeInformation type, Object parent) {
949949

950950
if (conversions.hasCustomReadTarget(value.getClass(), rawType)) {
951951
return (R) conversionService.convert(value, rawType);
952-
} else if (value instanceof CouchbaseDocument) {
953-
return (R) read(type, (CouchbaseDocument) value, parent);
954-
} else if (value instanceof CouchbaseList) {
955-
return (R) readCollection(type, (CouchbaseList) value, parent);
952+
} else if (value instanceof CouchbaseDocument couchbaseDocument) {
953+
return (R) read(type, couchbaseDocument, parent);
954+
} else if (value instanceof CouchbaseList couchbaseList) {
955+
return (R) readCollection(type, couchbaseList, parent);
956956
} else {
957957
return (R) getPotentiallyConvertedSimpleRead(value, type.getType()); // type does not have annotations
958958
}
@@ -982,11 +982,11 @@ public <R> R readValue(Object value, CouchbasePersistentProperty prop, Object pa
982982
TypeInformation ti = TypeInformation.of(value.getClass());
983983
return (R) conversionService.convert(value, ti.toTypeDescriptor(), new TypeDescriptor(prop.getField()));
984984
}
985-
if (value instanceof CouchbaseDocument) {
986-
return (R) read(prop.getTypeInformation(), (CouchbaseDocument) value, parent);
985+
if (value instanceof CouchbaseDocument couchbaseDocument) {
986+
return (R) read(prop.getTypeInformation(), couchbaseDocument, parent);
987987
}
988-
if (value instanceof CouchbaseList) {
989-
return (R) readCollection(prop.getTypeInformation(), (CouchbaseList) value, parent);
988+
if (value instanceof CouchbaseList couchbaseList) {
989+
return (R) readCollection(prop.getTypeInformation(), couchbaseList, parent);
990990
}
991991
return (R) getPotentiallyConvertedSimpleRead(value, prop);// passes PersistentProperty with annotations
992992

src/main/java/org/springframework/data/couchbase/core/convert/translation/JacksonTranslationService.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ private void encodeRecursive(final CouchbaseStorable source, final JsonGenerator
9696
String key = entry.getKey();
9797
Object value = entry.getValue();
9898
generator.writeFieldName(key);
99-
if (value instanceof CouchbaseDocument) {
100-
encodeRecursive((CouchbaseDocument) value, generator);
99+
if (value instanceof CouchbaseDocument couchbaseDocument) {
100+
encodeRecursive(couchbaseDocument, generator);
101101
continue;
102102
}
103103

src/main/java/org/springframework/data/couchbase/core/index/CouchbasePersistentEntityIndexCreator.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ private void checkForAndCreateIndexes(final CouchbasePersistentEntity<?> entity)
8686
if (entity.isAnnotationPresent(Document.class)) {
8787

8888
for (IndexDefinition indexDefinition : indexResolver.resolveIndexFor(entity.getTypeInformation())) {
89-
IndexDefinitionHolder indexToCreate = indexDefinition instanceof IndexDefinitionHolder
90-
? (IndexDefinitionHolder) indexDefinition
89+
IndexDefinitionHolder indexToCreate = indexDefinition instanceof IndexDefinitionHolder indexDefinitionHolder
90+
? indexDefinitionHolder
9191
: new IndexDefinitionHolder(indexDefinition.getIndexFields(), indexDefinition.getIndexName(),
9292
indexDefinition.getIndexPredicate());
9393

src/main/java/org/springframework/data/couchbase/core/mapping/CouchbaseList.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ public final int size(final boolean recursive) {
131131

132132
int totalSize = thisSize;
133133
for (Object value : payload) {
134-
if (value instanceof CouchbaseDocument) {
135-
totalSize += ((CouchbaseDocument) value).size(true);
136-
} else if (value instanceof CouchbaseList) {
137-
totalSize += ((CouchbaseList) value).size(true);
134+
if (value instanceof CouchbaseDocument couchbaseDocument) {
135+
totalSize += couchbaseDocument.size(true);
136+
} else if (value instanceof CouchbaseList couchbaseList) {
137+
totalSize += couchbaseList.size(true);
138138
}
139139
}
140140

src/main/java/org/springframework/data/couchbase/core/query/Meta.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,23 @@ public void setValue(String key, @Nullable Object value) {
8383

8484
Assert.hasText(key, "Meta key must not be 'null' or blank.");
8585

86-
if (value == null || (value instanceof String && !StringUtils.hasText((String) value))) {
86+
if (value == null || (value instanceof String str && !StringUtils.hasText(str))) {
8787
this.values.remove(MetaKey.valueOf(key));
8888
}
8989
this.values.put(MetaKey.valueOf(key), value);
9090
}
9191

9292
public void setValue(MetaKey key, @Nullable Object value) {
9393

94-
if (value == null || (value instanceof String && !StringUtils.hasText((String) value))) {
94+
if (value == null || (value instanceof String str && !StringUtils.hasText(str))) {
9595
this.values.remove(key);
9696
}
9797
this.values.put(key, value);
9898
}
9999

100100
public void set(MetaKey key, @Nullable Object value) {
101101

102-
if (value == null || (value instanceof String && !StringUtils.hasText((String) value))) {
102+
if (value == null || (value instanceof String str && !StringUtils.hasText(str))) {
103103
this.values.remove(key);
104104
}
105105
this.values.put(key, value);

src/main/java/org/springframework/data/couchbase/core/query/N1QLExpression.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ public static N1QLExpression path(Object... pathComponents) {
162162
StringBuilder path = new StringBuilder();
163163
for (Object p : pathComponents) {
164164
path.append('.');
165-
if (p instanceof N1QLExpression) {
166-
path.append(((N1QLExpression) p).toString());
165+
if (p instanceof N1QLExpression n1QLExpression) {
166+
path.append(n1QLExpression.toString());
167167
} else {
168168
path.append(String.valueOf(p));
169169
}

0 commit comments

Comments
 (0)