Skip to content

Commit

Permalink
Improvements and cleanup of fluent api implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
kwakeroni committed Nov 25, 2017
1 parent c2bc774 commit 4b3613b
Show file tree
Hide file tree
Showing 26 changed files with 413 additions and 309 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public default <ES extends BackendGroup<Q>> ES as(Class<? super ES> type) {
}

public String getName();
public ParameterGroupDefinition getDefinition();

public ParameterGroupDefinition<?> getDefinition();

public BackendQuery<? extends Q, ?> internalize(Object query, BackendWireFormatterContext context);

Expand Down
5 changes: 5 additions & 0 deletions parameters-basic/parameters-basic-common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
<groupId>be.kwakeroni.parameters.definition</groupId>
<artifactId>parameters-definition-api</artifactId>
</dependency>
<dependency>
<groupId>be.kwakeroni.parameters.definition</groupId>
<artifactId>parameters-definition-ext-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>be.kwakeroni.parameters.types</groupId>
<artifactId>parameters-types-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
public interface BasicGroup {

public default SimpleDefinitionBuilder simple() {
return DefaultSimpleDefinition.builder();
return new DefaultSimpleDefinitionBuilder();
}

public default MappedDefinitionBuilder<?, ?> mapped() {
return DefaultMappedDefinition.builder();
return new DefaultMappedDefinitionBuilder<>();
}

public default RangedDefinitionBuilder<?, ?> ranged() {
return DefaultRangedDefinition.builder();
return new DefaultRangedDefinitionBuilder<>();
}

public static BasicGroup builder() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
package be.kwakeroni.parameters.basic.definition;

import be.kwakeroni.parameters.basic.client.model.Mapped;
import be.kwakeroni.parameters.basic.definition.builder.MappedDefinitionBuilder;
import be.kwakeroni.parameters.basic.definition.factory.MappedDefinitionVisitor;
import be.kwakeroni.parameters.client.api.BusinessParameters;
import be.kwakeroni.parameters.client.api.model.EntryType;
import be.kwakeroni.parameters.definition.api.DefinitionVisitorContext;
import be.kwakeroni.parameters.definition.api.ParameterGroupDefinition;
import be.kwakeroni.parameters.definition.api.builder.DefinitionBuilder;
import be.kwakeroni.parameters.definition.api.builder.DefinitionBuilderFinalizer;
import be.kwakeroni.parameters.definition.ext.PartialDefinition;
import be.kwakeroni.parameters.definition.ext.PartialGroup;
import be.kwakeroni.parameters.types.api.ParameterType;

import java.util.Collection;
import java.util.function.Function;

/**
* Created by kwakeroni on 11.04.17.
*/
final class DefaultMappedDefinition<KeyType, SubType extends EntryType> implements MappedDefinitionVisitor.Definition, ParameterGroupDefinition<Mapped<KeyType, SubType>> {
final class DefaultMappedDefinition<GroupType extends EntryType, KeyType, SubType extends EntryType> implements MappedDefinitionVisitor.Definition, PartialDefinition<GroupType, Mapped<KeyType, SubType>> {

private String keyParameter;
private ParameterType<KeyType> keyType;
private ParameterGroupDefinition<SubType> subGroupDefinition;

private DefaultMappedDefinition() {
private final String keyParameter;
private final ParameterType<KeyType> keyType;
private final PartialDefinition<GroupType, SubType> subGroupDefinition;

DefaultMappedDefinition(String keyParameter, ParameterType<KeyType> keyType, PartialDefinition<GroupType, SubType> subGroupDefinition) {
this.keyParameter = keyParameter;
this.keyType = keyType;
this.subGroupDefinition = subGroupDefinition;
}

@Override
Expand Down Expand Up @@ -53,9 +53,14 @@ public ParameterGroupDefinition<Mapped<KeyType, SubType>> getDefinition() {
}

@Override
public Partial<Mapped<KeyType, SubType>> createPartial(BusinessParameters businessParameters) {
Partial<SubType> subDefinition = subGroupDefinition.createPartial(businessParameters);
return partialQuery -> new DefaultMappedGroup<>(subDefinition, keyType, partialQuery);
public Mapped<KeyType, SubType> createGroup(BusinessParameters businessParameters) {
return createPartial(businessParameters).resolve();
}

@Override
public PartialGroup<GroupType, Mapped<KeyType, SubType>> createPartial(BusinessParameters businessParameters) {
PartialGroup<GroupType, SubType> subGroup = subGroupDefinition.createPartial(businessParameters);
return new DefaultMappedGroup<>(keyType, subGroup);
}

@Override
Expand All @@ -64,34 +69,4 @@ public <G> G apply(DefinitionVisitorContext<G> context) {
return MappedDefinitionVisitor.from(context).visit(this, subGroup);
}

static DefaultMappedDefinition<?, ?>.Builder builder() {
return new DefaultMappedDefinition<>().new Builder();
}

private final class Builder implements MappedDefinitionBuilder<KeyType, SubType> {
private DefinitionBuilder<SubType> subGroup;

@Override
public <NewKeyType> MappedDefinitionBuilder<NewKeyType, SubType> withKeyParameter(String name, ParameterType<NewKeyType> type) {
keyParameter = name;
keyType = (ParameterType<KeyType>) type;
return (MappedDefinitionBuilder<NewKeyType, SubType>) this;
}

@Override
public <NewSubType extends EntryType> MappedDefinitionBuilder<KeyType, NewSubType> mappingTo(DefinitionBuilder<NewSubType> subGroup) {
this.subGroup = (DefinitionBuilder<SubType>) subGroup;
return (MappedDefinitionBuilder<KeyType, NewSubType>) this;
}

@Override
public ParameterGroupDefinition<Mapped<KeyType, SubType>> build(String name, Function<DefinitionBuilderFinalizer, DefinitionBuilderFinalizer> theirFinalizer) {
subGroupDefinition = subGroup.build(name, myFinalizer().andThen(theirFinalizer));
return DefaultMappedDefinition.this;
}

private Function<DefinitionBuilderFinalizer, DefinitionBuilderFinalizer> myFinalizer() {
return builder -> builder.prependParameter(keyParameter);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package be.kwakeroni.parameters.basic.definition;

import be.kwakeroni.parameters.basic.client.model.Mapped;
import be.kwakeroni.parameters.basic.definition.builder.MappedDefinitionBuilder;
import be.kwakeroni.parameters.client.api.model.EntryType;
import be.kwakeroni.parameters.definition.ext.DefinitionBuilder;
import be.kwakeroni.parameters.definition.ext.DefinitionBuilderFinalizer;
import be.kwakeroni.parameters.definition.ext.PartialDefinition;
import be.kwakeroni.parameters.types.api.ParameterType;

import java.util.function.Function;

final /* value */ class DefaultMappedDefinitionBuilder<KeyType, SubType extends EntryType> implements MappedDefinitionBuilder<KeyType, SubType> {
private String keyParameter;
private ParameterType<KeyType> keyType;
private DefinitionBuilder<SubType> subGroup;

private <NewKeyType, NewSubType extends EntryType>
DefaultMappedDefinitionBuilder<NewKeyType, NewSubType> with(ParameterType<NewKeyType> key, DefinitionBuilder<NewSubType> subGroup) {
DefaultMappedDefinitionBuilder<NewKeyType, NewSubType> self = (DefaultMappedDefinitionBuilder<NewKeyType, NewSubType>) this;
self.keyType = key;
self.subGroup = subGroup;
return self;
}

@Override
public <NewKeyType> MappedDefinitionBuilder<NewKeyType, SubType> withKeyParameter(String keyParameter, ParameterType<NewKeyType> keyType) {
this.keyParameter = keyParameter;
return with(keyType, subGroup);
}

@Override
public <NewSubType extends EntryType> MappedDefinitionBuilder<KeyType, NewSubType> mappingTo(DefinitionBuilder<NewSubType> subGroup) {
return with(keyType, subGroup);
}

@Override
public PartialDefinition<?, Mapped<KeyType, SubType>> createPartialDefinition(String name, Function<DefinitionBuilderFinalizer, DefinitionBuilderFinalizer> theirFinalizer) {
PartialDefinition<?, SubType> subGroupDefinition = subGroup.createPartialDefinition(name, myFinalizer().andThen(theirFinalizer));
return new DefaultMappedDefinition<>(keyParameter, keyType, subGroupDefinition);
}

private Function<DefinitionBuilderFinalizer, DefinitionBuilderFinalizer> myFinalizer() {
return builder -> builder.prependParameter(keyParameter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,39 @@
import be.kwakeroni.parameters.basic.client.query.MappedQuery;
import be.kwakeroni.parameters.client.api.model.EntryType;
import be.kwakeroni.parameters.client.api.query.PartialQuery;
import be.kwakeroni.parameters.definition.api.ParameterGroupDefinition;
import be.kwakeroni.parameters.definition.ext.PartialGroup;
import be.kwakeroni.parameters.types.api.ParameterType;

/**
* Created by kwakeroni on 20/11/17.
*/
final class DefaultMappedGroup<KeyType, SubType extends EntryType> implements Mapped<KeyType, SubType> {
final class DefaultMappedGroup<GroupType extends EntryType, KeyType, SubType extends EntryType> implements PartialGroup<GroupType, Mapped<KeyType, SubType>> {

private final ParameterGroupDefinition.Partial<SubType> subDefinition;
private final ParameterType<KeyType> keyType;
private final PartialQuery<?, Mapped<KeyType, SubType>> partial;
private final PartialGroup<GroupType, SubType> subGroupDefinition;

public DefaultMappedGroup(ParameterGroupDefinition.Partial<SubType> subDefinition, ParameterType<KeyType> keyType, PartialQuery<?, Mapped<KeyType, SubType>> partial) {
this.subDefinition = subDefinition;
DefaultMappedGroup(ParameterType<KeyType> keyType, PartialGroup<GroupType, SubType> subGroupDefinition) {
this.keyType = keyType;
this.partial = partial;
this.subGroupDefinition = subGroupDefinition;
}

@Override
public SubType forKey(KeyType key) {
PartialQuery<Mapped<KeyType, SubType>, SubType> mappedQuery = new MappedQuery.Partial<>(key, keyType);
return createSubType(partial.andThen(mappedQuery));
public Mapped<KeyType, SubType> resolve(PartialQuery<GroupType, Mapped<KeyType, SubType>> parentQuery) {
return new Resolved(parentQuery);
}

private SubType createSubType(PartialQuery<?, SubType> partialQuery) {
return subDefinition.createGroup(partialQuery);
}
private final /* value */ class Resolved implements Mapped<KeyType, SubType> {
private final PartialQuery<GroupType, Mapped<KeyType, SubType>> parentQuery;

Resolved(PartialQuery<GroupType, Mapped<KeyType, SubType>> parentQuery) {
this.parentQuery = parentQuery;
}

@Override
public SubType forKey(KeyType key) {
return resolveSubGroup(new MappedQuery.Partial<>(key, keyType));
}

private SubType resolveSubGroup(PartialQuery<Mapped<KeyType, SubType>, SubType> myQueryPart) {
PartialQuery<GroupType, SubType> myQuery = parentQuery.andThen(myQueryPart);
return subGroupDefinition.resolve(myQuery);
}
}
}
Loading

0 comments on commit 4b3613b

Please sign in to comment.