-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DRAF - Support comparable entity fields and DSL
- Loading branch information
Effi Ban
committed
Feb 5, 2024
1 parent
1e01826
commit 3cfb38d
Showing
9 changed files
with
120 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
main/src/main/java/com/kenshoo/pl/entity/ComparableEntityField.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.kenshoo.pl.entity; | ||
|
||
import org.jooq.Record; | ||
import org.jooq.TableField; | ||
|
||
import java.util.Objects; | ||
import java.util.function.Predicate; | ||
|
||
public interface ComparableEntityField<E extends EntityType<E>, T extends Comparable<T>> extends EntityField<E, T> { | ||
|
||
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<Record, Object>)getDbAdapter().getFirstTableField(); | ||
return new PLCondition(tableField.greaterThan(tableValue), entityFieldGreaterThan(value), this); | ||
} | ||
|
||
private Predicate<Entity> entityFieldGreaterThan(T value) { | ||
return entity -> entity.safeGet(this) | ||
.asOptional() | ||
.filter(entityValue -> valueGreaterThan(entityValue, value)) | ||
.isPresent(); | ||
} | ||
|
||
private boolean valueGreaterThan(final T value1, final T value2) { | ||
return Objects.isNull(value2) || value1.compareTo(value2) > 0; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
main/src/main/java/com/kenshoo/pl/entity/internal/ComparableEntityFieldImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.kenshoo.pl.entity.internal; | ||
|
||
import com.kenshoo.pl.entity.ComparableEntityField; | ||
import com.kenshoo.pl.entity.EntityFieldDbAdapter; | ||
import com.kenshoo.pl.entity.EntityType; | ||
import com.kenshoo.pl.entity.ValueConverter; | ||
import com.kenshoo.pl.entity.equalityfunctions.EntityValueEqualityFunction; | ||
|
||
import java.util.Comparator; | ||
|
||
public class ComparableEntityFieldImpl<E extends EntityType<E>, T extends Comparable<T>> extends EntityFieldImpl<E, T> | ||
implements ComparableEntityField<E, T> { | ||
|
||
public ComparableEntityFieldImpl(final EntityType<E> entityType, | ||
final EntityFieldDbAdapter<T> dbAdapter, | ||
final ValueConverter<T, String> stringValueConverter, | ||
final Comparator<T> valueComparator) { | ||
super(entityType, dbAdapter, stringValueConverter, (v1, v2) -> valueComparator.compare(v1, v2) == 0); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters