DATACASS-84 - Polish.
Resolves gh-171.
This commit is contained in:
@@ -58,8 +58,9 @@ public abstract class AbstractCassandraConfiguration extends AbstractSessionConf
|
||||
private @Nullable ClassLoader beanClassLoader;
|
||||
|
||||
/**
|
||||
* Creates a {@link CassandraConverter} using the configured {@link #cassandraMapping()}. Will apply all specified
|
||||
* {@link #customConversions()}.
|
||||
* Creates a {@link CassandraConverter} using the configured {@link #cassandraMapping()}.
|
||||
*
|
||||
* Will apply all specified {@link #customConversions()}.
|
||||
*
|
||||
* @return {@link CassandraConverter} used to convert Java and Cassandra value types during the mapping process.
|
||||
* @see #cassandraMapping()
|
||||
@@ -68,11 +69,11 @@ public abstract class AbstractCassandraConfiguration extends AbstractSessionConf
|
||||
@Bean
|
||||
public CassandraConverter cassandraConverter() {
|
||||
|
||||
UserTypeResolver userTypeResolver = new SimpleUserTypeResolver(getRequiredSession(),
|
||||
CqlIdentifier.fromCql(getKeyspaceName()));
|
||||
UserTypeResolver userTypeResolver =
|
||||
new SimpleUserTypeResolver(getRequiredSession(), CqlIdentifier.fromCql(getKeyspaceName()));
|
||||
|
||||
MappingCassandraConverter converter = new MappingCassandraConverter(
|
||||
requireBeanOfType(CassandraMappingContext.class));
|
||||
MappingCassandraConverter converter =
|
||||
new MappingCassandraConverter(requireBeanOfType(CassandraMappingContext.class));
|
||||
|
||||
converter.setCodecRegistry(getRequiredSession().getContext().getCodecRegistry());
|
||||
converter.setUserTypeResolver(userTypeResolver);
|
||||
@@ -82,20 +83,20 @@ public abstract class AbstractCassandraConfiguration extends AbstractSessionConf
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the {@link MappingContext} instance to map Entities to properties.
|
||||
* Return the {@link MappingContext} instance to map Entities to {@link Object Java Objects}.
|
||||
*
|
||||
* @throws ClassNotFoundException if the Cassandra Entity class type identified by name cannot be found during the
|
||||
* scan.
|
||||
* @see CassandraMappingContext
|
||||
* @throws ClassNotFoundException if the Cassandra Entity class type identified by name
|
||||
* cannot be found during the scan.
|
||||
* @see org.springframework.data.cassandra.core.mapping.CassandraMappingContext
|
||||
*/
|
||||
@Bean
|
||||
public CassandraMappingContext cassandraMapping() throws ClassNotFoundException {
|
||||
|
||||
UserTypeResolver userTypeResolver = new SimpleUserTypeResolver(getRequiredSession(),
|
||||
CqlIdentifier.fromCql(getKeyspaceName()));
|
||||
UserTypeResolver userTypeResolver =
|
||||
new SimpleUserTypeResolver(getRequiredSession(), CqlIdentifier.fromCql(getKeyspaceName()));
|
||||
|
||||
CassandraMappingContext mappingContext = new CassandraMappingContext(userTypeResolver,
|
||||
SimpleTupleTypeFactory.DEFAULT);
|
||||
CassandraMappingContext mappingContext =
|
||||
new CassandraMappingContext(userTypeResolver, SimpleTupleTypeFactory.DEFAULT);
|
||||
|
||||
CustomConversions customConversions = requireBeanOfType(CassandraCustomConversions.class);
|
||||
|
||||
|
||||
@@ -108,13 +108,13 @@ public class BasicCassandraPersistentEntity<T> extends BasicPersistentEntity<T,
|
||||
return determineName(annotation.value(), annotation.forceQuote());
|
||||
}
|
||||
|
||||
return IdentifierFactory.create(namingStrategy.getTableName(this), false);
|
||||
return IdentifierFactory.create(getNamingStrategy().getTableName(this), false);
|
||||
}
|
||||
|
||||
CqlIdentifier determineName(String value, boolean forceQuote) {
|
||||
|
||||
if (!StringUtils.hasText(value)) {
|
||||
return IdentifierFactory.create(namingStrategy.getTableName(this), forceQuote);
|
||||
return IdentifierFactory.create(getNamingStrategy().getTableName(this), forceQuote);
|
||||
}
|
||||
|
||||
String name = Optional.ofNullable(this.spelContext).map(it -> SpelUtils.evaluate(value, it)).orElse(value);
|
||||
@@ -256,5 +256,4 @@ public class BasicCassandraPersistentEntity<T> extends BasicPersistentEntity<T,
|
||||
public boolean isUserDefinedType() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,7 +22,6 @@ import java.lang.reflect.Field;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -184,7 +183,8 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP
|
||||
return null;
|
||||
}
|
||||
|
||||
Supplier<String> defaultName = () -> namingStrategy.getColumnName(this);
|
||||
Supplier<String> defaultName = () -> getNamingStrategy().getColumnName(this);
|
||||
|
||||
String overriddenName = null;
|
||||
|
||||
boolean forceQuote = false;
|
||||
@@ -259,6 +259,10 @@ public class BasicCassandraPersistentProperty extends AnnotationBasedPersistentP
|
||||
this.namingStrategy = namingStrategy;
|
||||
}
|
||||
|
||||
NamingStrategy getNamingStrategy() {
|
||||
return this.namingStrategy;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.cassandra.core.mapping.CassandraPersistentProperty#setForceQuote(boolean)
|
||||
*/
|
||||
|
||||
@@ -199,9 +199,8 @@ public class CassandraMappingContext
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @deprecated since 3.0. Use custom conversion through
|
||||
* {@link org.springframework.data.cassandra.core.convert.MappingCassandraConverter}.
|
||||
* {@link org.springframework.data.cassandra.core.convert.MappingCassandraConverter}.
|
||||
*/
|
||||
@Deprecated
|
||||
public CustomConversions getCustomConversions() {
|
||||
@@ -255,9 +254,8 @@ public class CassandraMappingContext
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @deprecated since 3.0. Retrieve {@link CodecRegistry} directly from
|
||||
* {@link org.springframework.data.cassandra.core.convert.CassandraConverter}.
|
||||
* {@link org.springframework.data.cassandra.core.convert.CassandraConverter}.
|
||||
*/
|
||||
@Deprecated
|
||||
public CodecRegistry getCodecRegistry() {
|
||||
@@ -305,9 +303,8 @@ public class CassandraMappingContext
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* @deprecated since 3.0. Retrieve {@link UserTypeResolver} directly from
|
||||
* {@link org.springframework.data.cassandra.core.convert.CassandraConverter}.
|
||||
* {@link org.springframework.data.cassandra.core.convert.CassandraConverter}.
|
||||
*/
|
||||
@Nullable
|
||||
@Deprecated
|
||||
@@ -378,8 +375,9 @@ public class CassandraMappingContext
|
||||
|
||||
BasicCassandraPersistentEntity<T> entity = isUserDefinedType(typeInformation)
|
||||
? new CassandraUserTypePersistentEntity<>(typeInformation, getVerifier())
|
||||
: isTuple(typeInformation) ? new BasicCassandraPersistentTupleEntity<>(typeInformation)
|
||||
: new BasicCassandraPersistentEntity<>(typeInformation, getVerifier());
|
||||
: isTuple(typeInformation)
|
||||
? new BasicCassandraPersistentTupleEntity<>(typeInformation)
|
||||
: new BasicCassandraPersistentEntity<>(typeInformation, getVerifier());
|
||||
|
||||
entity.setNamingStrategy(this.namingStrategy);
|
||||
Optional.ofNullable(this.applicationContext).ifPresent(entity::setApplicationContext);
|
||||
@@ -447,10 +445,9 @@ public class CassandraMappingContext
|
||||
|
||||
return getPersistentEntities().stream().flatMap(entity -> StreamSupport.stream(entity.spliterator(), false))
|
||||
.flatMap(it -> Optionals.toStream(Optional.ofNullable(it.findAnnotation(CassandraType.class))))
|
||||
.map(CassandraType::userTypeName) //
|
||||
.filter(StringUtils::hasText) //
|
||||
.map(CqlIdentifier::fromCql) //
|
||||
.map(CassandraType::userTypeName)
|
||||
.filter(StringUtils::hasText)
|
||||
.map(CqlIdentifier::fromCql)
|
||||
.anyMatch(identifier::equals);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -46,21 +46,21 @@ public interface NamingStrategy {
|
||||
/**
|
||||
* Create a table name from the given {@link CassandraPersistentEntity}.
|
||||
*/
|
||||
default String getTableName(CassandraPersistentEntity<?> type) {
|
||||
default String getTableName(CassandraPersistentEntity<?> entity) {
|
||||
|
||||
Assert.notNull(type, "CassandraPersistentEntity must not be null!");
|
||||
Assert.notNull(entity, "CassandraPersistentEntity must not be null");
|
||||
|
||||
return type.getType().getSimpleName();
|
||||
return entity.getType().getSimpleName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a user-defined type name from the given {@link CassandraPersistentEntity}.
|
||||
*/
|
||||
default String getUserDefinedTypeName(CassandraPersistentEntity<?> type) {
|
||||
default String getUserDefinedTypeName(CassandraPersistentEntity<?> entity) {
|
||||
|
||||
Assert.notNull(type, "CassandraPersistentEntity must not be null!");
|
||||
Assert.notNull(entity, "CassandraPersistentEntity must not be null");
|
||||
|
||||
return type.getType().getSimpleName();
|
||||
return entity.getType().getSimpleName();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,14 +68,16 @@ public interface NamingStrategy {
|
||||
*/
|
||||
default String getColumnName(CassandraPersistentProperty property) {
|
||||
|
||||
Assert.notNull(property, "CassandraPersistentProperty must not be null!");
|
||||
Assert.notNull(property, "CassandraPersistentProperty must not be null");
|
||||
|
||||
return property.getName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply a {@link UnaryOperator transformation function} to create a new {@link NamingStrategy} that applies the given
|
||||
* transformation to each name component. Example:
|
||||
* Apply a {@link UnaryOperator transformation function} to create a new {@link NamingStrategy}
|
||||
* that applies the given transformation to each name component.
|
||||
*
|
||||
* Example:
|
||||
* <p class="code">
|
||||
* NamingStrategy lower = NamingStrategy.INSTANCE.transform(String::toLowerCase);
|
||||
* </p>
|
||||
@@ -85,7 +87,7 @@ public interface NamingStrategy {
|
||||
*/
|
||||
default NamingStrategy transform(UnaryOperator<String> mappingFunction) {
|
||||
|
||||
Assert.notNull(mappingFunction, "Mapping function must not be null!");
|
||||
Assert.notNull(mappingFunction, "Mapping function must not be null");
|
||||
|
||||
NamingStrategy previous = this;
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ import org.springframework.data.util.ParsingUtils;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Naming strategy that renders CamelCase name parts to {@code snake_case}.
|
||||
* Naming strategy that renders {@literal CamelCase} name parts to {@literal snake_case}.
|
||||
*
|
||||
* @author Mark Paluch
|
||||
* @since 3.0
|
||||
@@ -31,21 +31,21 @@ public class SnakeCaseNamingStrategy implements NamingStrategy {
|
||||
/**
|
||||
* Uses {@link Class#getSimpleName()} and separates camel case parts with '_'.
|
||||
*/
|
||||
public String getTableName(CassandraPersistentEntity<?> type) {
|
||||
public String getTableName(CassandraPersistentEntity<?> entity) {
|
||||
|
||||
Assert.notNull(type, "CassandraPersistentEntity must not be null!");
|
||||
Assert.notNull(entity, "CassandraPersistentEntity must not be null");
|
||||
|
||||
return ParsingUtils.reconcatenateCamelCase(type.getType().getSimpleName(), "_");
|
||||
return ParsingUtils.reconcatenateCamelCase(entity.getType().getSimpleName(), "_");
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses {@link Class#getSimpleName()} and separates camel case parts with '_'.
|
||||
*/
|
||||
public String getUserDefinedTypeName(CassandraPersistentEntity<?> type) {
|
||||
public String getUserDefinedTypeName(CassandraPersistentEntity<?> entity) {
|
||||
|
||||
Assert.notNull(type, "CassandraPersistentEntity must not be null!");
|
||||
Assert.notNull(entity, "CassandraPersistentEntity must not be null");
|
||||
|
||||
return ParsingUtils.reconcatenateCamelCase(type.getType().getSimpleName(), "_");
|
||||
return ParsingUtils.reconcatenateCamelCase(entity.getType().getSimpleName(), "_");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,7 +53,7 @@ public class SnakeCaseNamingStrategy implements NamingStrategy {
|
||||
*/
|
||||
public String getColumnName(CassandraPersistentProperty property) {
|
||||
|
||||
Assert.notNull(property, "CassandraPersistentProperty must not be null.");
|
||||
Assert.notNull(property, "CassandraPersistentProperty must not be null");
|
||||
|
||||
return ParsingUtils.reconcatenateCamelCase(property.getName(), "_");
|
||||
}
|
||||
|
||||
@@ -26,26 +26,27 @@ import java.util.function.Function;
|
||||
*/
|
||||
public class TransformingNamingStrategy implements NamingStrategy {
|
||||
|
||||
private final NamingStrategy delegate;
|
||||
private final Function<String, String> mappingFunction;
|
||||
|
||||
private final NamingStrategy delegate;
|
||||
|
||||
public TransformingNamingStrategy(NamingStrategy delegate, Function<String, String> mappingFunction) {
|
||||
this.delegate = delegate;
|
||||
this.mappingFunction = mappingFunction;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTableName(CassandraPersistentEntity<?> type) {
|
||||
return mappingFunction.apply(delegate.getTableName(type));
|
||||
public String getTableName(CassandraPersistentEntity<?> entity) {
|
||||
return this.mappingFunction.apply(this.delegate.getTableName(entity));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUserDefinedTypeName(CassandraPersistentEntity<?> type) {
|
||||
return mappingFunction.apply(delegate.getUserDefinedTypeName(type));
|
||||
public String getUserDefinedTypeName(CassandraPersistentEntity<?> entity) {
|
||||
return this.mappingFunction.apply(this.delegate.getUserDefinedTypeName(entity));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getColumnName(CassandraPersistentProperty property) {
|
||||
return mappingFunction.apply(delegate.getColumnName(property));
|
||||
return this.mappingFunction.apply(this.delegate.getColumnName(property));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.cassandra.core.mapping;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.cassandra.core.mapping;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
Reference in New Issue
Block a user