마침내 초기 디자인이 바뀌었고 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 클래스에서 부족한 공통된 개념을 잘 캡슐화한다고 생각합니다. 나는 다른 사람에게도 유용 할 수 있기를 바랍니다.
당신은 joda, right ;-)를 의미합니다. – assylias
어, 예 joda, 감사 –
날짜/시간 API에 대한 Yoda의 솔루션에 관심이 있습니다 .-) – home