如何跟踪Joda日期缓冲区中的非空字段

时间:2012-05-06 06:07:30

标签: java jodatime

我正在编写一组应该能够改变Joda Time MutableDateTime实例中的字段的对象。

每个对象按顺序应用于缓冲区,当应用了所有该对象时,将构建一个完整的有效MutableDateTime。

每个实例必须能够知道集合中的其他实例已经设置了哪些日期时间字段。

我被困了因为我遇到了以下问题:

  1. 如何使用空值创建MutableDateTime实例 日期时间字段,以便将其用作构建的初始值?
  2. 我怎么知道是否设置了MutableDateTime的某个字段?
  3. MutableDateTime内部跟踪它在长实例字段中的数据,该字段初始化为从开始到现在经过的毫秒数。因此,所有字段都已设置为某个值。

    你知道MutableDateTime是否有一个空值的概念?

    修改

    正如我在回复中所示,我使用经理类开发了一个解决方案,正如弗拉基米尔所建议的那样。

2 个答案:

答案 0 :(得分:1)

您应该创建“Manager”类来记住已设置的字段。如果用户在设置所有字段之前尝试检索MutableDateTime的实例,则应抛出异常。

如果您始终为MutableDateTime设置所有字段,那么[1]并不重要(值将被覆盖)。

答案 1 :(得分:1)

我终于改变了我的初始设计,我完全按照Vadim Ponomarev的建议实现了它。由于Joda缓冲区中的每个字段类型都有一个对应的DateTimeFieldType实例,因此我使用私有的Set对象来跟踪存在的字段。

下面的代码展示了我的表现:

 private final Set<DateTimeFieldType> fieldTypes = Sets.newHashSet();

    /**
 * Allow to set to or reset one of the DateTimeFieldType fields 
 * @param fieldType the DateTimeFieldType field to change
 * @param value the value to set it
 */
    public void changeField(DateTimeFieldType fieldType, boolean value) {
        if (value)
            fieldTypes.add(fieldType);
        else
            fieldTypes.remove(fieldType);
    }

    /**
 * Check if one of the DateTimeFieldType is present in this set.
 * @param fieldType The field type to check for presence.
 * @return true if the DateTimeFieldType is present, otherwise false
 */
    public boolean isFieldSet(DateTimeFieldType fieldType) {
        return !fieldTypes.contains(fieldType);
    }

我还添加了一些实用程序方法,允许一次更改日期和所有字段的所有字段。这可以在客户端编写代码以便于在日期字段集上进行常规操作。

/**
 * Allow to set the fields that build the time part
 * of a date time
 * <p/>
 *
 * @param value value to set the DateTime fields
 */
    public void changeTimeFields(boolean value) {

        changeField(DateTimeFieldType.hourOfDay(), value);
        changeField(DateTimeFieldType.minuteOfHour(), value);
    }


/**
 * Allow to set the fields that build the date part
 * of a date time
 * <p/>
 *
 * @param value value to set the DateTime fields
 */
    public void changeDateFields(boolean value) {
        changeField(DateTimeFieldType.dayOfMonth(), value);
        changeField(DateTimeFieldType.monthOfYear(), value);
        changeField(DateTimeFieldType.yearOfEra(), value);
    }

最后,我还添加了一些方法来查询是否设置了所有日期字段以及是否设置了所有时间字段:

/**
 * Allow to check if the DateTimeFieldType fields that build the 
 * date part of a datetime has been set in this instance.
 * <p/>
 *
 * @return true if date part has yet to be applied to
 * the instance, false otherwise
 */
    public boolean isDateSet() {
        return fieldTypes.contains(DateTimeFieldType.dayOfMonth()) &&
                fieldTypes.contains(DateTimeFieldType.monthOfYear()) &&
                fieldTypes.contains(DateTimeFieldType.yearOfEra());
    }

    /**
 * Allow to check if the DateTimeFieldType fields that build the 
 * time part of a datetime has been set in this instance.
 * <p/>
 *
 * @return true if time part has yet to be applied to
 * the instance, false otherwise
 */
    public boolean isTimeSet() {
        return fieldTypes.contains(DateTimeFieldType.minuteOfHour()) &&
                fieldTypes.contains(DateTimeFieldType.hourOfDay());

    }

我最终使它成为DateTimeFieldTypeSet类。我认为它很好地封装了Joda课程中缺乏的常见概念。我希望它对其他人也有用。