2012-05-06 3 views
0

Joda Time MutableDateTime 인스턴스에서 필드를 변경할 수 있어야하는 객체 세트를 작성하고 있습니다.Joda 날짜 버퍼의 비어 있지 않은 필드를 추적하는 방법

각 개체는 버퍼에 순차적으로 적용되며 모든 해당 집합이 적용될 때 완전한 유효한 MutableDateTime이 작성됩니다.

각 인스턴스는 집합의 다른 인스턴스에 의해 이미 설정된 날짜 시간 필드를 알 수 있어야합니다.

나는 다음과 같은 문제를 얻을 수 있기 때문에 내가 stucked 수 :

  1. 어떻게 구축 할 수있는 초기 값으로 사용하기 위해 모든 날짜 시간 필드에 빈 값으로 MutableDateTime 인스턴스를 만들려면?
  2. MutableDateTime의 일부 필드가 설정되었는지 어떻게 알 수 있습니까?

MutableDateTime은 내부적으로 긴 인스턴스 필드에서 데이터가 경과 된 시간 (밀리 초)으로 초기화됩니다. 따라서 모든 필드는 이미 어떤 값으로 설정되어 있습니다.

MutableDateTime에 빈 값 개념이 있는지 압니까?

편집 : 내 응답에 표시로

, 나는 블라디미르는 제안 관리자 클래스를 사용하여 솔루션을 개발한다.

+0

당신은 joda, right ;-)를 의미합니다. – assylias

+0

어, 예 joda, 감사 –

+1

날짜/시간 API에 대한 Yoda의 솔루션에 관심이 있습니다 .-) – home

답변

1

이미 설정된 필드를 기억하려면 "관리자"클래스를 만들어야합니다. 모든 필드가 설정되기 전에 사용자가 MutableDateTime의 인스턴스를 검색하려고하면 예외가 발생합니다.

그리고 항상 모든 필드를 MutableDateTime으로 설정하면 [1]은 중요하지 않습니다 (값은 덮어 쓰기됩니다).

1

마침내 초기 디자인이 바뀌었고 Vadim Ponomarev가 제안한대로 정확히 구현했습니다. Joda 버퍼의 각 필드 유형에는 해당 DateTimeFieldType 인스턴스가 있으므로 개인 필드를 사용하여 필드를 추적합니다.

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 클래스에서 부족한 공통된 개념을 잘 캡슐화한다고 생각합니다. 나는 다른 사람에게도 유용 할 수 있기를 바랍니다.

관련 문제