Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright 2001-2005 Stephen Colebourne
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  *  Unless required by applicable law or agreed to in writing, software
  *  distributed under the License is distributed on an "AS IS" BASIS,
  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *  See the License for the specific language governing permissions and
  *  limitations under the License.
  */
 package org.joda.time.field;
 
General utilities that don't fit elsewhere.

FieldUtils is thread-safe and immutable.

Author(s):
Stephen Colebourne
Since:
1.0
 
 public class FieldUtils {

    
Restricted constructor.
 
     private FieldUtils() {
         super();
     }
     
     //------------------------------------------------------------------------
     
Negates the input throwing an exception if it can't negate it.

Parameters:
value the value to negate
Returns:
the negated value
Throws:
java.lang.ArithmeticException if the value is Integer.MIN_VALUE
Since:
1.1
 
     public static int safeNegate(int value) {
         if (value == .) {
             throw new ArithmeticException("Integer.MIN_VALUE cannot be negated");
         }
         return -value;
     }
    
    
Add two values throwing an exception if overflow occurs.

Parameters:
val1 the first value
val2 the second value
Returns:
the new total
Throws:
java.lang.ArithmeticException if the value is too big or too small
 
     public static int safeAdd(int val1int val2) {
         int sum = val1 + val2;
         // If there is a sign change, but the two values have the same sign...
         if ((val1 ^ sum) < 0 && (val1 ^ val2) >= 0) {
             throw new ArithmeticException
                 ("The calculation caused an overflow: " + val1 + " + " + val2);
         }
         return sum;
     }
    
    
Add two values throwing an exception if overflow occurs.

Parameters:
val1 the first value
val2 the second value
Returns:
the new total
Throws:
java.lang.ArithmeticException if the value is too big or too small
 
     public static long safeAdd(long val1long val2) {
         long sum = val1 + val2;
         // If there is a sign change, but the two values have the same sign...
         if ((val1 ^ sum) < 0 && (val1 ^ val2) >= 0) {
             throw new ArithmeticException
                 ("The calculation caused an overflow: " + val1 + " + " + val2);
         }
         return sum;
     }
    
    
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
Throws:
java.lang.ArithmeticException if the value is too big or too small
 
     public static long safeSubtract(long val1long val2) {
        long diff = val1 - val2;
        // If there is a sign change, but the two values have different signs...
        if ((val1 ^ diff) < 0 && (val1 ^ val2) < 0) {
            throw new ArithmeticException
                ("The calculation caused an overflow: " + val1 + " - " + val2);
        }
        return diff;
    }
    
    
Multiply two values throwing an exception if overflow occurs.

Parameters:
val1 the first value
val2 the second value
Returns:
the new total
Throws:
java.lang.ArithmeticException if the value is too big or too small
Since:
1.2
    public static int safeMultiply(int val1int val2) {
        long total = (longval1 * (longval2;
        if (total < . || total > .) {
            throw new ArithmeticException
                ("The calculation caused an overflow: " + val1 + " * " + val2);
        }
        return (inttotal;
    }

    
Multiply two values throwing an exception if overflow occurs.

Parameters:
val1 the first value
scalar the second value
Returns:
the new total
Throws:
java.lang.ArithmeticException if the value is too big or too small
Since:
1.2
    public static long safeMultiply(long val1int scalar) {
        switch (scalar) {
        case -1:
            return -val1;
        case 0:
            return 0L;
        case 1:
            return val1;
        }
        long total = val1 * scalar;
        if (total / scalar != val1) {
            throw new ArithmeticException
                ("The calculation caused an overflow: " + val1 + " * " + scalar);
        }
        return total;
    }

    
Multiply two values throwing an exception if overflow occurs.

Parameters:
val1 the first value
val2 the second value
Returns:
the new total
Throws:
java.lang.ArithmeticException if the value is too big or too small
    public static long safeMultiply(long val1long val2) {
        if (val2 == 1) {
            return val1;
        }
        if (val2 == 0) {
            return 0;
        }
        long total = val1 * val2;
        if (total / val2 != val1) {
            throw new ArithmeticException
                ("The calculation caused an overflow: " + val1 + " * " + val2);
        }
        return total;
    }
    
    
Casts to an int throwing an exception if overflow occurs.

Parameters:
value the value
Returns:
the value as an int
Throws:
java.lang.ArithmeticException if the value is too big or too small
    public static int safeToInt(long value) {
        if (. <= value && value <= .) {
            return (intvalue;
        }
        throw new ArithmeticException("Value cannot fit in an int: " + value);
    }
    
    
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
Throws:
java.lang.ArithmeticException if the value is too big or too small
    public static int safeMultiplyToInt(long val1long val2) {
        long val = FieldUtils.safeMultiply(val1val2);
        return FieldUtils.safeToInt(val);
    }
    //-----------------------------------------------------------------------
    
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
Throws:
org.joda.time.IllegalFieldValueException if value is not in the specified bounds
    public static void verifyValueBounds(DateTimeField field
                                         int valueint lowerBoundint upperBound) {
        if ((value < lowerBound) || (value > upperBound)) {
            throw new IllegalFieldValueException
                (field.getType(), Integer.valueOf(value),
                 Integer.valueOf(lowerBound), Integer.valueOf(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
Throws:
org.joda.time.IllegalFieldValueException if value is not in the specified bounds
Since:
1.1
    public static void verifyValueBounds(DateTimeFieldType fieldType
                                         int valueint lowerBoundint upperBound) {
        if ((value < lowerBound) || (value > upperBound)) {
            throw new IllegalFieldValueException
                (fieldType, Integer.valueOf(value),
                 Integer.valueOf(lowerBound), Integer.valueOf(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
Throws:
org.joda.time.IllegalFieldValueException if value is not in the specified bounds
    public static void verifyValueBounds(String fieldName,
                                         int valueint lowerBoundint upperBound) {
        if ((value < lowerBound) || (value > upperBound)) {
            throw new IllegalFieldValueException
                (fieldName, Integer.valueOf(value),
                 Integer.valueOf(lowerBound), Integer.valueOf(upperBound));
        }
    }

    
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
Throws:
java.lang.IllegalArgumentException if minValue is greater than or equal to maxValue
    public static int getWrappedValue(int currentValueint wrapValue,
                                      int minValueint maxValue) {
        return getWrappedValue(currentValue + wrapValueminValuemaxValue);
    }

    
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
Throws:
java.lang.IllegalArgumentException if minValue is greater than or equal to maxValue
    public static int getWrappedValue(int valueint minValueint maxValue) {
        if (minValue >= maxValue) {
            throw new IllegalArgumentException("MIN > MAX");
        }
        int wrapRange = maxValue - minValue + 1;
        value -= minValue;
        if (value >= 0) {
            return (value % wrapRange) + minValue;
        }
        int remByRange = (-value) % wrapRange;
        if (remByRange == 0) {
            return 0 + minValue;
        }
        return (wrapRange - remByRange) + minValue;
    }
    //-----------------------------------------------------------------------
    
Compares two objects as equals handling null.

Parameters:
object1 the first object
object2 the second object
Returns:
true if equal
Since:
1.4
    public static boolean equals(Object object1Object object2) {
        if (object1 == object2) {
            return true;
        }
        if (object1 == null || object2 == null) {
            return false;
        }
        return object1.equals(object2);
    }
New to GrepCode? Check out our FAQ X