Skip to content

Commit

Permalink
Remove now redundant prototype pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdinand-swoboda committed Nov 9, 2021
1 parent c75c77a commit 570d02d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 56 deletions.
16 changes: 6 additions & 10 deletions src/main/java/tech/picnic/jolo/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public final class Entity<T, R extends Record> {
private final Field<Long> primaryKey;
private final Class<T> type;

private Field<?>[] fields;
private final Field<?>[] fields;
@Nullable private Field<?>[] resultFields;

/**
Expand Down Expand Up @@ -92,17 +92,13 @@ public String toString() {
* line. The corresponding class has one constructor without, and one with an argument {@code int
* picked}. If it is mapped from a record that contains a (computed) {@code picked} column, then
* the constructor with the extra argument is used to bring it into Java land.
*
* @apiNote This method returns a new object.
*/
public Entity<T, R> withExtraFields(Field<?>... extraFields) {
fields =
concat(stream(fields), stream(extraFields).filter(Objects::nonNull))
.toArray(Field<?>[]::new);
return this;
}

/** Copies this entity, discarding any state. This method is used in a prototype pattern. */
public Entity<T, R> copy() {
return new Entity<>(table, type, primaryKey, fields);
Field<?>[] extendedFields = concat(stream(fields), stream(extraFields).filter(Objects::nonNull))
.toArray(Field<?>[]::new);
return new Entity<>(table, type, primaryKey, extendedFields);
}

/** The table that is mapped by this entity. */
Expand Down
23 changes: 4 additions & 19 deletions src/main/java/tech/picnic/jolo/LoaderFactoryBuilderImpl.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package tech.picnic.jolo;

import static java.util.function.Function.identity;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toSet;
import static tech.picnic.jolo.Util.getForeignKey;
import static tech.picnic.jolo.Util.getOptionalForeignKey;
import static tech.picnic.jolo.Util.validate;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jooq.Record;
import org.jooq.Table;
Expand All @@ -38,23 +33,14 @@ public LoaderFactory<T> build() {

/**
* Creates a new {@link Loader} with the entities and relations specified in this builder. The
* resulting loader can be used as a jOOQ record handler.
* resulting loader can be used as a {@link Record record} {@link java.util.stream.Collector
* collector}.
*
* @see Loader
*/
@Override
public Loader<T> newLoader() {
// We use a prototype pattern to create new entities / relations that keep state about the
// deserialisation process, by calling Entity#copy and Relation#copy.
Map<Entity<?, ?>, Entity<?, ?>> newEntities =
entities.stream().collect(toMap(identity(), Entity::copy));
@SuppressWarnings("unchecked")
Entity<T, ?> mainEntity = (Entity<T, ?>) newEntities.get(entity);
assert mainEntity != null : "Main entity was not copied";
return new Loader<>(
mainEntity,
entities.stream().map(newEntities::get).collect(toSet()),
relations.stream().map(r -> r.copy(newEntities)).collect(toList()));
return new Loader<>(entity, entities, relations);
}

/**
Expand Down Expand Up @@ -128,8 +114,7 @@ private static <L extends Record, R extends Record> TableField<?, Long> getForei
}

/**
* Used by {@link RelationBuilder} to return completed {@link Relation} prototypes to this
* builder.
* Used by {@link RelationBuilder} to return completed {@link Relation relations} to this builder.
*/
LoaderFactoryBuilderImpl<T> addRelation(Relation<?, ?> relation) {
relations.add(relation);
Expand Down
20 changes: 0 additions & 20 deletions src/main/java/tech/picnic/jolo/Relation.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
Expand Down Expand Up @@ -114,25 +113,6 @@ public String toString() {
rightKey.getName());
}

/** Copies this relation. This method is used in a prototype pattern. */
@SuppressWarnings("unchecked")
Relation<L, R> copy(Map<Entity<?, ?>, Entity<?, ?>> newEntities) {
Entity<L, ?> newLeft = (Entity<L, ?>) newEntities.get(left);
assert newLeft != null : "Attempt to create copy without new left entity";
Entity<R, ?> newRight = (Entity<R, ?>) newEntities.get(right);
assert newRight != null : "Attempt to create copy without new right entity";
return new Relation<>(
newLeft,
newRight,
leftKey,
rightKey,
leftArity,
rightArity,
leftSetter,
rightSetter,
relationLoaderIsCustom ? Optional.of(relationLoader) : Optional.empty());
}

@SuppressWarnings("NoFunctionalReturnType")
Function<Record, Set<IdPair>> getRelationLoader() {
return relationLoader;
Expand Down
7 changes: 0 additions & 7 deletions src/test/java/tech/picnic/jolo/EntityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,4 @@ public void testLoadWrongTable() {
Record record = createRecord(ImmutableMap.of(bar.ID, 1L, bar.FOO_, 1));
assertThrows(ValidationException.class, () -> aEntity.load(record));
}

@Test
public void testCopy() {
Entity<FooEntity, FooRecord> aEntity = new Entity<>(FOO, FooEntity.class);
aEntity.load(new FooRecord(1L, 1, new Long[0]));
assertEquals(aEntity, aEntity.copy());
}
}

0 comments on commit 570d02d

Please sign in to comment.