Skip to content

Commit

Permalink
Merge pull request #1056 from SpineEventEngine/fix-column-lookup
Browse files Browse the repository at this point in the history
Fix column lookup
  • Loading branch information
dmdashenkov authored May 3, 2019
2 parents 6ff18d6 + d506ddf commit f815bb9
Show file tree
Hide file tree
Showing 32 changed files with 256 additions and 143 deletions.
16 changes: 8 additions & 8 deletions license-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Mon Apr 29 18:54:03 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Wed May 01 17:14:08 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).



Expand Down Expand Up @@ -821,7 +821,7 @@ This report was generated on **Mon Apr 29 18:54:03 EEST 2019** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Mon Apr 29 18:54:03 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Wed May 01 17:14:09 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).



Expand Down Expand Up @@ -1359,7 +1359,7 @@ This report was generated on **Mon Apr 29 18:54:03 EEST 2019** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Mon Apr 29 18:54:04 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Wed May 01 17:14:09 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).



Expand Down Expand Up @@ -2059,7 +2059,7 @@ This report was generated on **Mon Apr 29 18:54:04 EEST 2019** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Mon Apr 29 18:54:04 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Wed May 01 17:14:10 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).



Expand Down Expand Up @@ -2603,7 +2603,7 @@ This report was generated on **Mon Apr 29 18:54:04 EEST 2019** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Mon Apr 29 18:54:05 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Wed May 01 17:14:10 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).



Expand Down Expand Up @@ -3108,7 +3108,7 @@ This report was generated on **Mon Apr 29 18:54:05 EEST 2019** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Mon Apr 29 18:54:05 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Wed May 01 17:14:10 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).



Expand Down Expand Up @@ -3621,7 +3621,7 @@ This report was generated on **Mon Apr 29 18:54:05 EEST 2019** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Mon Apr 29 18:54:06 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Wed May 01 17:14:11 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).



Expand Down Expand Up @@ -4234,4 +4234,4 @@ This report was generated on **Mon Apr 29 18:54:06 EEST 2019** using [Gradle-Lic
The dependencies distributed under several licenses, are used according their commercial-use-friendly license.


This report was generated on **Mon Apr 29 18:54:06 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
This report was generated on **Wed May 01 17:14:11 EEST 2019** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE).
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Lists.newLinkedList;
import static io.spine.server.entity.storage.Methods.IS_PREFIX;
import static io.spine.server.entity.storage.Methods.getAnnotatedVersion;
import static io.spine.server.entity.storage.Methods.annotatedVersion;
import static io.spine.util.Exceptions.newIllegalStateException;
import static java.util.stream.Stream.concat;

Expand All @@ -58,13 +59,7 @@
* @see Columns
* @see EntityColumn
*/
class ColumnReader {

/**
* A predicate to check if the given method or one of its predecessors are annotated with
* {@link Column}.
*/
private static final Predicate<Method> hasAnnotatedVersion = hasAnnotatedVersion();
final class ColumnReader {

/**
* A predicate to check whether the given method represents an entity property with the
Expand All @@ -74,19 +69,19 @@ class ColumnReader {
static final Predicate<Method> isBooleanWrapperProperty = isBooleanWrapperProperty();

private final BeanInfo entityDescriptor;
private final String className;
private final Class<? extends Entity<?, ?>> entityClass;

/**
* Creates a new {@code ColumnReader} instance.
*
* @param entityDescriptor
* a descriptor of the entity as a Java Bean
* @param className
* an entity class name for logging purposes
* the descriptor of the entity as a Java Bean
* @param entityClass
* the entity class
*/
private ColumnReader(BeanInfo entityDescriptor, String className) {
private ColumnReader(BeanInfo entityDescriptor, Class<? extends Entity<?, ?>> entityClass) {
this.entityDescriptor = entityDescriptor;
this.className = className;
this.entityClass = entityClass;
}

/**
Expand All @@ -96,11 +91,11 @@ private ColumnReader(BeanInfo entityDescriptor, String className) {
* the {@link Entity} class for which to create the instance
* @return a new instance of {@code ColumnReader} for the specified class
*/
static ColumnReader forClass(Class<? extends Entity> entityClass) {
static ColumnReader forClass(Class<? extends Entity<?, ?>> entityClass) {
checkNotNull(entityClass);
try {
BeanInfo entityDescriptor = Introspector.getBeanInfo(entityClass);
return new ColumnReader(entityDescriptor, entityClass.getName());
return new ColumnReader(entityDescriptor, entityClass);
} catch (IntrospectionException e) {
throw new IllegalStateException(e);
}
Expand Down Expand Up @@ -144,10 +139,11 @@ private ImmutableSet<EntityColumn> scanColumns() {
.stream(methodDescriptors)
.map(MethodDescriptor::getMethod)
.filter(isBooleanWrapperProperty);

Stream<Method> candidates = concat(propertyAccessors, booleanWrapperGetters);
ImmutableSet<EntityColumn> columns = candidates
.filter(hasAnnotatedVersion)
.map(method -> annotatedVersion(entityClass, method))
.filter(Optional::isPresent)
.map(Optional::get)
.map(EntityColumn::from)
.collect(toImmutableSet());
return columns;
Expand All @@ -170,17 +166,14 @@ private void checkRepeatedColumnNames(Iterable<EntityColumn> columns) {
if (checkedNames.contains(columnName)) {
throw newIllegalStateException(
"The entity `%s` has columns with the same name for storing `%s`.",
className,
columnName);
entityClass.getName(),
columnName
);
}
checkedNames.add(columnName);
}
}

private static Predicate<Method> hasAnnotatedVersion() {
return method -> getAnnotatedVersion(method).isPresent();
}

private static Predicate<Method> isBooleanWrapperProperty() {
return method -> {
Class<?> returnType = method.getReturnType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

package io.spine.server.entity.storage;

import io.spine.annotation.Internal;
import io.spine.server.entity.Entity;
import io.spine.server.entity.storage.EntityColumn.MemoizedValue;

Expand Down Expand Up @@ -62,8 +61,7 @@
*
* @see EntityColumn
*/
@Internal
public class Columns {
final class Columns {

/**
* Prevents instantiation of this utility class.
Expand All @@ -82,7 +80,7 @@ private Columns() {
* @return a {@code Collection} of {@link EntityColumn} corresponded to entity class
* @throws IllegalStateException if entity column definitions are incorrect
*/
static Collection<EntityColumn> getAllColumns(Class<? extends Entity> entityClass) {
static Collection<EntityColumn> getAllColumns(Class<? extends Entity<?, ?>> entityClass) {
checkNotNull(entityClass);

ColumnReader columnReader = ColumnReader.forClass(entityClass);
Expand All @@ -100,7 +98,7 @@ static Collection<EntityColumn> getAllColumns(Class<? extends Entity> entityClas
* @return an instance of {@link EntityColumn} with the given name
* @throws IllegalArgumentException if the {@link EntityColumn} is not found
*/
static EntityColumn findColumn(Class<? extends Entity> entityClass, String columnName) {
static EntityColumn findColumn(Class<? extends Entity<?, ?>> entityClass, String columnName) {
checkNotNull(entityClass);
checkColumnName(columnName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.gson.internal.Primitives.wrap;
import static io.spine.server.entity.storage.Methods.checkGetter;
import static io.spine.server.entity.storage.Methods.checkAnnotatedGetter;
import static io.spine.server.entity.storage.Methods.mayReturnNull;
import static io.spine.server.entity.storage.Methods.nameFromAnnotation;
import static io.spine.server.entity.storage.Methods.nameFromGetter;
import static io.spine.server.entity.storage.Methods.retrieveAnnotatedVersion;
import static io.spine.util.Exceptions.newIllegalArgumentException;
import static io.spine.util.Exceptions.newIllegalStateException;
import static java.lang.String.format;
Expand Down Expand Up @@ -234,15 +233,18 @@ private EntityColumn(Method getter,
/**
* Creates a new instance of the {@code EntityColumn} from the given getter method.
*
* <p>The given method must be annotated with the {@link Column} annotation.
* An {@code IllegalArgumentException} is thrown otherwise.
*
* @param getter
* the getter of the EntityColumn
* @return new instance of the {@code EntityColumn} reflecting the given property
*/
@VisibleForTesting
public static EntityColumn from(Method getter) {
checkGetter(getter);
Method annotatedVersion = retrieveAnnotatedVersion(getter);
checkAnnotatedGetter(getter);
String nameForQuery = nameFromGetter(getter);
String nameForStore = nameFromAnnotation(annotatedVersion).orElse(nameForQuery);
String nameForStore = nameFromAnnotation(getter).orElse(nameForQuery);
boolean nullable = mayReturnNull(getter);
return new EntityColumn(getter, nameForQuery, nameForStore, nullable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@
* @see Columns
*/
@Internal
public class EntityColumnCache {
public final class EntityColumnCache {

private final Class<? extends Entity> entityClass;
private final Class<? extends Entity<?, ?>> entityClass;
private boolean columnsCached = false;

/**
Expand All @@ -66,9 +66,7 @@ public class EntityColumnCache {
private final Map<String, EntityColumn> entityColumnData =
synchronizedMap(new LinkedHashMap<>());

private EntityColumnCache(Class<? extends Entity> entityClass) {
checkNotNull(entityClass);

private EntityColumnCache(Class<? extends Entity<?, ?>> entityClass) {
this.entityClass = entityClass;
}

Expand All @@ -83,9 +81,8 @@ private EntityColumnCache(Class<? extends Entity> entityClass) {
* be obtained and cached
* @return new instance
*/
public static EntityColumnCache initializeFor(Class<? extends Entity> entityClass) {
public static EntityColumnCache initializeFor(Class<? extends Entity<?, ?>> entityClass) {
checkNotNull(entityClass);

return new EntityColumnCache(entityClass);
}

Expand Down
Loading

0 comments on commit f815bb9

Please sign in to comment.