有没有办法表达TemporalAmount的“符号”?

时间:2019-08-07 09:02:47

标签: java datetime java-time

基于此answer,我决定实施自己的MutableClock进行单元测试,其工作方式略有不同:

class MutableClock extends Clock {
  private final List<Instant> instants = new ArrayList<>();

  public MutableClock(Instant now, TemporalAmount... amounts) {
    Objects.requireNonNull(now, "now must not be null");
    Objects.requireNonNull(amounts, "amounts must not be null");
    instants.add(now);
    for (TemporalAmount amount : amounts) {
        Instant latest = instants.get(instants.size() - 1);
        instants.add(latest.plus(amount));
    }
  }

  @Override
  public Instant instant() {
    Instant latest = instants.get(0);
    if (instants.size() > 1) {
        instants.remove(0);
    }
    return latest;
  }

  ... 

但是后来我注意到我在这里这样做:

instants.add(latest.plus(amount));

所以,基本上,我只能将时钟打“向前”。当然,这在大多数时候都是有意义的,但是由于所有这些都是用于单元测试的,因此我可以想象到我想使用这样一个MutableClock实例,并使它返回的时刻并不总是“增加”。

但是在查看TemporalAmount界面时:无法表达的时间吗?换句话说:TemporalAmount的实例似乎没有被“签名”。

那么,怎么可能呢?

2 个答案:

答案 0 :(得分:1)

这个问题可以很直接地解决:只需查看该接口的具体实现,即Duration。该类实际上提供了negated(),可以否定Duration对象。

因此,当传递负数的Duration时,上述实现已经有效:

["name"] = foldername

答案 1 :(得分:1)

TemporalAmount由java.time中的两个类实现:DurationPeriod。当然,用户也可以编写自己的界面实现。我没有检查,但我假设ThreeTen Extra项目的PeriodDuration类也实现了该接口。您可能认为您无法将Period添加到Instant中,因为Instant不知道Period的组成日期,月份和年份。通常,您确实不能,但是在某些情况下您可以。添加Period.ZEROPeriod.ofWeeks(3)很不错(后者将一周定义为168小时,并且我们知道,夏令时开始和结束时,虽然不正确,但是可以这样做)。简而言之:我们不能安全地假设TemporalAmountDuration

如果要对接口进行编程,一个相当简单的技巧将是在添加数量时检查时钟是否实际上倒退了:

        Instant latest = instants.get(instants.size() - 1);
        Instant newInstant = latest.plus(amount);
        if (newInstant.isBefore(latest)) {
            // The amount was negative; do whatever handling of the situation you need
        } else {
            instants.add(newInstant);
        }

当然,如果要让时钟向后移动,则无需对此情况进行特殊处理(可以省略if-else构造)。正如您在自己的答案中所指出的那样,创建一个负数没有问题,例如Duration.ofSeconds(-5)Period.ofWeeks(-3)

为什么界面不提供负数和/或负数法的测试?

尽管Duration始终明确地为负,零或正,但对于Period而言并不成立。 Period.ofMonths(1).minusDays(30)可以是负数,零或正数,具体取决于月份的选择。奇怪的是,Period有一个isNegative方法,但是它只是测试三个单位(年,月,日)中的任何一个是否为负,因此语义不是您需要的。因此Period.ofMonths(1).minusDays(3).isNegative()返回true