org.joda.time.field

Class ZeroIsMaxDateTimeField


public final class ZeroIsMaxDateTimeField
extends DecoratedDateTimeField

Wraps another field such that zero values are replaced with one more than it's maximum. This is particularly useful for implementing an clockhourOfDay field, where the midnight value of 0 is replaced with 24.

ZeroIsMaxDateTimeField is thread-safe and immutable.

Author:
Brian S O'Neill
Since:
1.0

Constructor Summary

ZeroIsMaxDateTimeField(DateTimeField field, DateTimeFieldType type)
Constructor.

Method Summary

long
add(long instant, int value)
Adds a value (which may be negative) to the instant value, overflowing into larger fields if necessary.
long
add(long instant, long value)
Adds a value (which may be negative) to the instant value, overflowing into larger fields if necessary.
long
addWrapField(long instant, int value)
Adds a value (which may be negative) to the instant value, wrapping within this field.
int[]
addWrapField(ReadablePartial instant, int fieldIndex, int[] values, int valueToAdd)
Adds a value (which may be negative) to the partial instant, wrapping within this field.
int
get(long instant)
Get the value of this field from the milliseconds.
int
getDifference(long minuendInstant, long subtrahendInstant)
Computes the difference between two instants, as measured in the units of this field.
long
getDifferenceAsLong(long minuendInstant, long subtrahendInstant)
Computes the difference between two instants, as measured in the units of this field.
int
getLeapAmount(long instant)
Gets the amount by which this field is 'leap' for the specified instant.
DurationField
getLeapDurationField()
If this field were to leap, then it would be in units described by the returned duration.
int
getMaximumValue()
Get the maximum value for the field, which is one more than the wrapped field's maximum value.
int
getMaximumValue(long instant)
Get the maximum value for the field, which is one more than the wrapped field's maximum value.
int
getMaximumValue(ReadablePartial instant)
Get the maximum value for the field, which is one more than the wrapped field's maximum value.
int
getMaximumValue(ReadablePartial instant, int[] values)
Get the maximum value for the field, which is one more than the wrapped field's maximum value.
int
getMinimumValue()
Always returns 1.
int
getMinimumValue(long instant)
Always returns 1.
int
getMinimumValue(ReadablePartial instant)
Always returns 1.
int
getMinimumValue(ReadablePartial instant, int[] values)
Always returns 1.
boolean
isLeap(long instant)
Returns whether this field is 'leap' for the specified instant.
long
remainder(long instant)
Returns the fractional duration milliseconds of this field.
long
roundCeiling(long instant)
Round to the highest whole unit of this field.
long
roundFloor(long instant)
Round to the lowest whole unit of this field.
long
roundHalfCeiling(long instant)
Round to the nearest whole unit of this field.
long
roundHalfEven(long instant)
Round to the nearest whole unit of this field.
long
roundHalfFloor(long instant)
Round to the nearest whole unit of this field.
long
set(long instant, int value)
Sets a value in the milliseconds supplied.

Methods inherited from class org.joda.time.field.DecoratedDateTimeField

get, getDurationField, getMaximumValue, getMinimumValue, getRangeDurationField, getWrappedField, isLenient, roundFloor, set

Methods inherited from class org.joda.time.field.BaseDateTimeField

add, add, add, addWrapField, addWrapField, addWrapPartial, convertText, get, getAsShortText, getAsShortText, getAsShortText, getAsShortText, getAsShortText, getAsText, getAsText, getAsText, getAsText, getAsText, getDifference, getDifferenceAsLong, getDurationField, getLeapAmount, getLeapDurationField, getMaximumShortTextLength, getMaximumTextLength, getMaximumValue, getMaximumValue, getMaximumValue, getMaximumValue, getMinimumValue, getMinimumValue, getMinimumValue, getMinimumValue, getName, getRangeDurationField, getType, isLeap, isSupported, remainder, roundCeiling, roundFloor, roundHalfCeiling, roundHalfEven, roundHalfFloor, set, set, set, set, set, toString

Methods inherited from class org.joda.time.DateTimeField

add, add, add, addWrapField, addWrapField, addWrapPartial, get, getAsShortText, getAsShortText, getAsShortText, getAsShortText, getAsShortText, getAsText, getAsText, getAsText, getAsText, getAsText, getDifference, getDifferenceAsLong, getDurationField, getLeapAmount, getLeapDurationField, getMaximumShortTextLength, getMaximumTextLength, getMaximumValue, getMaximumValue, getMaximumValue, getMaximumValue, getMinimumValue, getMinimumValue, getMinimumValue, getMinimumValue, getName, getRangeDurationField, getType, isLeap, isLenient, isSupported, remainder, roundCeiling, roundFloor, roundHalfCeiling, roundHalfEven, roundHalfFloor, set, set, set, set, set, toString

Constructor Details

ZeroIsMaxDateTimeField

public ZeroIsMaxDateTimeField(DateTimeField field,
                              DateTimeFieldType type)
Constructor.
Parameters:
field - the base field
type - the field type this field will actually use

Method Details

add

public long add(long instant,
                int value)
Adds a value (which may be negative) to the instant value, overflowing into larger fields if necessary.

The value will be added to this field. If the value is too large to be added solely to this field, larger fields will increase as required. Smaller fields should be unaffected, except where the result would be an invalid value for a smaller field. In this case the smaller field is adjusted to be in range.

For example, in the ISO chronology:
2000-08-20 add six months is 2001-02-20
2000-08-20 add twenty months is 2002-04-20
2000-08-20 add minus nine months is 1999-11-20
2001-01-31 add one month is 2001-02-28
2001-01-31 add two months is 2001-03-31

Overrides:
add in interface BaseDateTimeField
Parameters:
instant - the milliseconds from 1970-01-01T00:00:00Z to add to
value - the value to add, in the units of the field
Returns:
the updated milliseconds

add

public long add(long instant,
                long value)
Adds a value (which may be negative) to the instant value, overflowing into larger fields if necessary.
Overrides:
add in interface BaseDateTimeField
Parameters:
instant - the milliseconds from 1970-01-01T00:00:00Z to add to
value - the long value to add, in the units of the field
Returns:
the updated milliseconds

addWrapField

public long addWrapField(long instant,
                         int value)
Adds a value (which may be negative) to the instant value, wrapping within this field.

The value will be added to this field. If the value is too large to be added solely to this field then it wraps. Larger fields are always unaffected. Smaller fields should be unaffected, except where the result would be an invalid value for a smaller field. In this case the smaller field is adjusted to be in range.

For example, in the ISO chronology:
2000-08-20 addWrapField six months is 2000-02-20
2000-08-20 addWrapField twenty months is 2000-04-20
2000-08-20 addWrapField minus nine months is 2000-11-20
2001-01-31 addWrapField one month is 2001-02-28
2001-01-31 addWrapField two months is 2001-03-31

The default implementation internally calls set. Subclasses are encouraged to provide a more efficient implementation.

Overrides:
addWrapField in interface BaseDateTimeField
Parameters:
instant - the milliseconds from 1970-01-01T00:00:00Z to add to
value - the value to add, in the units of the field
Returns:
the updated milliseconds

addWrapField

public int[] addWrapField(ReadablePartial instant,
                          int fieldIndex,
                          int[] values,
                          int valueToAdd)
Adds a value (which may be negative) to the partial instant, wrapping within this field.

The value will be added to this field. If the value is too large to be added solely to this field then it wraps. Larger fields are always unaffected. Smaller fields should be unaffected, except where the result would be an invalid value for a smaller field. In this case the smaller field is adjusted to be in range.

For example, in the ISO chronology:
2000-08-20 addWrapField six months is 2000-02-20
2000-08-20 addWrapField twenty months is 2000-04-20
2000-08-20 addWrapField minus nine months is 2000-11-20
2001-01-31 addWrapField one month is 2001-02-28
2001-01-31 addWrapField two months is 2001-03-31

The default implementation internally calls set. Subclasses are encouraged to provide a more efficient implementation.

Overrides:
addWrapField in interface BaseDateTimeField
Parameters:
instant - the partial instant
fieldIndex - the index of this field in the instant
values - the values of the partial instant which should be updated
valueToAdd - the value to add, in the units of the field
Returns:
the passed in values

get

public int get(long instant)
Get the value of this field from the milliseconds.
Overrides:
get in interface DecoratedDateTimeField
Parameters:
instant - the milliseconds from 1970-01-01T00:00:00Z to query
Returns:
the value of the field, in the units of the field

getDifference

public int getDifference(long minuendInstant,
                         long subtrahendInstant)
Computes the difference between two instants, as measured in the units of this field. Any fractional units are dropped from the result. Calling getDifference reverses the effect of calling add. In the following code:
 long instant = ...
 int v = ...
 int age = getDifference(add(instant, v), instant);
 
The value 'age' is the same as the value 'v'.
Overrides:
getDifference in interface BaseDateTimeField
Parameters:
minuendInstant - the milliseconds from 1970-01-01T00:00:00Z to subtract from
subtrahendInstant - the milliseconds from 1970-01-01T00:00:00Z to subtract off the minuend
Returns:
the difference in the units of this field

getDifferenceAsLong

public long getDifferenceAsLong(long minuendInstant,
                                long subtrahendInstant)
Computes the difference between two instants, as measured in the units of this field. Any fractional units are dropped from the result. Calling getDifference reverses the effect of calling add. In the following code:
 long instant = ...
 long v = ...
 long age = getDifferenceAsLong(add(instant, v), instant);
 
The value 'age' is the same as the value 'v'.
Overrides:
getDifferenceAsLong in interface BaseDateTimeField
Parameters:
minuendInstant - the milliseconds from 1970-01-01T00:00:00Z to subtract from
subtrahendInstant - the milliseconds from 1970-01-01T00:00:00Z to subtract off the minuend
Returns:
the difference in the units of this field

getLeapAmount

public int getLeapAmount(long instant)
Gets the amount by which this field is 'leap' for the specified instant.

For example, a leap year would return one, a non leap year would return zero.

This implementation returns zero.

Overrides:
getLeapAmount in interface BaseDateTimeField

getLeapDurationField

public DurationField getLeapDurationField()
If this field were to leap, then it would be in units described by the returned duration. If this field doesn't ever leap, null is returned.

This implementation returns null.

Overrides:
getLeapDurationField in interface BaseDateTimeField

getMaximumValue

public int getMaximumValue()
Get the maximum value for the field, which is one more than the wrapped field's maximum value.
Overrides:
getMaximumValue in interface DecoratedDateTimeField
Returns:
the maximum value

getMaximumValue

public int getMaximumValue(long instant)
Get the maximum value for the field, which is one more than the wrapped field's maximum value.
Overrides:
getMaximumValue in interface BaseDateTimeField
Returns:
the maximum value

getMaximumValue

public int getMaximumValue(ReadablePartial instant)
Get the maximum value for the field, which is one more than the wrapped field's maximum value.
Overrides:
getMaximumValue in interface BaseDateTimeField
Returns:
the maximum value

getMaximumValue

public int getMaximumValue(ReadablePartial instant,
                           int[] values)
Get the maximum value for the field, which is one more than the wrapped field's maximum value.
Overrides:
getMaximumValue in interface BaseDateTimeField
Returns:
the maximum value

getMinimumValue

public int getMinimumValue()
Always returns 1.
Overrides:
getMinimumValue in interface DecoratedDateTimeField
Returns:
the minimum value of 1

getMinimumValue

public int getMinimumValue(long instant)
Always returns 1.
Overrides:
getMinimumValue in interface BaseDateTimeField
Returns:
the minimum value of 1

getMinimumValue

public int getMinimumValue(ReadablePartial instant)
Always returns 1.
Overrides:
getMinimumValue in interface BaseDateTimeField
Returns:
the minimum value of 1

getMinimumValue

public int getMinimumValue(ReadablePartial instant,
                           int[] values)
Always returns 1.
Overrides:
getMinimumValue in interface BaseDateTimeField
Returns:
the minimum value of 1

isLeap

public boolean isLeap(long instant)
Returns whether this field is 'leap' for the specified instant.

For example, a leap year would return true, a non leap year would return false.

This implementation returns false.

Overrides:
isLeap in interface BaseDateTimeField
Returns:
true if the field is 'leap'

remainder

public long remainder(long instant)
Returns the fractional duration milliseconds of this field. In other words, calling remainder returns the duration that roundFloor would subtract.

For example, on a datetime of 2002-11-02T23:34:56.789, the remainder by hour is 34 minutes and 56.789 seconds.

The default implementation computes instant - roundFloor(instant). Subclasses are encouraged to provide a more efficient implementation.

Overrides:
remainder in interface BaseDateTimeField
Parameters:
instant - the milliseconds from 1970-01-01T00:00:00Z to get the remainder
Returns:
remainder duration, in milliseconds

roundCeiling

public long roundCeiling(long instant)
Round to the highest whole unit of this field. The value of this field and all fields of a higher magnitude may be incremented in order to achieve this result. The fractional millis that cannot be expressed in whole increments of this field are set to minimum.

For example, a datetime of 2002-11-02T23:34:56.789, rounded to the highest whole hour is 2002-11-03T00:00:00.000.

The default implementation calls roundFloor, and if the instant is modified as a result, adds one field unit. Subclasses are encouraged to provide a more efficient implementation.

Overrides:
roundCeiling in interface BaseDateTimeField
Parameters:
instant - the milliseconds from 1970-01-01T00:00:00Z to round
Returns:
rounded milliseconds

roundFloor

public long roundFloor(long instant)
Round to the lowest whole unit of this field. After rounding, the value of this field and all fields of a higher magnitude are retained. The fractional millis that cannot be expressed in whole increments of this field are set to minimum.

For example, a datetime of 2002-11-02T23:34:56.789, rounded to the lowest whole hour is 2002-11-02T23:00:00.000.

Overrides:
roundFloor in interface DecoratedDateTimeField
Parameters:
instant - the milliseconds from 1970-01-01T00:00:00Z to round
Returns:
rounded milliseconds

roundHalfCeiling

public long roundHalfCeiling(long instant)
Round to the nearest whole unit of this field. If the given millisecond value is closer to the floor, this function behaves like roundFloor. If the millisecond value is closer to the ceiling or is exactly halfway, this function behaves like roundCeiling.
Overrides:
roundHalfCeiling in interface BaseDateTimeField
Parameters:
instant - the milliseconds from 1970-01-01T00:00:00Z to round
Returns:
rounded milliseconds

roundHalfEven

public long roundHalfEven(long instant)
Round to the nearest whole unit of this field. If the given millisecond value is closer to the floor, this function behaves like roundFloor. If the millisecond value is closer to the ceiling, this function behaves like roundCeiling.

If the millisecond value is exactly halfway between the floor and ceiling, the ceiling is chosen over the floor only if it makes this field's value even.

Overrides:
roundHalfEven in interface BaseDateTimeField
Parameters:
instant - the milliseconds from 1970-01-01T00:00:00Z to round
Returns:
rounded milliseconds

roundHalfFloor

public long roundHalfFloor(long instant)
Round to the nearest whole unit of this field. If the given millisecond value is closer to the floor or is exactly halfway, this function behaves like roundFloor. If the millisecond value is closer to the ceiling, this function behaves like roundCeiling.
Overrides:
roundHalfFloor in interface BaseDateTimeField
Parameters:
instant - the milliseconds from 1970-01-01T00:00:00Z to round
Returns:
rounded milliseconds

set

public long set(long instant,
                int value)
Sets a value in the milliseconds supplied.

The value of this field will be set. If the value is invalid, an exception if thrown.

If setting this field would make other fields invalid, then those fields may be changed. For example if the current date is the 31st January, and the month is set to February, the day would be invalid. Instead, the day would be changed to the closest value - the 28th/29th February as appropriate.

Overrides:
set in interface DecoratedDateTimeField
Parameters:
instant - the milliseconds from 1970-01-01T00:00:00Z to set in
value - the value to set, in the units of the field
Returns:
the updated milliseconds

Copyright (c) 2001-2006 - Joda.org