-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[New] Added search predicates isBefore() and isAfter() for ZonedDateTime
- Loading branch information
Showing
7 changed files
with
198 additions
and
57 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
29 changes: 29 additions & 0 deletions
29
agent/src/main/java/li/strolch/search/predicates/DatePredicate.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,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; | ||
} | ||
} |
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
31 changes: 31 additions & 0 deletions
31
agent/src/main/java/li/strolch/search/predicates/IsAfterPredicate.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,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()); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
agent/src/main/java/li/strolch/search/predicates/IsBeforePredicate.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,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()); | ||
} | ||
} |
Oops, something went wrong.