下面列出了java.time.LocalTime#minus ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test(dataProvider = "localTimeRanges")
public void testRangeOfLocalTimes(LocalTime start, LocalTime end, Duration step, boolean parallel) {
final Range<LocalTime> range = Range.of(start, end, step);
final Array<LocalTime> array = range.toArray(parallel);
final boolean ascend = start.isBefore(end);
final int expectedLength = (int)Math.ceil(Math.abs((double)ChronoUnit.SECONDS.between(start, end)) / (double)step.getSeconds());
Assert.assertEquals(array.length(), expectedLength);
Assert.assertEquals(array.typeCode(), ArrayType.LOCAL_TIME);
Assert.assertTrue(!array.style().isSparse());
Assert.assertEquals(range.start(), start, "The range start");
Assert.assertEquals(range.end(), end, "The range end");
LocalTime expected = null;
for (int i=0; i<array.length(); ++i) {
final LocalTime actual = array.getValue(i);
expected = expected == null ? start : ascend ? expected.plus(step) : expected.minus(step);
Assert.assertEquals(actual, expected, "Value matches at " + i);
Assert.assertTrue(ascend ? actual.compareTo(start) >=0 && actual.isBefore(end) : actual.compareTo(start) <= 0 && actual.isAfter(end), "Value in bounds at " + i);
}
}
@Test(dataProvider = "LocalTimeRanges")
public void testRangeOfLocalTimes(LocalTime start, LocalTime end, Duration step, boolean parallel) {
final boolean ascend = start.isBefore(end);
final Range<LocalTime> range = Range.of(start, end, step, v -> v.getHour() == 6);
final Array<LocalTime> array = range.toArray(parallel);
final LocalTime first = array.first(v -> true).map(ArrayValue::getValue).get();
final LocalTime last = array.last(v -> true).map(ArrayValue::getValue).get();
Assert.assertEquals(array.typeCode(), ArrayType.LOCAL_TIME);
Assert.assertTrue(!array.style().isSparse());
Assert.assertEquals(range.start(), start, "The range start");
Assert.assertEquals(range.end(), end, "The range end");
int index = 0;
LocalTime value = first;
while (ascend ? value.isBefore(last) : value.isAfter(last)) {
final LocalTime actual = array.getValue(index);
Assert.assertEquals(actual, value, "Value matches at " + index);
Assert.assertTrue(ascend ? actual.compareTo(start) >= 0 && actual.isBefore(end) : actual.compareTo(start) <= 0 && actual.isAfter(end), "Value in bounds at " + index);
value = ascend ? value.plus(step) : value.minus(step);
while (value.getHour() == 6) value = ascend ? value.plus(step) : value.minus(step);
index++;
}
}
/**
* This checks if the condition on its own works properly.
*/
@Test
public void assertThatConditionWorks() {
LocalTime currentTime = LocalTime.now().truncatedTo(ChronoUnit.MINUTES);
LocalTime beforeCurrentTime = currentTime.minus(Duration.ofMinutes(2));
LocalTime afterCurrentTime = currentTime.plus(Duration.ofMinutes(2));
// Time is between start and end time -> should return true.
TimeOfDayConditionHandler handler = getTimeOfDayConditionHandler(beforeCurrentTime.toString(),
afterCurrentTime.toString());
assertThat(handler.isSatisfied(Collections.emptyMap()), is(true));
// Time is equal to start time -> should return true
handler = getTimeOfDayConditionHandler(currentTime.toString(), afterCurrentTime.toString());
assertThat(handler.isSatisfied(Collections.emptyMap()), is(true));
// Time is equal to end time -> should return false
handler = getTimeOfDayConditionHandler(beforeCurrentTime.toString(), currentTime.toString());
assertThat(handler.isSatisfied(Collections.emptyMap()), is(false));
// Start value is in the future & end value is in the past
// -> should return false
handler = getTimeOfDayConditionHandler(afterCurrentTime.toString(), beforeCurrentTime.toString());
assertThat(handler.isSatisfied(Collections.emptyMap()), is(false));
// Start & end time are in the future & start time is after the end time
// -> should return true
handler = getTimeOfDayConditionHandler(afterCurrentTime.plus(Duration.ofMinutes(2)).toString(),
afterCurrentTime.toString());
assertThat(handler.isSatisfied(Collections.emptyMap()), is(true));
}
@Override
public Condition getPassingCondition() {
LocalTime currentTime = LocalTime.now().truncatedTo(ChronoUnit.MINUTES);
LocalTime beforeCurrentTime = currentTime.minus(Duration.ofMinutes(2));
LocalTime afterCurrentTime = currentTime.plus(Duration.ofMinutes(2));
return getTimeCondition(beforeCurrentTime.toString(), afterCurrentTime.toString());
}
@Override
public Configuration getFailingConfiguration() {
LocalTime currentTime = LocalTime.now().truncatedTo(ChronoUnit.MINUTES);
LocalTime beforeCurrentTime = currentTime.minus(Duration.ofMinutes(2));
LocalTime afterCurrentTime = currentTime.plus(Duration.ofMinutes(2));
return getTimeConfiguration(afterCurrentTime.toString(), beforeCurrentTime.toString());
}
/**
* This checks if the condition on its own works properly.
*/
@Test
public void assertThatConditionWorks() {
LocalTime currentTime = LocalTime.now().truncatedTo(ChronoUnit.MINUTES);
LocalTime beforeCurrentTime = currentTime.minus(Duration.ofMinutes(2));
LocalTime afterCurrentTime = currentTime.plus(Duration.ofMinutes(2));
// Time is between start and end time -> should return true.
TimeOfDayConditionHandler handler = getTimeOfDayConditionHandler(beforeCurrentTime.toString(),
afterCurrentTime.toString());
assertThat(handler.isSatisfied(null), is(true));
// Time is equal to start time -> should return true
handler = getTimeOfDayConditionHandler(currentTime.toString(), afterCurrentTime.toString());
assertThat(handler.isSatisfied(null), is(true));
// Time is equal to end time -> should return false
handler = getTimeOfDayConditionHandler(beforeCurrentTime.toString(), currentTime.toString());
assertThat(handler.isSatisfied(null), is(false));
// Start value is in the future & end value is in the past
// -> should return false
handler = getTimeOfDayConditionHandler(afterCurrentTime.toString(), beforeCurrentTime.toString());
assertThat(handler.isSatisfied(null), is(false));
// Start & end time are in the future & start time is after the end time
// -> should return true
handler = getTimeOfDayConditionHandler(afterCurrentTime.plus(Duration.ofMinutes(2)).toString(),
afterCurrentTime.toString());
assertThat(handler.isSatisfied(null), is(true));
}
@Override
public Condition getPassingCondition() {
LocalTime currentTime = LocalTime.now().truncatedTo(ChronoUnit.MINUTES);
LocalTime beforeCurrentTime = currentTime.minus(Duration.ofMinutes(2));
LocalTime afterCurrentTime = currentTime.plus(Duration.ofMinutes(2));
return getTimeCondition(beforeCurrentTime.toString(), afterCurrentTime.toString());
}
@Override
public Configuration getFailingConfiguration() {
LocalTime currentTime = LocalTime.now().truncatedTo(ChronoUnit.MINUTES);
LocalTime beforeCurrentTime = currentTime.minus(Duration.ofMinutes(2));
LocalTime afterCurrentTime = currentTime.plus(Duration.ofMinutes(2));
return getTimeConfiguration(afterCurrentTime.toString(), beforeCurrentTime.toString());
}