Skip to content

Commit

Permalink
Refactor to instanceof pattern variable
Browse files Browse the repository at this point in the history
  • Loading branch information
arefbehboudi committed Jan 9, 2025
1 parent 3e295a9 commit ca7c3a7
Show file tree
Hide file tree
Showing 24 changed files with 93 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ public Long getCas(final Object entity) {
long cas = 0;
if (versionProperty != null) {
Object casObject = accessor.getProperty(versionProperty);
if (casObject instanceof Number) {
cas = ((Number) casObject).longValue();
if (casObject instanceof Number number) {
cas = number.longValue();
}
}
return cas;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ public final DataAccessException translateExceptionIfPossible(final RuntimeExcep
return new DataRetrievalFailureException(ex.getMessage(), ex);
}

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

// Unable to translate exception, therefore just throw the original!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ public CouchbaseTemplate(final CouchbaseClientFactory clientFactory, final Couch
this.scanConsistency = scanConsistency;

this.mappingContext = this.converter.getMappingContext();
if (mappingContext instanceof CouchbaseMappingContext) {
CouchbaseMappingContext cmc = (CouchbaseMappingContext) mappingContext;
if (cmc.isAutoIndexCreation()) {
if (mappingContext instanceof CouchbaseMappingContext cmc) {
if (cmc.isAutoIndexCreation()) {
indexCreator = new CouchbasePersistentEntityIndexCreator(cmc, this);
}
}
Expand Down Expand Up @@ -264,11 +263,10 @@ private void prepareIndexCreator(final ApplicationContext context) {
}
}

if (context instanceof ConfigurableApplicationContext && indexCreator != null) {
((ConfigurableApplicationContext) context).addApplicationListener(indexCreator);
if (mappingContext instanceof CouchbaseMappingContext) {
CouchbaseMappingContext cmc = (CouchbaseMappingContext) mappingContext;
cmc.setIndexCreator(indexCreator);
if (context instanceof ConfigurableApplicationContext configurableApplicationContext && indexCreator != null) {
configurableApplicationContext.addApplicationListener(indexCreator);
if (mappingContext instanceof CouchbaseMappingContext cmc) {
cmc.setIndexCreator(indexCreator);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public Mono<Boolean> one(final String id) {
.getCollection(pArgs.getCollection()).reactive().exists(id, buildOptions(pArgs.getOptions()))
.map(ExistsResult::exists))
.onErrorMap(throwable -> {
if (throwable instanceof RuntimeException) {
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
if (throwable instanceof RuntimeException e) {
return template.potentiallyConvertRuntimeException(e);
} else {
return throwable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ public Flux<T> all() {
}
return TransactionalSupport.verifyNotInTransaction("findByAnalytics").then(template.getCouchbaseClientFactory()
.getCluster().reactive().analyticsQuery(statement, buildAnalyticsOptions())).onErrorMap(throwable -> {
if (throwable instanceof RuntimeException) {
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
if (throwable instanceof RuntimeException e) {
return template.potentiallyConvertRuntimeException(e);
} else {
return throwable;
}
Expand Down Expand Up @@ -153,8 +153,8 @@ public Mono<Long> count() {
}
return TransactionalSupport.verifyNotInTransaction("findByAnalytics").then(template.getCouchbaseClientFactory()
.getCluster().reactive().analyticsQuery(statement, buildAnalyticsOptions())).onErrorMap(throwable -> {
if (throwable instanceof RuntimeException) {
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
if (throwable instanceof RuntimeException e) {
return template.potentiallyConvertRuntimeException(e);
} else {
return throwable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ public Mono<T> one(final Object id) {
}
return Mono.error(throwable);
}).onErrorMap(throwable -> {
if (throwable instanceof RuntimeException) {
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
if (throwable instanceof RuntimeException e) {
return template.potentiallyConvertRuntimeException(e);
} else {
return throwable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,12 @@ public Flux<T> all() {
});

return allResult.onErrorMap(throwable -> {
if (throwable instanceof RuntimeException) {
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
if (throwable instanceof RuntimeException e) {
return template.potentiallyConvertRuntimeException(e);
} else {
return throwable;
}
}).flatMapMany(o -> o instanceof ReactiveQueryResult ? ((ReactiveQueryResult) o).rowsAsObject()
}).flatMapMany(o -> o instanceof ReactiveQueryResult reactiveQueryResult ? reactiveQueryResult.rowsAsObject()
: Flux.fromIterable(((TransactionQueryResult) o).rowsAsObject())).flatMap(row -> {
String id = "";
Long cas = Long.valueOf(0);
Expand Down Expand Up @@ -273,12 +273,12 @@ public Mono<Long> count() {
});

return allResult.onErrorMap(throwable -> {
if (throwable instanceof RuntimeException) {
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
if (throwable instanceof RuntimeException e) {
return template.potentiallyConvertRuntimeException(e);
} else {
return throwable;
}
}).flatMapMany(o -> o instanceof ReactiveQueryResult ? ((ReactiveQueryResult) o).rowsAsObject()
}).flatMapMany(o -> o instanceof ReactiveQueryResult reactiveQueryResult ? reactiveQueryResult.rowsAsObject()
: Flux.fromIterable(((TransactionQueryResult) o).rowsAsObject()))
.map(row -> row.getLong(row.getNames().iterator().next())).next();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ public Mono<T> any(final String id) {
.flatMap(result -> support.decodeEntity(id, result.contentAs(String.class), result.cas(), returnType,
pArgs.getScope(), pArgs.getCollection(), null, null))
.onErrorMap(throwable -> {
if (throwable instanceof RuntimeException) {
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
if (throwable instanceof RuntimeException e) {
return template.potentiallyConvertRuntimeException(e);
} else {
return throwable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ public Mono<T> one(T object) {
null, null));
}
})).onErrorMap(throwable -> {
if (throwable instanceof RuntimeException) {
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
if (throwable instanceof RuntimeException e) {
return template.potentiallyConvertRuntimeException(e);
} else {
return throwable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ public Mono<T> one(T object) {
});

return reactiveEntity.onErrorMap(throwable -> {
if (throwable instanceof RuntimeException) {
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
if (throwable instanceof RuntimeException e) {
return template.potentiallyConvertRuntimeException(e);
} else {
return throwable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ Flux<T> rangeScan(String lower, String upper, boolean isSamplingScan, Long limit
pArgs.getScope(), pArgs.getCollection(), null, null)));

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

return reactiveEntities.onErrorMap(throwable -> {
if (throwable instanceof RuntimeException) {
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
if (throwable instanceof RuntimeException e) {
return template.potentiallyConvertRuntimeException(e);
} else {
return throwable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ public Mono<RemoveResult> one(final Object id) {

}
}).onErrorMap(throwable -> {
if (throwable instanceof RuntimeException) {
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
if (throwable instanceof RuntimeException e) {
return template.potentiallyConvertRuntimeException(e);
} else {
return throwable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ public Mono<T> one(T object) {
result -> support.applyResult(object, converted, converted.getId(), result.cas(), null, null));
}
})).onErrorMap(throwable -> {
if (throwable instanceof RuntimeException) {
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
if (throwable instanceof RuntimeException e) {
return template.potentiallyConvertRuntimeException(e);
} else {
return throwable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ public Mono<T> one(T object) {
});

return reactiveEntity.onErrorMap(throwable -> {
if (throwable instanceof RuntimeException) {
return template.potentiallyConvertRuntimeException((RuntimeException) throwable);
if (throwable instanceof RuntimeException e) {
return template.potentiallyConvertRuntimeException(e);
} else {
return throwable;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ public Date convert(Object source) {
if (source == null) {
return null;
}
if (source instanceof Number) {
if (source instanceof Number number) {
Date date = new Date();
date.setTime(((Number) source).longValue());
date.setTime(number.longValue());
return date;
} else if (source instanceof String) {
return Date.from(Instant.parse((String) source).atZone(systemDefault()).toInstant());
} else if (source instanceof String str) {
return Date.from(Instant.parse(str).atZone(systemDefault()).toInstant());
} else {
// Unsupported serialized object
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,10 +381,10 @@ protected Map<Object, Object> readMap(final TypeInformation<?> type, final Couch
}

TypeInformation<?> valueType = type.getMapValueType();
if (value instanceof CouchbaseDocument) {
map.put(key, read(valueType, (CouchbaseDocument) value, parent));
} else if (value instanceof CouchbaseList) {
map.put(key, readCollection(valueType, (CouchbaseList) value, parent));
if (value instanceof CouchbaseDocument couchbaseDocument) {
map.put(key, read(valueType, couchbaseDocument, parent));
} else if (value instanceof CouchbaseList couchbaseList) {
map.put(key, readCollection(valueType, couchbaseList, parent));
} else {
Class<?> valueClass = valueType == null ? null : valueType.getType();
map.put(key, getPotentiallyConvertedSimpleRead(value, valueClass));
Expand Down Expand Up @@ -511,8 +511,8 @@ protected void copyCouchbaseDocument(final CouchbaseDocument source, final Couch
}

private String convertToString(Object propertyObj) {
if (propertyObj instanceof String) {
return (String) propertyObj;
if (propertyObj instanceof String str) {
return str;
} else if (propertyObj instanceof Number) {
return new StringBuffer().append(propertyObj).toString();
} else {
Expand Down Expand Up @@ -821,10 +821,10 @@ private Object readCollection(final TypeInformation<?> targetType, final Couchba

Object dbObjItem = source.get(i);

if (dbObjItem instanceof CouchbaseDocument) {
items.add(read(componentType, (CouchbaseDocument) dbObjItem, parent));
} else if (dbObjItem instanceof CouchbaseList) {
items.add(readCollection(componentType != null ? componentType :TypeInformation.of(dbObjItem.getClass()), (CouchbaseList) dbObjItem, parent));
if (dbObjItem instanceof CouchbaseDocument couchbaseDocument) {
items.add(read(componentType, couchbaseDocument, parent));
} else if (dbObjItem instanceof CouchbaseList couchbaseList) {
items.add(readCollection(componentType != null ? componentType :TypeInformation.of(dbObjItem.getClass()), couchbaseList, parent));
} else {
items.add(getPotentiallyConvertedSimpleRead(dbObjItem, rawComponentType));
}
Expand Down Expand Up @@ -915,8 +915,8 @@ public void setApplicationContext(ApplicationContext applicationContext) {
setEntityCallbacks(EntityCallbacks.create(applicationContext));
}
ClassLoader classLoader = applicationContext.getClassLoader();
if (this.typeMapper instanceof BeanClassLoaderAware && classLoader != null) {
((BeanClassLoaderAware) this.typeMapper).setBeanClassLoader(classLoader);
if (this.typeMapper instanceof BeanClassLoaderAware beanClassLoaderAware && classLoader != null) {
beanClassLoaderAware.setBeanClassLoader(classLoader);
}
}

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

if (conversions.hasCustomReadTarget(value.getClass(), rawType)) {
return (R) conversionService.convert(value, rawType);
} else if (value instanceof CouchbaseDocument) {
return (R) read(type, (CouchbaseDocument) value, parent);
} else if (value instanceof CouchbaseList) {
return (R) readCollection(type, (CouchbaseList) value, parent);
} else if (value instanceof CouchbaseDocument couchbaseDocument) {
return (R) read(type, couchbaseDocument, parent);
} else if (value instanceof CouchbaseList couchbaseList) {
return (R) readCollection(type, couchbaseList, parent);
} else {
return (R) getPotentiallyConvertedSimpleRead(value, type.getType()); // type does not have annotations
}
Expand Down Expand Up @@ -982,11 +982,11 @@ public <R> R readValue(Object value, CouchbasePersistentProperty prop, Object pa
TypeInformation ti = TypeInformation.of(value.getClass());
return (R) conversionService.convert(value, ti.toTypeDescriptor(), new TypeDescriptor(prop.getField()));
}
if (value instanceof CouchbaseDocument) {
return (R) read(prop.getTypeInformation(), (CouchbaseDocument) value, parent);
if (value instanceof CouchbaseDocument couchbaseDocument) {
return (R) read(prop.getTypeInformation(), couchbaseDocument, parent);
}
if (value instanceof CouchbaseList) {
return (R) readCollection(prop.getTypeInformation(), (CouchbaseList) value, parent);
if (value instanceof CouchbaseList couchbaseList) {
return (R) readCollection(prop.getTypeInformation(), couchbaseList, parent);
}
return (R) getPotentiallyConvertedSimpleRead(value, prop);// passes PersistentProperty with annotations

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ private void encodeRecursive(final CouchbaseStorable source, final JsonGenerator
String key = entry.getKey();
Object value = entry.getValue();
generator.writeFieldName(key);
if (value instanceof CouchbaseDocument) {
encodeRecursive((CouchbaseDocument) value, generator);
if (value instanceof CouchbaseDocument couchbaseDocument) {
encodeRecursive(couchbaseDocument, generator);
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ private void checkForAndCreateIndexes(final CouchbasePersistentEntity<?> entity)
if (entity.isAnnotationPresent(Document.class)) {

for (IndexDefinition indexDefinition : indexResolver.resolveIndexFor(entity.getTypeInformation())) {
IndexDefinitionHolder indexToCreate = indexDefinition instanceof IndexDefinitionHolder
? (IndexDefinitionHolder) indexDefinition
IndexDefinitionHolder indexToCreate = indexDefinition instanceof IndexDefinitionHolder indexDefinitionHolder
? indexDefinitionHolder
: new IndexDefinitionHolder(indexDefinition.getIndexFields(), indexDefinition.getIndexName(),
indexDefinition.getIndexPredicate());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ public final int size(final boolean recursive) {

int totalSize = thisSize;
for (Object value : payload) {
if (value instanceof CouchbaseDocument) {
totalSize += ((CouchbaseDocument) value).size(true);
} else if (value instanceof CouchbaseList) {
totalSize += ((CouchbaseList) value).size(true);
if (value instanceof CouchbaseDocument couchbaseDocument) {
totalSize += couchbaseDocument.size(true);
} else if (value instanceof CouchbaseList couchbaseList) {
totalSize += couchbaseList.size(true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,23 @@ public void setValue(String key, @Nullable Object value) {

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

if (value == null || (value instanceof String && !StringUtils.hasText((String) value))) {
if (value == null || (value instanceof String str && !StringUtils.hasText(str))) {
this.values.remove(MetaKey.valueOf(key));
}
this.values.put(MetaKey.valueOf(key), value);
}

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

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

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

if (value == null || (value instanceof String && !StringUtils.hasText((String) value))) {
if (value == null || (value instanceof String str && !StringUtils.hasText(str))) {
this.values.remove(key);
}
this.values.put(key, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ public static N1QLExpression path(Object... pathComponents) {
StringBuilder path = new StringBuilder();
for (Object p : pathComponents) {
path.append('.');
if (p instanceof N1QLExpression) {
path.append(((N1QLExpression) p).toString());
if (p instanceof N1QLExpression n1QLExpression) {
path.append(n1QLExpression.toString());
} else {
path.append(String.valueOf(p));
}
Expand Down
Loading

0 comments on commit ca7c3a7

Please sign in to comment.