Skip to content

Commit

Permalink
Drop interface NamingStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
asereda-gs committed Dec 7, 2019
1 parent cb9d064 commit 764d3b5
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 53 deletions.
Expand Up @@ -22,9 +22,10 @@
import java.util.Objects;

/**
* Resolves container name given a class. Container in this context means table, collection, index etc.
* Resolves container name given a class. Container in this context means RDBMS table,
* mongo collection, ElasticSearch index etc.
*/
public interface ContainerNaming extends NamingStrategy<Class<?>> {
public interface ContainerNaming {

/**
* Resolve container name for a particular type. Container in this context means
Expand All @@ -33,7 +34,6 @@ public interface ContainerNaming extends NamingStrategy<Class<?>> {
* @throws UnsupportedOperationException if name can't be derived
* @return name of the container (never null)
*/
@Override
String name(Class<?> type);

/**
Expand Down
Expand Up @@ -18,16 +18,17 @@

import org.immutables.criteria.expression.Expression;

public interface ExpressionNaming extends NamingStrategy<Expression> {
import java.util.function.Function;

public interface ExpressionNaming {

/**
* Give a name to a expression
*/
@Override
String name(Expression expression);

static ExpressionNaming of(NamingStrategy<Expression> delegate) {
return delegate::name;
static ExpressionNaming from(Function<Expression, String> fn) {
return fn::apply;
}

}

This file was deleted.

Expand Up @@ -23,15 +23,14 @@
*
* @see org.immutables.criteria.Criteria.Id
*/
public interface PathNaming extends NamingStrategy<Path> {
public interface PathNaming {

/**
* Return name of a field (aka {@link Path}) like {@code a.b.c} or {@code _id}.
* Should take in consideration {@code ID} naming properties of the backend.
* @param path path to be converted to string
* @return path representation as string
*/
@Override
String name(Path path);

static PathNaming defaultNaming() {
Expand Down
Expand Up @@ -25,14 +25,15 @@

import java.util.Iterator;
import java.util.Objects;
import java.util.function.Function;

/**
* Gives an unique (generated) name to objects so they can be used as identifiers in queries.
* Same objects (as defined by object equality) will have same name.
*
* @param <T>
*/
public class UniqueCachedNaming<T> implements NamingStrategy<T> {
public class UniqueCachedNaming<T> implements Function<T, String> {

private static final Suggester<?> PREFIX_SUGGESTER = (first, attempts, size) -> "expr" + size;

Expand Down Expand Up @@ -82,18 +83,18 @@ public Converter<T, String> asConverter() {
return converter;
}

@Override
public String apply(T value) {
return asConverter().convert(value);
}

/**
* Suggest a name for a value
* Suggest new string name for a value
*/
private interface Suggester<T> {
String suggest(T value, int attempts, int size);
}

@Override
public String name(T toName) {
return asConverter().convert(toName);
}

public static <T> UniqueCachedNaming<T> of(Iterable<T> iterable) {
return new UniqueCachedNaming<>(iterable);
}
Expand Down
Expand Up @@ -107,7 +107,7 @@ ObjectNode jsonQuery() {
ObjectNode parent = json.with(AGGREGATIONS);
for (Expression expr: orderedGroupBy) {
final String name = ((Path) expr).toStringPath();
final String aggName = naming.name(expr);
final String aggName = naming.apply(expr);
final ObjectNode section = parent.with(aggName);
final ObjectNode terms = section.with("terms");
terms.put("field", name);
Expand All @@ -133,7 +133,7 @@ ObjectNode jsonQuery() {
ObjectNode agg = nodeFactory.objectNode();
String field = ((Path) call.arguments().get(0)).toStringPath();
agg.with(toElasticAggregate(call)).put("field", field);
parent.set(naming.name(call), agg);
parent.set(naming.apply(call), agg);
}
}

Expand Down
Expand Up @@ -70,10 +70,10 @@ class AggregationQuery {
.map(AggregationQuery::extractPath).collect(Collectors.toList());

@SuppressWarnings("unchecked")
ExpressionNaming naming = ExpressionNaming.of(UniqueCachedNaming.of(paths.iterator()));
ExpressionNaming naming = ExpressionNaming.from(UniqueCachedNaming.of(paths.iterator()));
paths.forEach(p -> biMap.put(p, naming.name(p)));

this.projectionNaming = ExpressionNaming.of(UniqueCachedNaming.of(query.projections()));
this.projectionNaming = ExpressionNaming.from(UniqueCachedNaming.of(query.projections()));
this.naming = ImmutableBiMap.copyOf(biMap);
this.registry = MongoClientSettings.getDefaultCodecRegistry();
}
Expand Down
Expand Up @@ -126,7 +126,7 @@ private Publisher<?> query(StandardOperations.Select select) {
final boolean hasProjections = query.hasProjections();

boolean useAggregationPipeline = query.hasAggregations() || query.distinct();
ExpressionNaming expressionNaming = useAggregationPipeline ? ExpressionNaming.of(UniqueCachedNaming.of(query.projections())) : expression -> pathNaming.name((Path) expression);
ExpressionNaming expressionNaming = useAggregationPipeline ? ExpressionNaming.from(UniqueCachedNaming.of(query.projections())) : expression -> pathNaming.name((Path) expression);

MongoCollection<?> collection = this.collection;
if (hasProjections) {
Expand Down

0 comments on commit 764d3b5

Please sign in to comment.