org.joda.time.field

Class FieldUtils


public class FieldUtils
extends java.lang.Object

General utilities that don't fit elsewhere.

FieldUtils is thread-safe and immutable.

Author:
Stephen Colebourne
Since:
1.0

Method Summary

static boolean
equals(Object object1, Object object2)
Compares two objects as equals handling null.
static int
getWrappedValue(int value, int minValue, int maxValue)
Utility method that ensures the given value lies within the field's legal value range.
static int
getWrappedValue(int currentValue, int wrapValue, int minValue, int maxValue)
Utility method used by addWrapField implementations to ensure the new value lies within the field's legal value range.
static int
safeAdd(int val1, int val2)
Add two values throwing an exception if overflow occurs.
static long
safeAdd(long val1, long val2)
Add two values throwing an exception if overflow occurs.
static int
safeMultiply(int val1, int val2)
Multiply two values throwing an exception if overflow occurs.
static long
safeMultiply(long val1, int scalar)
Multiply two values throwing an exception if overflow occurs.
static long
safeMultiply(long val1, long val2)
Multiply two values throwing an exception if overflow occurs.
static int
safeMultiplyToInt(long val1, long val2)
Multiply two values to return an int throwing an exception if overflow occurs.
static int
safeNegate(int value)
Negates the input throwing an exception if it can't negate it.
static long
safeSubtract(long val1, long val2)
Subtracts two values throwing an exception if overflow occurs.
static int
safeToInt(long value)
Casts to an int throwing an exception if overflow occurs.
static void
verifyValueBounds(String fieldName, int value, int lowerBound, int upperBound)
Verify that input values are within specified bounds.
static void
verifyValueBounds(DateTimeField field, int value, int lowerBound, int upperBound)
Verify that input values are within specified bounds.
static void
verifyValueBounds(DateTimeFieldType fieldType, int value, int lowerBound, int upperBound)
Verify that input values are within specified bounds.

Method Details

equals

public static boolean equals(Object object1,
                             Object object2)
Compares two objects as equals handling null.
Parameters:
object1 - the first object
object2 - the second object
Returns:
true if equal
Since:
1.4

getWrappedValue

public static int getWrappedValue(int value,
                                  int minValue,
                                  int maxValue)
Utility method that ensures the given value lies within the field's legal value range.
Parameters:
value - the value to fit into the wrapped value range
minValue - the wrap range minimum value.
maxValue - the wrap range maximum value. This must be greater than minValue (checked by the method).
Returns:
the wrapped value

getWrappedValue

public static int getWrappedValue(int currentValue,
                                  int wrapValue,
                                  int minValue,
                                  int maxValue)
Utility method used by addWrapField implementations to ensure the new value lies within the field's legal value range.
Parameters:
currentValue - the current value of the data, which may lie outside the wrapped value range
wrapValue - the value to add to current value before wrapping. This may be negative.
minValue - the wrap range minimum value.
maxValue - the wrap range maximum value. This must be greater than minValue (checked by the method).
Returns:
the wrapped value

safeAdd

public static int safeAdd(int val1,
                          int val2)
Add two values throwing an exception if overflow occurs.
Parameters:
val1 - the first value
val2 - the second value
Returns:
the new total

safeAdd

public static long safeAdd(long val1,
                           long val2)
Add two values throwing an exception if overflow occurs.
Parameters:
val1 - the first value
val2 - the second value
Returns:
the new total

safeMultiply

public static int safeMultiply(int val1,
                               int val2)
Multiply two values throwing an exception if overflow occurs.
Parameters:
val1 - the first value
val2 - the second value
Returns:
the new total
Since:
1.2

safeMultiply

public static long safeMultiply(long val1,
                                int scalar)
Multiply two values throwing an exception if overflow occurs.
Parameters:
val1 - the first value
scalar - the second value
Returns:
the new total
Since:
1.2

safeMultiply

public static long safeMultiply(long val1,
                                long val2)
Multiply two values throwing an exception if overflow occurs.
Parameters:
val1 - the first value
val2 - the second value
Returns:
the new total

safeMultiplyToInt

public static int safeMultiplyToInt(long val1,
                                    long val2)
Multiply two values to return an int throwing an exception if overflow occurs.
Parameters:
val1 - the first value
val2 - the second value
Returns:
the new total

safeNegate

public static int safeNegate(int value)
Negates the input throwing an exception if it can't negate it.
Parameters:
value - the value to negate
Returns:
the negated value
Since:
1.1

safeSubtract

public static long safeSubtract(long val1,
                                long val2)
Subtracts two values throwing an exception if overflow occurs.
Parameters:
val1 - the first value, to be taken away from
val2 - the second value, the amount to take away
Returns:
the new total

safeToInt

public static int safeToInt(long value)
Casts to an int throwing an exception if overflow occurs.
Parameters:
value - the value
Returns:
the value as an int

verifyValueBounds

public static void verifyValueBounds(String fieldName,
                                     int value,
                                     int lowerBound,
                                     int upperBound)
Verify that input values are within specified bounds.
Parameters:
value - the value to check
lowerBound - the lower bound allowed for value
upperBound - the upper bound allowed for value

verifyValueBounds

public static void verifyValueBounds(DateTimeField field,
                                     int value,
                                     int lowerBound,
                                     int upperBound)
Verify that input values are within specified bounds.
Parameters:
value - the value to check
lowerBound - the lower bound allowed for value
upperBound - the upper bound allowed for value

verifyValueBounds

public static void verifyValueBounds(DateTimeFieldType fieldType,
                                     int value,
                                     int lowerBound,
                                     int upperBound)
Verify that input values are within specified bounds.
Parameters:
value - the value to check
lowerBound - the lower bound allowed for value
upperBound - the upper bound allowed for value
Since:
1.1

Copyright (c) 2001-2006 - Joda.org