I have a custom range (~ Collection
) which has 2 Temporal
bounds (from
and to
) and can enumerate all values between those 2 bounds in time by incrementing with a given TemporalUnit incrementUnitType
.
private final Temporal_ from;
private final Temporal_ to;
private final TemporalUnit incrementUnitType; // For example Month, Day, Minute, ...
In it, I need to implement a contains method, to check if iterating through that range would contain a specific value. For example if it contains 8 March. Here's how I 'd like to write that method:
public boolean contains(Temporal_ value) {
...
if (from.until(value, incrementUnitType) < 0
|| value.until(to, incrementUnitType) <= 0) {
return false; // Out of bounds
}
// This doesn't work because 1-MAR + 1 month doesn't include 8-MAR
return true;
}
Here's a few iterations:
- from - to (incrementUnitType)
- 1-MAR - 10-APR (1 day): 1-MAR, 2-MAR, 3-MAR, 4-MAR, ..., 8-APR, 9-APR
- 1-MAR - 10-APR (1 week): 1-MAR, 8-MAR, 15-MAR, 22-MAR, 29-MAR, 5-APR
- 1-MAR - 10-APR (1 month): 1-MAR, 1-APR
The code above would incorrect return true for 8-MAR in the last case. Here's how I need to write that code to work, by doing a for
loop and check every possible value:
public boolean contains(Temporal_ value) {
...
if (from.until(value, incrementUnitType) < 0
|| value.until(to, incrementUnitType) <= 0) {
return false; // Out of bounds
}
// This works but it kills scalability
for (long i = 0; i < getSize(); i++) {
Temporal_ temporal = get(i);
if (value.equals(temporal)) {
return true;
}
}
return false;
}
That's a scalability issue. Is there any way I can avoid that?