Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Pair and its factory method #9

Merged
merged 5 commits into from
Jan 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,22 @@
<url>https://travis-ci.org/PicnicSupermarket/jolo</url>
</ciManagement>

<properties>
<version.immutables>2.8.3</version.immutables>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.immutables</groupId>
<artifactId>value-annotations</artifactId>
<version>${version.immutables}</version>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
Expand All @@ -97,6 +106,11 @@
<artifactId>javax.annotation-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.immutables</groupId>
<artifactId>value-annotations</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jooq</groupId>
<artifactId>jooq</artifactId>
Expand All @@ -116,9 +130,39 @@
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<!-- The plugin's "excludes" property accepts an Ant
pattern, but does not match against the full path. So
rather than explicitly excluding generated sources, we
have to explicitly include "original" sources. -->
<sourceDirectories>
<sourceDirectory>${project.build.sourceDirectory}</sourceDirectory>
</sourceDirectories>
<testSourceDirectories>
<testSourceDirectory>${project.build.testSourceDirectory}</testSourceDirectory>
</testSourceDirectories>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.immutables</groupId>
<artifactId>value-processor</artifactId>
<version>${version.immutables}</version>
</path>
<path>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.2-jre</version>
</path>
</annotationProcessorPaths>
</configuration>
<executions>
<execution>
<id>default-testCompile</id>
Expand All @@ -127,7 +171,7 @@
<goal>testCompile</goal>
</goals>
<configuration>
<compilerArgs combine.children="append">
<compilerArgs>
<!-- XXX: We disable `html` checks to work
around a jOOQ codegen issue; file a PR for
that. -->
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/tech/picnic/jolo/IdPairInterface.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tech.picnic.jolo;

import org.immutables.value.Value;

/** A pair of related IDs. */
@Value.Immutable(builder = false)
@Value.Style(
allParameters = true,
typeAbstract = "*Interface",
typeImmutable = "*",
visibility = Value.Style.ImplementationVisibility.PUBLIC)
interface IdPairInterface {
/** The left-hand ID of the relation. */
long getLeftId();

/** The right-hand ID of the relation. */
long getRightId();
}
58 changes: 9 additions & 49 deletions src/main/java/tech/picnic/jolo/Relation.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiConsumer;
Expand All @@ -33,7 +32,7 @@ enum Arity {
MANY
}

private final Set<Pair> pairs = new LinkedHashSet<>();
private final Set<IdPair> pairs = new LinkedHashSet<>();
private final Entity<L, ?> left;
private final Entity<R, ?> right;
private final Field<Long> leftKey;
Expand All @@ -42,7 +41,7 @@ enum Arity {
private final Arity rightArity;
private final Optional<BiConsumer<L, ?>> leftSetter;
private final Optional<BiConsumer<R, ?>> rightSetter;
private final BiConsumer<Record, Set<Pair>> relationLoader;
private final BiConsumer<Record, Set<IdPair>> relationLoader;
private final boolean relationLoaderIsCustom;

@SuppressWarnings("ConstructorLeaksThis")
Expand All @@ -55,7 +54,7 @@ enum Arity {
Arity rightArity,
Optional<BiConsumer<L, ?>> leftSetter,
Optional<BiConsumer<R, ?>> rightSetter,
Optional<BiConsumer<Record, Set<Pair>>> relationLoader) {
Optional<BiConsumer<Record, Set<IdPair>>> relationLoader) {
this.left = left;
this.right = right;
this.leftKey = leftKey;
Expand Down Expand Up @@ -147,11 +146,11 @@ private static <T, U> void linkMany(
}

private Map<Long, List<L>> getPreSets() {
return pairs.stream().collect(toMultiset(Pair::getRightId, p -> left.get(p.getLeftId())));
return pairs.stream().collect(toMultiset(IdPair::getRightId, p -> left.get(p.getLeftId())));
}

private Map<Long, List<R>> getPostSets() {
return pairs.stream().collect(toMultiset(Pair::getLeftId, p -> right.get(p.getRightId())));
return pairs.stream().collect(toMultiset(IdPair::getLeftId, p -> right.get(p.getRightId())));
}

private static <T, K, V> Collector<T, ?, Map<K, List<V>>> toMultiset(
Expand All @@ -161,12 +160,12 @@ private Map<Long, List<R>> getPostSets() {

private Map<Long, L> getPredecessors() {
return pairs.stream()
.collect(toMap(Pair::getRightId, p -> left.get(p.getLeftId()), this::unexpectedPair));
.collect(toMap(IdPair::getRightId, p -> left.get(p.getLeftId()), this::unexpectedPair));
}

private Map<Long, R> getSuccessors() {
return pairs.stream()
.collect(toMap(Pair::getLeftId, p -> right.get(p.getRightId()), this::unexpectedPair));
.collect(toMap(IdPair::getLeftId, p -> right.get(p.getRightId()), this::unexpectedPair));
}

private <T> T unexpectedPair(T oldValue, T newValue) {
Expand All @@ -182,50 +181,11 @@ private <T> T unexpectedPair(T oldValue, T newValue) {
* key, or two foreign keys in case of a many-to-may relation), and if both can be found, the two
* values are considered to represent a pair that is part of the relation.
*/
private void foreignKeyRelationLoader(Record record, Set<Relation.Pair> sink) {
private void foreignKeyRelationLoader(Record record, Set<IdPair> sink) {
Long leftId = record.get(leftKey);
Long rightId = record.get(rightKey);
if (leftId != null && rightId != null) {
sink.add(Relation.Pair.of(leftId, rightId));
}
}

static final class Pair {
private final long leftId;
private final long rightId;

Pair(long leftId, long rightId) {
this.leftId = leftId;
this.rightId = rightId;
}

static Pair of(long leftId, long rightId) {
return new Pair(leftId, rightId);
}

long getLeftId() {
return leftId;
}

long getRightId() {
return rightId;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Pair pair = (Pair) o;
return leftId == pair.leftId && rightId == pair.rightId;
}

@Override
public int hashCode() {
return Objects.hash(leftId, rightId);
sink.add(IdPair.of(leftId, rightId));
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/tech/picnic/jolo/RelationBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public final class RelationBuilder<T, L, R> {
@Nullable private Arity rightArity;
@Nullable private BiConsumer<L, ?> leftSetter;
@Nullable private BiConsumer<R, ?> rightSetter;
private Optional<BiConsumer<Record, Set<Relation.Pair>>> relationLoader = Optional.empty();
private Optional<BiConsumer<Record, Set<IdPair>>> relationLoader = Optional.empty();

RelationBuilder(LoaderFactoryBuilderImpl<T> builder, Entity<L, ?> left, Entity<R, ?> right) {
this.builder = builder;
Expand Down Expand Up @@ -156,7 +156,7 @@ public RelationBuilder<T, L, R> setManyRight(BiConsumer<R, List<L>> setter) {

/** Specifies a function to programmatically identify relation pairs in loaded records. */
public RelationBuilder<T, L, R> setRelationLoader(
BiConsumer<Record, Set<Relation.Pair>> relationLoader) {
BiConsumer<Record, Set<IdPair>> relationLoader) {
this.relationLoader = Optional.of(relationLoader);
return this;
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/tech/picnic/jolo/LoaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public void testRelationWithDummyRelationLoader() {
.manyToMany(foo, bar, FOOBAR)
.setManyLeft(FooEntity::setBarList)
.setManyRight(BarEntity::setFooList)
.setRelationLoader((record, pairs) -> pairs.add(Relation.Pair.of(1, 1)))
.setRelationLoader((record, pairs) -> pairs.add(IdPair.of(1, 1)))
.build()
.newLoader();

Expand Down Expand Up @@ -434,7 +434,7 @@ public void testFallbackToForeignKeyRelation() {
assertEquals(fooEntitiesInLoader.get(1).getBarList(), ImmutableList.of());
}

private static void customRelationLoader(Record record, Set<Relation.Pair> pairs) {
private static void customRelationLoader(Record record, Set<IdPair> pairs) {
Long barId = record.get(BAR.ID);
if (barId == null) {
return;
Expand All @@ -443,6 +443,6 @@ private static void customRelationLoader(Record record, Set<Relation.Pair> pairs
if (fooIds == null) {
return;
}
Stream.of(fooIds).map(fooId -> Relation.Pair.of(fooId, barId)).forEach(pairs::add);
Stream.of(fooIds).map(fooId -> IdPair.of(fooId, barId)).forEach(pairs::add);
}
}