From 695258c1a90a6ad30f5f323fc1a715ef431a0294 Mon Sep 17 00:00:00 2001 From: Effi Ban Date: Mon, 5 Feb 2024 12:32:33 +0200 Subject: [PATCH] DRAFT - Support comparable entity fields and DSL --- .../kenshoo/pl/entity/AbstractEntityType.java | 92 +++++++---- .../com/kenshoo/pl/entity/EntityField.java | 19 +++ .../kenshoo/pl/entity/MutableEntityField.java | 8 + .../entity/internal/AbstractEntityField.java | 148 ++++++++++++++++++ .../pl/entity/internal/EntityFieldImpl.java | 73 ++++----- .../internal/PrototypedEntityFieldImpl.java | 61 +++++++- .../SecondaryTableRelationExtractor.java | 8 +- .../internal/VirtualEntityFieldImpl.java | 45 +++++- .../com/kenshoo/pl/entity/TestEntity.java | 10 ++ .../kenshoo/pl/entity/TestEntityTable.java | 3 + 10 files changed, 388 insertions(+), 79 deletions(-) create mode 100644 main/src/main/java/com/kenshoo/pl/entity/MutableEntityField.java create mode 100644 main/src/main/java/com/kenshoo/pl/entity/internal/AbstractEntityField.java diff --git a/main/src/main/java/com/kenshoo/pl/entity/AbstractEntityType.java b/main/src/main/java/com/kenshoo/pl/entity/AbstractEntityType.java index da02f0bc..1747e7e0 100644 --- a/main/src/main/java/com/kenshoo/pl/entity/AbstractEntityType.java +++ b/main/src/main/java/com/kenshoo/pl/entity/AbstractEntityType.java @@ -38,55 +38,87 @@ protected AbstractEntityType(String name) { this.name = name; } - protected EntityField field(TableField tableField) { + protected MutableEntityField field(final TableField tableField) { return field(tableField, IdentityValueConverter.getInstance(tableField.getType())); } - protected EntityField field(TableField tableField, ValueConverter valueConverter) { + protected MutableEntityField field(final TableField tableField, final ValueConverter valueConverter) { return field(tableField, valueConverter, Objects::equals); } - protected EntityField field(TableField tableField, ValueConverter valueConverter, ValueConverter stringValueConverter) { + protected MutableEntityField field(final TableField tableField, + final ValueConverter valueConverter, + final ValueConverter stringValueConverter) { return field(tableField, valueConverter, stringValueConverter, Objects::equals); } - protected EntityField field(TableField tableField, EntityValueEqualityFunction valueEqualityFunction) { - return field(tableField, IdentityValueConverter.getInstance(tableField.getType()), createStringValueConverter(tableField.getType()), valueEqualityFunction); + protected MutableEntityField field(final TableField tableField, + final EntityValueEqualityFunction valueEqualityFunction) { + return field(tableField, + IdentityValueConverter.getInstance(tableField.getType()), + createStringValueConverter(tableField.getType()), + valueEqualityFunction); } - protected EntityField field(TableField tableField, ValueConverter valueConverter, EntityValueEqualityFunction valueEqualityFunction) { + protected MutableEntityField field(final TableField tableField, + final ValueConverter valueConverter, + final EntityValueEqualityFunction valueEqualityFunction) { return field(tableField, valueConverter, createStringValueConverter(valueConverter.getValueClass()), valueEqualityFunction); } - protected EntityField field(TableField tableField, ValueConverter valueConverter, - ValueConverter stringValueConverter, EntityValueEqualityFunction valueEqualityFunction) { - return addField(new EntityFieldImpl<>(this, new SimpleEntityFieldDbAdapter<>(tableField, valueConverter), stringValueConverter, valueEqualityFunction)); - } - - protected EntityField field(EntityFieldDbAdapter dbAdapter, ValueConverter stringValueConverter) { - return addField(new EntityFieldImpl<>(this, dbAdapter, stringValueConverter, Objects::equals)); - } - - protected EntityField virtualField(EntityField field1, Function translator, - ValueConverter stringValueConverter, EntityValueEqualityFunction valueEqualityFunction) { + protected MutableEntityField field(final TableField tableField, + final ValueConverter valueConverter, + final ValueConverter stringValueConverter, + final EntityValueEqualityFunction valueEqualityFunction) { + return addField( + EntityFieldImpl.builder(this) + .withDbAdapter(new SimpleEntityFieldDbAdapter<>(tableField, valueConverter)) + .withStringValueConverter(stringValueConverter) + .withValueEqualityFunction(valueEqualityFunction) + .build() + ); + } + + protected MutableEntityField field(EntityFieldDbAdapter dbAdapter, ValueConverter stringValueConverter) { + return addField( + EntityFieldImpl.builder(this) + .withDbAdapter(dbAdapter) + .withStringValueConverter(stringValueConverter) + .withValueEqualityFunction(Objects::equals) + .build() + ); + } + + protected MutableEntityField virtualField(final EntityField field1, + final Function translator, + final ValueConverter stringValueConverter, + final EntityValueEqualityFunction valueEqualityFunction) { return virtualField(new VirtualEntityFieldDbAdapter<>(field1.getDbAdapter(), translator), stringValueConverter, valueEqualityFunction); } - protected EntityField virtualField(DataTable table, - ValueConverter stringValueConverter, - EntityValueEqualityFunction valueEqualityFunction) { + protected MutableEntityField virtualField(final DataTable table, + final ValueConverter stringValueConverter, + final EntityValueEqualityFunction valueEqualityFunction) { return virtualField(new EmptyVirtualEntityFieldDbAdapter<>(table), stringValueConverter, valueEqualityFunction); } - protected EntityField virtualField(EntityField field1, EntityField field2, BiFunction combiner, - ValueConverter stringValueConverter, EntityValueEqualityFunction valueEqualityFunction) { + protected MutableEntityField virtualField(final EntityField field1, + final EntityField field2, + final BiFunction combiner, + final ValueConverter stringValueConverter, + final EntityValueEqualityFunction valueEqualityFunction) { return virtualField(new VirtualEntityFieldDbAdapter2<>(field1.getDbAdapter(), field2.getDbAdapter(), combiner), stringValueConverter, valueEqualityFunction); } - private EntityField virtualField(EntityFieldDbAdapter entityFieldDbAdapter, - ValueConverter stringValueConverter, - EntityValueEqualityFunction valueEqualityFunction) { - return addField(new VirtualEntityFieldImpl<>(this, entityFieldDbAdapter, stringValueConverter, valueEqualityFunction)); + private MutableEntityField virtualField(final EntityFieldDbAdapter entityFieldDbAdapter, + final ValueConverter stringValueConverter, + final EntityValueEqualityFunction valueEqualityFunction) { + return addField( + VirtualEntityFieldImpl.builder(this) + .withDbAdapter(entityFieldDbAdapter) + .withStringValueConverter(stringValueConverter) + .withValueEqualityFunction(valueEqualityFunction) + .build()); } private static ValueConverter createStringValueConverter(Class valueClass) { @@ -113,8 +145,12 @@ protected PrototypedEntityField prototypedField(EntityFieldProtot } protected PrototypedEntityField prototypedField(EntityFieldPrototype entityFieldPrototype, TableField tableField, ValueConverter valueConverter, EntityValueEqualityFunction valueEqualityFunction) { - PrototypedEntityFieldImpl field = new PrototypedEntityFieldImpl<>(this, entityFieldPrototype, new SimpleEntityFieldDbAdapter<>(tableField, valueConverter), - createStringValueConverter(valueConverter.getValueClass()), valueEqualityFunction); + final var field = PrototypedEntityFieldImpl.builder(this) + .withDbAdapter(new SimpleEntityFieldDbAdapter<>(tableField, valueConverter)) + .withStringValueConverter(createStringValueConverter(valueConverter.getValueClass())) + .withValueEqualityFunction(valueEqualityFunction) + .withEntityFieldPrototype(entityFieldPrototype) + .build(); prototypedFields.add(field); return addField(field); } diff --git a/main/src/main/java/com/kenshoo/pl/entity/EntityField.java b/main/src/main/java/com/kenshoo/pl/entity/EntityField.java index 78d62956..720a0887 100644 --- a/main/src/main/java/com/kenshoo/pl/entity/EntityField.java +++ b/main/src/main/java/com/kenshoo/pl/entity/EntityField.java @@ -6,6 +6,7 @@ import java.util.Arrays; import java.util.Objects; +import java.util.function.Predicate; import java.util.stream.Collectors; public interface EntityField, T> { @@ -16,6 +17,8 @@ public interface EntityField, T> { boolean valuesEqual(T v1, T v2); + int compareValues(T v1, T v2); + default Class getValueClass() { return getStringValueConverter().getValueClass(); } @@ -78,4 +81,20 @@ default PLCondition isNotNull() { final TableField tableField = (TableField)getDbAdapter().getFirstTableField(); return new PLCondition(tableField.isNotNull(), entity -> entity.safeGet(this).isNotNull(), this); } + + default PLCondition greaterThan(T value) { + if (isVirtual()) { + throw new UnsupportedOperationException("The greaterThan operation is unsupported for virtual fields"); + } + + final Object tableValue = getDbAdapter().getFirstDbValue(value); + @SuppressWarnings("unchecked") final var tableField = (TableField) getDbAdapter().getFirstTableField(); + return new PLCondition(tableField.greaterThan(tableValue), entityFieldGreaterThan(value), this); + } + + private Predicate entityFieldGreaterThan(T value) { + return entity -> entity.safeGet(this) + .filter(entityValue -> compareValues(entityValue, value) > 0) + .isPresent(); + } } diff --git a/main/src/main/java/com/kenshoo/pl/entity/MutableEntityField.java b/main/src/main/java/com/kenshoo/pl/entity/MutableEntityField.java new file mode 100644 index 00000000..cefbb4dd --- /dev/null +++ b/main/src/main/java/com/kenshoo/pl/entity/MutableEntityField.java @@ -0,0 +1,8 @@ +package com.kenshoo.pl.entity; + +import java.util.Comparator; + +public interface MutableEntityField, T> extends EntityField { + + EntityField comparedBy(final Comparator valueComparator); +} diff --git a/main/src/main/java/com/kenshoo/pl/entity/internal/AbstractEntityField.java b/main/src/main/java/com/kenshoo/pl/entity/internal/AbstractEntityField.java new file mode 100644 index 00000000..a2a7cd10 --- /dev/null +++ b/main/src/main/java/com/kenshoo/pl/entity/internal/AbstractEntityField.java @@ -0,0 +1,148 @@ +package com.kenshoo.pl.entity.internal; + +import com.kenshoo.pl.entity.*; +import com.kenshoo.pl.entity.equalityfunctions.EntityValueEqualityFunction; + +import java.util.Comparator; +import java.util.Optional; + +import static java.util.Objects.requireNonNull; + +abstract class AbstractEntityField< + E extends EntityType, + T, + F extends EntityField, + B extends AbstractEntityField.Builder> implements MutableEntityField { + + protected final EntityType entityType; + + protected final EntityFieldDbAdapter dbAdapter; + + protected final EntityValueEqualityFunction valueEqualityFunction; + + protected final Comparator valueComparator; + + protected final ValueConverter stringValueConverter; + + protected AbstractEntityField(final EntityType entityType, + final EntityFieldDbAdapter dbAdapter, + final ValueConverter stringValueConverter, + final EntityValueEqualityFunction valueEqualityFunction, + final Comparator valueComparator) { + this.entityType = entityType; + this.dbAdapter = dbAdapter; + this.stringValueConverter = stringValueConverter; + this.valueEqualityFunction = valueEqualityFunction; + this.valueComparator = valueComparator; + } + + @Override + public EntityFieldDbAdapter getDbAdapter() { + return dbAdapter; + } + + @Override + public ValueConverter getStringValueConverter() { + return stringValueConverter; + } + + @Override + public boolean valuesEqual(T v1, T v2) { + return valueEqualityFunction.apply(v1, v2); + } + + @Override + public int compareValues(final T v1, final T v2) { + return comparatorOf(v1).compare(v1, v2); + } + + @Override + public EntityType getEntityType() { + return entityType; + } + + protected B toBuilder() { + return newBuilder(entityType) + .withDbAdapter(dbAdapter) + .withValueEqualityFunction(valueEqualityFunction) + .withValueComparator(valueComparator) + .withStringValueConverter(stringValueConverter); + } + + protected abstract B newBuilder(final EntityType entityType); + + @Override + public EntityField comparedBy(final Comparator valueComparator) { + requireNonNull(valueComparator); + return toBuilder() + .withValueComparator(valueComparator) + .withValueEqualityFunction(equalityFunctionOf(valueComparator)) + .build(); + } + + @Override + public String toString() { + return entityType.toFieldName(this); + } + + private Comparator comparatorOf(T value) { + return Optional.ofNullable(valueComparator) + .orElseGet(() -> { + if (value instanceof Comparable) { + //noinspection unchecked + return (Comparator) Comparator.naturalOrder(); + } + throw new UnsupportedOperationException(String.format("The field [%s] is not comparable for the value [%s]", this, value)); + }); + } + + + private EntityValueEqualityFunction equalityFunctionOf(final Comparator valueComparator) { + return (v1, v2) -> valueComparator.compare(v1, v2) == 0; + } + + protected abstract static class Builder< + E extends EntityType, + T, + F extends EntityField, + B extends Builder> { + + protected final EntityType entityType; + + protected EntityFieldDbAdapter dbAdapter; + + protected EntityValueEqualityFunction valueEqualityFunction; + + protected Comparator valueComparator; + + protected ValueConverter stringValueConverter; + + protected Builder(final EntityType entityType) { + this.entityType = entityType; + } + + public B withDbAdapter(final EntityFieldDbAdapter dbAdapter) { + this.dbAdapter = dbAdapter; + return self(); + } + + public B withValueEqualityFunction(final EntityValueEqualityFunction valueEqualityFunction) { + this.valueEqualityFunction = valueEqualityFunction; + return self(); + } + + public B withValueComparator(final Comparator valueComparator) { + this.valueComparator = valueComparator; + return self(); + } + + public B withStringValueConverter(final ValueConverter stringValueConverter) { + this.stringValueConverter = stringValueConverter; + return self(); + } + + protected abstract B self(); + + public abstract F build(); + } +} diff --git a/main/src/main/java/com/kenshoo/pl/entity/internal/EntityFieldImpl.java b/main/src/main/java/com/kenshoo/pl/entity/internal/EntityFieldImpl.java index e9af9ce5..4f0cce2d 100644 --- a/main/src/main/java/com/kenshoo/pl/entity/internal/EntityFieldImpl.java +++ b/main/src/main/java/com/kenshoo/pl/entity/internal/EntityFieldImpl.java @@ -1,61 +1,48 @@ package com.kenshoo.pl.entity.internal; -import com.kenshoo.pl.entity.EntityField; -import com.kenshoo.pl.entity.EntityFieldDbAdapter; -import com.kenshoo.pl.entity.EntityType; -import com.kenshoo.pl.entity.ValueConverter; +import com.kenshoo.pl.entity.*; import com.kenshoo.pl.entity.equalityfunctions.EntityValueEqualityFunction; -public class EntityFieldImpl, T> implements EntityField { +import java.util.Comparator; - private final EntityType entityType; +public class EntityFieldImpl, T> + extends AbstractEntityField, EntityFieldImpl.Builder> { - private final EntityFieldDbAdapter dbAdapter; - - private final EntityValueEqualityFunction valueEqualityFunction; - - private final ValueConverter stringValueConverter; - - public EntityFieldImpl(EntityType entityType, EntityFieldDbAdapter dbAdapter, ValueConverter stringValueConverter, EntityValueEqualityFunction valueEqualityFunction) { - this.entityType = entityType; - this.dbAdapter = dbAdapter; - this.stringValueConverter = stringValueConverter; - this.valueEqualityFunction = valueEqualityFunction; + protected EntityFieldImpl(final EntityType entityType, + final EntityFieldDbAdapter dbAdapter, + final ValueConverter stringValueConverter, + final EntityValueEqualityFunction valueEqualityFunction, + final Comparator valueComparator) { + super(entityType, dbAdapter, stringValueConverter, valueEqualityFunction, valueComparator); } @Override - public EntityFieldDbAdapter getDbAdapter() { - return dbAdapter; + protected Builder newBuilder(final EntityType entityType) { + return new Builder<>(entityType); } - @Override - public ValueConverter getStringValueConverter() { - return stringValueConverter; - } - - @Override - public boolean valuesEqual(T v1, T v2) { - return valueEqualityFunction.apply(v1, v2); + public static , T> Builder builder(final EntityType entityType) { + return new Builder<>(entityType); } - @Override - public EntityType getEntityType() { - return entityType; - } + public static class Builder, T> + extends AbstractEntityField.Builder, Builder> { - @Override - public String toString() { - return entityType.toFieldName(this); - } + public Builder(final EntityType entityType) { + super(entityType); + } - @Override - @SuppressWarnings("EqualsWhichDoesntCheckParameterClass") - public boolean equals(Object o) { - return (this == o); - } + protected Builder self() { + return this; + } - @Override - public int hashCode() { - return System.identityHashCode(this); + public EntityFieldImpl build() { + return new EntityFieldImpl<>( + entityType, + dbAdapter, + stringValueConverter, + valueEqualityFunction, + valueComparator); + } } } diff --git a/main/src/main/java/com/kenshoo/pl/entity/internal/PrototypedEntityFieldImpl.java b/main/src/main/java/com/kenshoo/pl/entity/internal/PrototypedEntityFieldImpl.java index 42c78ffd..7da4d933 100644 --- a/main/src/main/java/com/kenshoo/pl/entity/internal/PrototypedEntityFieldImpl.java +++ b/main/src/main/java/com/kenshoo/pl/entity/internal/PrototypedEntityFieldImpl.java @@ -7,13 +7,21 @@ import com.kenshoo.pl.entity.ValueConverter; import com.kenshoo.pl.entity.equalityfunctions.EntityValueEqualityFunction; -public class PrototypedEntityFieldImpl, T> extends EntityFieldImpl implements PrototypedEntityField { +import java.util.Comparator; + +public class PrototypedEntityFieldImpl, T> + extends AbstractEntityField, PrototypedEntityFieldImpl.Builder> + implements PrototypedEntityField { private final EntityFieldPrototype entityFieldPrototype; - public PrototypedEntityFieldImpl(EntityType entityType, EntityFieldPrototype entityFieldPrototype, EntityFieldDbAdapter dbAdapter, - ValueConverter stringValueConverter, EntityValueEqualityFunction valueEqualityFunction) { - super(entityType, dbAdapter, stringValueConverter, valueEqualityFunction); + private PrototypedEntityFieldImpl(final EntityType entityType, + final EntityFieldPrototype entityFieldPrototype, + final EntityFieldDbAdapter dbAdapter, + final ValueConverter stringValueConverter, + final EntityValueEqualityFunction valueEqualityFunction, + final Comparator valueComparator) { + super(entityType, dbAdapter, stringValueConverter, valueEqualityFunction, valueComparator); this.entityFieldPrototype = entityFieldPrototype; } @@ -21,4 +29,49 @@ public PrototypedEntityFieldImpl(EntityType entityType, EntityFieldPrototype< public EntityFieldPrototype getEntityFieldPrototype() { return entityFieldPrototype; } + + @Override + protected Builder toBuilder() { + return super.toBuilder() + .withEntityFieldPrototype(entityFieldPrototype); + } + + @Override + protected Builder newBuilder(final EntityType entityType) { + return new PrototypedEntityFieldImpl.Builder<>(entityType); + } + + public static , T> Builder builder(final EntityType entityType) { + return new Builder<>(entityType); + } + + public static class Builder, T> + extends AbstractEntityField.Builder, PrototypedEntityFieldImpl.Builder> { + + private EntityFieldPrototype entityFieldPrototype; + + public Builder(final EntityType entityType) { + super(entityType); + } + + protected PrototypedEntityFieldImpl.Builder self() { + return this; + } + + public Builder withEntityFieldPrototype(final EntityFieldPrototype entityFieldPrototype) { + this.entityFieldPrototype = entityFieldPrototype; + return this; + } + + public PrototypedEntityFieldImpl build() { + return new PrototypedEntityFieldImpl<>( + entityType, + entityFieldPrototype, + dbAdapter, + stringValueConverter, + valueEqualityFunction, + valueComparator + ); + } + } } diff --git a/main/src/main/java/com/kenshoo/pl/entity/internal/SecondaryTableRelationExtractor.java b/main/src/main/java/com/kenshoo/pl/entity/internal/SecondaryTableRelationExtractor.java index d94c08f9..8c433c56 100644 --- a/main/src/main/java/com/kenshoo/pl/entity/internal/SecondaryTableRelationExtractor.java +++ b/main/src/main/java/com/kenshoo/pl/entity/internal/SecondaryTableRelationExtractor.java @@ -2,7 +2,9 @@ import com.kenshoo.jooq.DataTable; import com.kenshoo.pl.entity.EntityField; +import com.kenshoo.pl.entity.EntityFieldDbAdapter; import com.kenshoo.pl.entity.EntityType; +import com.kenshoo.pl.entity.ValueConverter; import com.kenshoo.pl.entity.converters.IdentityValueConverter; import org.jooq.Record; import org.jooq.TableField; @@ -54,7 +56,11 @@ class SecondaryTableRelationExtractor { private static > EntityField createTemporaryEntityField(E entityType, TableField secondaryField, TableField primaryTableField) { var primaryField = entityType.findField(primaryTableField).get(); var converter = IdentityValueConverter.getInstance(secondaryField.getType()); - return new EntityFieldImpl(entityType, new SimpleEntityFieldDbAdapter<>(secondaryField, converter), primaryField.getStringValueConverter(), Objects::equals); + return EntityFieldImpl.builder(entityType) + .withDbAdapter((EntityFieldDbAdapter) new SimpleEntityFieldDbAdapter<>(secondaryField, converter)) + .withStringValueConverter((ValueConverter) primaryField.getStringValueConverter()) + .withValueEqualityFunction(Objects::equals) + .build(); } static > Stream> relationUsingTableFieldsOfPrimary(DataTable secondaryTable, E entityType) { diff --git a/main/src/main/java/com/kenshoo/pl/entity/internal/VirtualEntityFieldImpl.java b/main/src/main/java/com/kenshoo/pl/entity/internal/VirtualEntityFieldImpl.java index 9f8f009b..d794f525 100644 --- a/main/src/main/java/com/kenshoo/pl/entity/internal/VirtualEntityFieldImpl.java +++ b/main/src/main/java/com/kenshoo/pl/entity/internal/VirtualEntityFieldImpl.java @@ -5,13 +5,52 @@ import com.kenshoo.pl.entity.ValueConverter; import com.kenshoo.pl.entity.equalityfunctions.EntityValueEqualityFunction; -public class VirtualEntityFieldImpl, T> extends EntityFieldImpl { - public VirtualEntityFieldImpl(EntityType entityType, EntityFieldDbAdapter dbAdapter, ValueConverter stringValueConverter, EntityValueEqualityFunction valueEqualityFunction) { - super(entityType, dbAdapter, stringValueConverter, valueEqualityFunction); +import java.util.Comparator; + +public class VirtualEntityFieldImpl, T> extends + AbstractEntityField, VirtualEntityFieldImpl.Builder> { + + private VirtualEntityFieldImpl(final EntityType entityType, + final EntityFieldDbAdapter dbAdapter, + final ValueConverter stringValueConverter, + final EntityValueEqualityFunction valueEqualityFunction, + final Comparator valueComparator) { + super(entityType, dbAdapter, stringValueConverter, valueEqualityFunction, valueComparator); } + @Override public boolean isVirtual() { return true; } + + @Override + protected Builder newBuilder(final EntityType entityType) { + return new Builder<>(entityType); + } + + public static , T> Builder builder(final EntityType entityType) { + return new Builder<>(entityType); + } + + public static class Builder, T> + extends AbstractEntityField.Builder, Builder> { + + public Builder(final EntityType entityType) { + super(entityType); + } + + protected Builder self() { + return this; + } + + public VirtualEntityFieldImpl build() { + return new VirtualEntityFieldImpl<>( + entityType, + dbAdapter, + stringValueConverter, + valueEqualityFunction, + valueComparator); + } + } } diff --git a/main/src/test/java/com/kenshoo/pl/entity/TestEntity.java b/main/src/test/java/com/kenshoo/pl/entity/TestEntity.java index 5c695695..8c2e615e 100644 --- a/main/src/test/java/com/kenshoo/pl/entity/TestEntity.java +++ b/main/src/test/java/com/kenshoo/pl/entity/TestEntity.java @@ -3,6 +3,13 @@ import com.kenshoo.jooq.DataTable; import com.kenshoo.pl.entity.annotation.Id; +import java.math.BigDecimal; +import java.math.RoundingMode; +import java.util.Comparator; + +import static java.math.RoundingMode.DOWN; +import static java.util.Comparator.comparing; + public class TestEntity extends AbstractEntityType { public static final TestEntity INSTANCE = new TestEntity(); @@ -14,6 +21,9 @@ public class TestEntity extends AbstractEntityType { public static final PrototypedEntityField FIELD_3 = INSTANCE.prototypedField(TestDataFieldPrototype.FIELD_3, TestEntityTable.TABLE.field_3); public static final EntityField SECONDARY_FIELD_1 = INSTANCE.field(SecondaryTable.TABLE.secondary_field_1); + public static final EntityField CUSTOM_COMPARABLE_FIELD = + INSTANCE.field(TestEntityTable.TABLE.field_4).comparedBy(comparing(n -> n.setScale(2, DOWN))); + private TestEntity() { super("test"); } diff --git a/main/src/test/java/com/kenshoo/pl/entity/TestEntityTable.java b/main/src/test/java/com/kenshoo/pl/entity/TestEntityTable.java index ec51d8e8..0db3e83b 100644 --- a/main/src/test/java/com/kenshoo/pl/entity/TestEntityTable.java +++ b/main/src/test/java/com/kenshoo/pl/entity/TestEntityTable.java @@ -5,6 +5,8 @@ import org.jooq.TableField; import org.jooq.impl.SQLDataType; +import java.math.BigDecimal; + /** * Created by dimag on 13/01/2016. */ @@ -20,6 +22,7 @@ private TestEntityTable() { public final TableField field_1 = createField("field_1", SQLDataType.VARCHAR.length(10)); public final TableField field_2 = createField("field_2", SQLDataType.VARCHAR.length(10)); public final TableField field_3 = createField("field_3", SQLDataType.INTEGER); + public final TableField field_4 = createField("field_4", SQLDataType.NUMERIC); @Override public TestEntityTable as(String alias) {