当前位置: 首页 > 知识库问答 >
问题:

时间相关变量的最佳实践

堵远航
2023-03-14

我有一个处理实时事件的系统。用户可以在指定的时间间隔插入自定义事件。实时引擎使用java.time.Duration来计时事件。通过jsp向用户展示一个菜单,该菜单以(全)秒为单位指定Duration。事件列表被序列化为Long值。所以我有三种使用相同值的方法。我创建了一个值对象,在每次访问时执行转换,这样我就不必将转换分散在整个代码中。我想知道是否有更好的方法来完成这个场景。这是我的VO。

import java.time.Duration;

public class PeriodicCommandVO {
    private String id;
    private String command;
    private Duration duration;
    private Long interval;
    private Long frequency;
    private boolean active;
    public String getCommand() {
        return command;
    }
    public void setCommand(String command) {
        this.command = command;
    }
    public Long getInterval() {
        if (interval == null) {
            if (duration != null) interval = duration.toMillis();
            else {
                if (frequency != null) interval = frequency*1000;
            }
        }
        return interval;
    }
    public void setInterval(Long interval) {
        this.interval = interval;
        this.duration = Duration.ofMillis(interval);
        this.frequency = (interval+500)/1000;
    }
    public Duration getDuration() {
        if (duration == null) {
            if (interval != null) duration = Duration.ofMillis(interval);
            else {
                if (frequency != null) duration = Duration.ofMillis(frequency*1000);
            }
        }
        return duration;
    }
    public void setDuration(Duration duration) {
        this.duration = duration;
        this.interval = duration.toMillis();
        this.frequency = (interval+500)/1000;
    }
    public Long getFrequency() {
        if (frequency == null) {
            if (duration != null) frequency = (duration.toMillis()+500)/1000;
            else {
                if (interval != null) frequency = (interval+500)/1000;
            }
        }
        return frequency;
    }
    public void setFrequency(Long frequency) {
        this.frequency = frequency;
        this.interval = frequency*1000;
        this.duration = Duration.ofSeconds(frequency);
    }
    public boolean isActive() {
        return active;
    }
    public void setActive(boolean active) {
        this.active = active;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
}

共有1个答案

吴均
2023-03-14

将转换逻辑移到表单中,以便JSP可以看到需要更新的变量。对VO使用多个构造函数,并将变量减少为仅存储持续时间(这是最常见的实现)。

 类似资料:
  • 我的Android项目中的数据类模型有一个变量lets,称之为value。 数据类Person(名称:String=“”,值:Int=0){} 我希望Int类型的变量值随时间减少。假设它每天减少1。所以如果今天是10,明天是9,后天是8等等,即使我的应用程序“死了”/没有打开。我该如何去实现这一点呢?

  • 问题内容: 在Java中,何时应使用静态非最终变量? 例如 显然,这里我们不是在谈论常量。 根据我的经验,我经常在使用单例时对它们进行辩护,但后来我最终需要拥有多个实例,这使我感到非常头痛和重构。 似乎很少在实践中使用它们。你怎么看? 问题答案: 统计信息收集可以使用非最终变量,例如,计算创建的实例数。另一方面,对于这种情况,您可能还是要使用etc,这时可能是最终的。另外,如果您要收集多个统计信息

  • 我需要存储一些应该在我的应用程序上随处可见的数据。到目前为止,我知道三种方法来做到这一点: < li >创建一个特殊文件使变量成为全局变量(Dart中的全局变量) < li >使用共享首选项(这里我只想访问数据,不一定要永久存储) 和InheritedWidget 我想知道哪个解决方案(或另一个我不知道的解决方案)在性能和易于实现方面是最好的。

  • 我正在试图找出设置实体图的最佳方法。我将基于下面的图像进行解释。 TBLParentCustomer:此表存储主要客户的信息,主要客户可以是企业或消费者。(使用查找表TBLCustomerType标识这些客户。) TBLChildCustomer:此表存储主客户下的客户。主要业务客户可以有授权员工和授权代表,主要消费客户可以有授权用户。(它们是使用查找表TBLCustomerType标识的。) T

  • 问题内容: 在eclipse-> junit-view中显示的测试执行时间取决于整个测试用例的执行,包括: 测试数据准备 执行业务逻辑 断言结果 我需要有关我的业务逻辑和仅我的业务逻辑的执行时间的更详细的说明。那就是我在测试用例中所做的: 好吧…我认为我以一种非常尴尬的方式确定时间。此外,我认为不必声明两个Date变量。 我需要一些建议来更有效地编写该代码… 问题答案: 在单元测试中,我希望为带有

  • 什么是最好的方法来衡量Android代码段的执行时间? 我有一段前后的代码,我想用时间戳来确定它的执行时间(例如,中的一段,以及活动方法中的另一段)。 我已经试过了。toMillies(false),但它只会向我返回秒数(最后是常量)。我还尝试了两个java函数:和。第一个返回纪元时间的毫秒,第二个不返回。 测量执行时间并获得良好精度的最佳方法是什么?