Skip to content

Commit

Permalink
[New] Added search predicates isBefore() and isAfter() for ZonedDateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
eitch committed Aug 25, 2023
1 parent fb13290 commit 517d9ff
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 57 deletions.
10 changes: 10 additions & 0 deletions agent/src/main/java/li/strolch/search/ExpressionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import li.strolch.model.StrolchRootElement;
import li.strolch.utils.collections.DateRange;

import java.time.ZonedDateTime;

/**
* An interface to add search expressions to easily discover the possible search expressions
*/
Expand Down Expand Up @@ -73,4 +75,12 @@ default <T extends StrolchRootElement> SearchExpression<T> isInIgnoreCase(Object
default <T extends StrolchRootElement> SearchExpression<T> inRange(DateRange range) {
return element -> PredicatesSupport.inRange(range).matches(extract(element));
}

default <T extends StrolchRootElement> SearchExpression<T> isBefore(ZonedDateTime date, boolean inclusive) {
return element -> PredicatesSupport.isBefore(date, inclusive).matches(extract(element));
}

default <T extends StrolchRootElement> SearchExpression<T> isAfter(ZonedDateTime date, boolean inclusive) {
return element -> PredicatesSupport.isAfter(date, inclusive).matches(extract(element));
}
}
10 changes: 10 additions & 0 deletions agent/src/main/java/li/strolch/search/PredicatesSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import li.strolch.search.predicates.*;
import li.strolch.utils.collections.DateRange;

import java.time.ZonedDateTime;

/**
* Implements predicates to be used as static imports when writing searches
*/
Expand Down Expand Up @@ -67,4 +69,12 @@ public static SearchPredicate isInIgnoreCase(Object right) {
public static SearchPredicate inRange(DateRange range) {
return new InRangePredicate(range);
}

public static SearchPredicate isBefore(ZonedDateTime dateTime, boolean inclusive) {
return new IsBeforePredicate(dateTime, inclusive);
}

public static SearchPredicate isAfter(ZonedDateTime dateTime, boolean inclusive) {
return new IsAfterPredicate(dateTime, inclusive);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package li.strolch.search.predicates;

import li.strolch.search.SearchPredicate;
import li.strolch.search.ValueCoercer;

import java.time.ZonedDateTime;
import java.util.Date;

/**
* <p>A date predicate, concrete classes implement matching.</p>
*
* <b>Note:</b> Can only be used with {@link Date} elements
*/
public abstract class DatePredicate implements SearchPredicate {

protected final ZonedDateTime dateTime;
protected final boolean inclusive;

public DatePredicate(ZonedDateTime dateTime, boolean inclusive) {
this.dateTime = dateTime;
this.inclusive = inclusive;
}

@Override
public SearchPredicate coerce(ValueCoercer coercer) {
// nothing to coerce
return this;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package li.strolch.search.predicates;

import java.time.ZonedDateTime;
import java.util.Date;

import li.strolch.search.SearchPredicate;
Expand All @@ -9,7 +10,7 @@
/**
* <p>Implements the date in range predicate.</p>
*
* <b>Note:</b> Can only be used with {@link Date} elements
* <b>Note:</b> Can only be used with {@link Date} or {@link ZonedDateTime} objects
*/
public class InRangePredicate implements SearchPredicate {
private final DateRange range;
Expand All @@ -20,7 +21,11 @@ public InRangePredicate(DateRange range) {

@Override
public boolean matches(Object left) {
return range.contains((Date) left);
if (left instanceof Date)
return this.range.contains((Date) left);
else if (left instanceof ZonedDateTime)
return this.range.contains((ZonedDateTime) left);
throw new IllegalStateException("Unhandled object type " + left.getClass());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package li.strolch.search.predicates;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

/**
* <p>Implements the date is after predicate.</p>
*
* <b>Note:</b> Can only be used with {@link Date} or {@link ZonedDateTime} objects
*/
public class IsAfterPredicate extends DatePredicate {
public IsAfterPredicate(ZonedDateTime dateTime, boolean inclusive) {
super(dateTime, inclusive);
}

@Override
public boolean matches(Object left) {
if (left instanceof Date other) {
ZonedDateTime zdt = ZonedDateTime.ofInstant(other.toInstant(), ZoneId.systemDefault());
if (this.inclusive && this.dateTime.isEqual(zdt))
return true;
return zdt.isAfter(this.dateTime);
} else if (left instanceof ZonedDateTime zdt) {
if (this.inclusive && this.dateTime.isEqual(zdt))
return true;
return zdt.isAfter(this.dateTime);
}
throw new IllegalStateException("Unhandled object type " + left.getClass());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package li.strolch.search.predicates;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;

/**
* <p>Implements the date is before predicate.</p>
*
* <b>Note:</b> Can only be used with {@link Date} or {@link ZonedDateTime} objects
*/
public class IsBeforePredicate extends DatePredicate {
public IsBeforePredicate(ZonedDateTime dateTime, boolean inclusive) {
super(dateTime, inclusive);
}

@Override
public boolean matches(Object left) {
if (left instanceof Date other) {
ZonedDateTime zdt = ZonedDateTime.ofInstant(other.toInstant(), ZoneId.systemDefault());
if (this.inclusive && this.dateTime.isEqual(zdt))
return true;
return zdt.isBefore(this.dateTime);
} else if (left instanceof ZonedDateTime zdt) {
if (this.inclusive && this.dateTime.isEqual(zdt))
return true;
return zdt.isBefore(this.dateTime);
}
throw new IllegalStateException("Unhandled object type " + left.getClass());
}
}
Loading

0 comments on commit 517d9ff

Please sign in to comment.