Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Add missing generics. Inline superfluous methods.

See #1384
  • Loading branch information
mp911de committed May 25, 2023
1 parent 7e831fd commit e871254
Show file tree
Hide file tree
Showing 5 changed files with 348 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,15 @@
import org.springframework.data.cassandra.core.mapping.*;
import org.springframework.data.cassandra.core.mapping.Embedded.OnEmpty;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.mapping.AccessOptions;
import org.springframework.data.mapping.InstanceCreatorMetadata;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.Parameter;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.PersistentPropertyAccessor;
import org.springframework.data.mapping.PersistentPropertyPath;
import org.springframework.data.mapping.PersistentPropertyPathAccessor;
import org.springframework.data.mapping.context.MappingContext;
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
import org.springframework.data.mapping.model.DefaultSpELExpressionEvaluator;
Expand Down Expand Up @@ -283,6 +286,7 @@ private <T> Class<T> transformClassToBeanClassLoaderClass(Class<T> entity) {
}

@Override
@SuppressWarnings("unchecked")
public <R> R project(EntityProjection<R, ?> projection, Row row) {

if (!projection.isProjection()) {
Expand Down Expand Up @@ -665,7 +669,7 @@ private void writeWhereFromObject(Object source, Where sink, CassandraPersistent
? getMappingContext().getRequiredPersistentEntity(compositeIdProperty)
: entity;

writeWhere(MapId.class.cast(id), sink, whereEntity);
writeWhere((MapId) id, sink, whereEntity);
return;
}

Expand Down Expand Up @@ -725,18 +729,6 @@ private void writeWhere(MapId id, Where sink, CassandraPersistentEntity<?> entit
}
}

private void writeWhere(ConvertingPropertyAccessor<?> accessor, Where sink, CassandraPersistentEntity<?> entity) {

Assert.isTrue(entity.isCompositePrimaryKey(),
() -> String.format("Entity [%s] is not a composite primary key", entity.getName()));

for (CassandraPersistentProperty property : entity) {
TypeCodec<Object> codec = getCodec(property);
Object value = accessor.getProperty(property, codec.getJavaType().getRawType());
sink.put(property.getRequiredColumnName(), value);
}
}

private void writeTupleValue(ConvertingPropertyAccessor<?> propertyAccessor, TupleValue tupleValue,
CassandraPersistentEntity<?> entity) {

Expand Down Expand Up @@ -863,7 +855,7 @@ private Class<?> getTargetType(CassandraPersistentProperty property) {
*/
@Nullable
@SuppressWarnings("unchecked")
private <T> T getWriteValue(CassandraPersistentProperty property, ConvertingPropertyAccessor propertyAccessor) {
private <T> T getWriteValue(CassandraPersistentProperty property, ConvertingPropertyAccessor<?> propertyAccessor) {

ColumnType cassandraTypeDescriptor = cassandraTypeResolver.resolve(property);

Expand Down Expand Up @@ -914,7 +906,7 @@ private Object getWriteValue(@Nullable Object value, ColumnType columnType) {
return writeMapInternal((Map<Object, Object>) value, columnType);
}

TypeInformation<?> type = TypeInformation.of((Class) value.getClass());
TypeInformation<?> type = TypeInformation.of((Class<?>) value.getClass());
TypeInformation<?> actualType = type.getRequiredActualType();
BasicCassandraPersistentEntity<?> entity = getMappingContext().getPersistentEntity(actualType.getType());

Expand Down Expand Up @@ -989,7 +981,6 @@ private Object writeMapInternal(Map<Object, Object> source, ColumnType type) {
* @param requestedTargetType must not be {@literal null}.
* @see org.springframework.data.cassandra.core.mapping.CassandraType
*/
@SuppressWarnings("unchecked")
@Nullable
private Object getPotentiallyConvertedSimpleValue(@Nullable Object value, @Nullable Class<?> requestedTargetType) {

Expand Down Expand Up @@ -1237,7 +1228,7 @@ public <T> T getParameterValue(Parameter<T, CassandraPersistentProperty> paramet
* Extension of {@link SpELExpressionParameterValueProvider} to recursively trigger value conversion on the raw
* resolved SpEL value.
*/
private class ConverterAwareSpELExpressionParameterValueProvider
private static class ConverterAwareSpELExpressionParameterValueProvider
extends SpELExpressionParameterValueProvider<CassandraPersistentProperty> {

private final ConversionContext context;
Expand Down Expand Up @@ -1450,16 +1441,8 @@ private CassandraPersistentProperty getPersistentProperty(String name, TypeInfor
}
}

private static class PropertyTranslatingPropertyAccessor<T> implements PersistentPropertyPathAccessor<T> {

private final PersistentPropertyAccessor<T> delegate;
private final PersistentPropertyTranslator propertyTranslator;

private PropertyTranslatingPropertyAccessor(PersistentPropertyAccessor<T> delegate,
PersistentPropertyTranslator propertyTranslator) {
this.delegate = delegate;
this.propertyTranslator = propertyTranslator;
}
private record PropertyTranslatingPropertyAccessor<T> (PersistentPropertyAccessor<T> delegate,
PersistentPropertyTranslator propertyTranslator) implements PersistentPropertyPathAccessor<T> {

static <T> PersistentPropertyAccessor<T> create(PersistentPropertyAccessor<T> delegate,
PersistentPropertyTranslator propertyTranslator) {
Expand Down Expand Up @@ -1566,7 +1549,7 @@ static class MapPersistentPropertyAccessor implements PersistentPropertyAccessor
Map<String, Object> map = new LinkedHashMap<>();

@Override
public void setProperty(PersistentProperty<?> persistentProperty, Object o) {
public void setProperty(PersistentProperty<?> persistentProperty, @Nullable Object o) {
map.put(persistentProperty.getName(), o);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.convert.CassandraConverter;
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.repository.query.CassandraQueryExecution.CollectionExecution;
import org.springframework.data.cassandra.repository.query.CassandraQueryExecution.ExistsExecution;
import org.springframework.data.cassandra.repository.query.CassandraQueryExecution.ResultProcessingConverter;
Expand All @@ -31,7 +29,6 @@
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.data.repository.query.ResultProcessor;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import com.datastax.oss.driver.api.core.cql.SimpleStatement;
import com.datastax.oss.driver.api.core.cql.Statement;
Expand All @@ -56,7 +53,7 @@ public abstract class AbstractCassandraQuery extends CassandraRepositoryQuerySup
*/
public AbstractCassandraQuery(CassandraQueryMethod queryMethod, CassandraOperations operations) {

super(queryMethod, toMappingContext(operations));
super(queryMethod, operations.getConverter().getMappingContext());

this.operations = operations;
}
Expand All @@ -81,7 +78,7 @@ public Object execute(Object[] parameters) {
Statement<?> statement = createQuery(parameterAccessor);

CassandraQueryExecution queryExecution = getExecution(parameterAccessor,
new ResultProcessingConverter(resultProcessor, toMappingContext(getOperations()), getEntityInstantiators()));
new ResultProcessingConverter(resultProcessor, getMappingContext(), getEntityInstantiators()));

Class<?> resultType = resolveResultType(resultProcessor);

Expand Down Expand Up @@ -170,14 +167,4 @@ private CassandraQueryExecution getExecutionToWrap(CassandraParameterAccessor pa
*/
protected abstract boolean isModifyingQuery();

private static CassandraConverter toConverter(CassandraOperations operations) {

Assert.notNull(operations, "CassandraOperations must not be null");

return operations.getConverter();
}

private static CassandraMappingContext toMappingContext(CassandraOperations operations) {
return toConverter(operations).getMappingContext();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import org.springframework.data.cassandra.ReactiveResultSet;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.ReactiveCassandraOperations;
import org.springframework.data.cassandra.core.convert.CassandraConverter;
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.repository.query.ReactiveCassandraQueryExecution.CollectionExecution;
import org.springframework.data.cassandra.repository.query.ReactiveCassandraQueryExecution.ExistsExecution;
import org.springframework.data.cassandra.repository.query.ReactiveCassandraQueryExecution.ResultProcessingConverter;
Expand All @@ -33,7 +31,6 @@
import org.springframework.data.repository.query.ParameterAccessor;
import org.springframework.data.repository.query.RepositoryQuery;
import org.springframework.data.repository.query.ResultProcessor;
import org.springframework.util.Assert;

import com.datastax.oss.driver.api.core.cql.SimpleStatement;

Expand All @@ -58,7 +55,7 @@ public abstract class AbstractReactiveCassandraQuery extends CassandraRepository
*/
public AbstractReactiveCassandraQuery(ReactiveCassandraQueryMethod method, ReactiveCassandraOperations operations) {

super(method, getRequiredMappingContext(operations));
super(method, operations.getConverter().getMappingContext());

this.operations = operations;
}
Expand All @@ -83,8 +80,8 @@ private Publisher<Object> executeLater(ReactiveCassandraParameterAccessor parame

Mono<SimpleStatement> statement = createQuery(parameterAccessor);
ResultProcessor resultProcessor = getQueryMethod().getResultProcessor().withDynamicProjection(parameterAccessor);
ReactiveCassandraQueryExecution queryExecution = getExecution(parameterAccessor, new ResultProcessingConverter(
resultProcessor, getRequiredMappingContext(getReactiveCassandraOperations()), getEntityInstantiators()));
ReactiveCassandraQueryExecution queryExecution = getExecution(parameterAccessor,
new ResultProcessingConverter(resultProcessor, getMappingContext(), getEntityInstantiators()));

Class<?> resultType = resolveResultType(resultProcessor);

Expand All @@ -94,7 +91,7 @@ private Publisher<Object> executeLater(ReactiveCassandraParameterAccessor parame
private Class<?> resolveResultType(ResultProcessor resultProcessor) {

CassandraReturnedType returnedType = new CassandraReturnedType(resultProcessor.getReturnedType(),
getRequiredConverter(getReactiveCassandraOperations()).getCustomConversions());
getReactiveCassandraOperations().getConverter().getCustomConversions());

return returnedType.getResultType();
}
Expand Down Expand Up @@ -173,14 +170,4 @@ private ReactiveCassandraQueryExecution getExecutionToWrap(CassandraParameterAcc
*/
protected abstract boolean isModifyingQuery();

private static CassandraConverter getRequiredConverter(ReactiveCassandraOperations operations) {

Assert.notNull(operations, "ReactiveCassandraOperations must not be null");

return operations.getConverter();
}

private static CassandraMappingContext getRequiredMappingContext(ReactiveCassandraOperations operations) {
return getRequiredConverter(operations).getMappingContext();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public abstract class CassandraRepositoryQuerySupport implements RepositoryQuery

private final QueryStatementCreator queryStatementCreator;

private final MappingContext<? extends CassandraPersistentEntity<?>, CassandraPersistentProperty> mappingContext;

/**
* Create a new {@link AbstractCassandraQuery} from the given {@link CassandraQueryMethod} and
* {@link CassandraOperations}.
Expand Down Expand Up @@ -78,6 +80,7 @@ public CassandraRepositoryQuerySupport(CassandraQueryMethod queryMethod,
this.queryMethod = queryMethod;
this.instantiators = new EntityInstantiators();
this.queryStatementCreator = new QueryStatementCreator(queryMethod, mappingContext);
this.mappingContext = mappingContext;
}

@Override
Expand All @@ -93,6 +96,10 @@ protected QueryStatementCreator getQueryStatementCreator() {
return this.queryStatementCreator;
}

protected MappingContext<? extends CassandraPersistentEntity<?>, CassandraPersistentProperty> getMappingContext() {
return mappingContext;
}

class CassandraReturnedType {

private final ReturnedType returnedType;
Expand Down
Loading

0 comments on commit e871254

Please sign in to comment.