@Status(stage=PRODUCTION, unitTests=ABSENT) @Review(by="Kees Schotanus", at="2009-09-28") @ThreadSafety(level=IMMUTABLE) public final class CLong extends Number implements Comparable<CLong>
Long
is that
this class has support for arithmetic operations that protect against both
underflow and overflow.Long
is final so this class extends
class Number
instead. This class is immutable and contains private
constructors in combination with static factory methods as suggested by
Joshua Bloch (Effective Java, Second Edition, item 15).Modifier and Type | Class and Description |
---|---|
static class |
CLong.FibonacciIterator
Iterator suitable for iterating over all the Fibonacci numbers in sequence.
|
Modifier and Type | Field and Description |
---|---|
static int |
MAX_FACTORIAL_ORDINAL
The maximum ordinal for which the factorial can be calculated.
|
static int |
MAX_FIBONACCI_ORDINAL
The maximum ordinal for which the Fibonacci number can be calculated.
|
Modifier and Type | Method and Description |
---|---|
CLong |
add(long value)
Adds the supplied value to this value.
|
static long |
add(long x,
long y)
Adds y to x.
|
byte |
byteValue()
Returns the value of this CLong as a byte.
|
int |
compareTo(CLong object)
Compares two CLong objects numerically.
|
CLong |
divide(long value)
Divides this value by the supplied value.
|
static long |
divide(long x,
long y)
Divides x by y.
|
CLong |
divideAndFloor(long value)
Divides this value by the supplied value and floors the result.
|
static long |
divideAndFloor(long x,
long y)
Divides x by y and floors the result.
|
double |
doubleValue()
Returns the value of this CLong as a double.
|
boolean |
equals(Object object)
Determines whether this object and the supplied object are the same.
|
static long |
factorial(int value)
Computes the factorial of the supplied value.
|
static long |
fibonacci(int x)
Computes fibonacci number x.
|
static CLong.FibonacciIterator |
fibonacciIterator()
Creates a Fibonacci Iterator that allows you to iterate over all the Fibonacci numbers in sequence.
|
float |
floatValue()
Returns the value of this CLong as a float.
|
static long |
greatestCommonDivisor(long first,
long second)
Computes the greatest common divisor of two long values.
|
int |
hashCode()
Computes a hash code for this object using the same algorithm as
Long.hashCode() does. |
int |
intValue()
Returns the value of this CLong as an integer.
|
static boolean |
isPrime(long value)
Determines whether the supplied value is a prime.
|
static long |
leastCommonMultiple(long a,
long b)
Computes the least common multiple of two long values.
|
long |
longValue()
Returns the value of this CLong as a long.
|
CLong |
multiply(long value)
Multiplies this value by the supplied value.
|
static long |
multiply(long x,
long y)
Multiplies x by y.
|
static long |
round(long value,
int position)
Rounds the supplied value to the supplied position.
|
static long |
roundUp(long value,
int position)
Rounds the supplied value up to the supplied position.
|
short |
shortValue()
Returns the value of this CLong as a short.
|
CLong |
subtract(long value)
Subtracts the supplied value from this value.
|
static long |
subtract(long x,
long y)
Subtracts y from x.
|
String |
toString()
Converts the stored primitive long value to a String.
|
static CLong |
valueOf(int value)
Returns a CLong object holding the value of the supplied value.
|
static CLong |
valueOf(long value)
Returns a CLong object holding the value of the supplied value.
|
static CLong |
valueOf(Number value)
Returns a CLong object holding the value of the supplied value.
|
static CLong |
valueOf(String value)
Returns a CLong object holding the parsed value of the supplied value.
|
public static final int MAX_FACTORIAL_ORDINAL
public static final int MAX_FIBONACCI_ORDINAL
public byte byteValue()
public short shortValue()
shortValue
in class Number
public int intValue()
public long longValue()
public float floatValue()
floatValue
in class Number
public double doubleValue()
doubleValue
in class Number
public static CLong valueOf(int value)
value
- The long value.public static CLong valueOf(long value)
value
- The long value.public static CLong valueOf(Number value)
Number.longValue()
is used to
perform the conversion.value
- The Number that holds the value.NullPointerException
- When the supplied value is null.public static CLong valueOf(String value)
value
- The String that is parsed.IllegalArgumentException
- When the supplied value (after parsing)
is not in the range:
[Long.MIN_VALUE
to Long.MAX_VALUE
].NumberFormatException
- When the supplied value does not contain a
parsable long (this includes a null or empty value).Long.parseLong(String)
public boolean equals(Object object)
public int hashCode()
Long.hashCode()
does.public int compareTo(CLong object)
compareTo
in interface Comparable<CLong>
object
- CLong object to compare with this object.NullPointerException
- When the supplied object is null.Comparable
public CLong add(long value)
value
- Value to add to the stored value.ArithmeticException
- When an arithmetic underflow or overflow
occurs.add(long, long)
public static long add(long x, long y)
x
- Operand one.y
- Operand two.ArithmeticException
- When an arithmetic underflow or overflow
occurs.public CLong subtract(long value)
value
- Value to subtract from the stored value.ArithmeticException
- When an arithmetic underflow or overflow
occurs.subtract(long, long)
public static long subtract(long x, long y)
x
- Operand one.y
- operand two.ArithmeticException
- When an arithmetic underflow or overflow
occurs.public CLong multiply(long value)
value
- Value to multiply by the stored value.ArithmeticException
- When an arithmetic underflow or overflow
occurs.multiply(long, long)
public static long multiply(long x, long y)
x
- Operand one.y
- Operand two.ArithmeticException
- When an arithmetic underflow or overflow
occurs.public CLong divide(long value)
value
- Value by which this value will be divided.ArithmeticException
- When an arithmetic overflow occurs or when
the supplied value is 0.divide(long, long)
public static long divide(long x, long y)
x
- Operand one.y
- Operand two.ArithmeticException
- When an arithmetic overflow occurs or when
the supplied y value is 0.public CLong divideAndFloor(long value)
value
- Value by which this value will be divided.ArithmeticException
- When an arithmetic overflow occurs or when
the supplied value is 0.divideAndFloor(long, long)
public static long divideAndFloor(long x, long y)
x
- Operand one.y
- Operand two.ArithmeticException
- When an arithmetic overflow occurs or when
the supplied y value is 0.public static long greatestCommonDivisor(long first, long second)
first
- The first value.second
- The second value.IllegalArgumentException
- When the supplied second parameter is 0.public static long leastCommonMultiple(long a, long b)
a
- The first long value.b
- The second long value.public static boolean isPrime(long value)
value
- Value to check whether it is a prime number or not.public static long factorial(int value)
public static long factorialRecursive(final long value) {
if (value <= 1) {
return 1;
} else {
return value * factorialRecursive(value - 1);
}
}
If you check out the actual implementation you will see that not using
recursion is better in this case. The current implementation does not
have the overhead of method calls and only has to check for a wrong
supplied value, once (the implementation above does not check for errors,
but if it did, it would do so on every recursive call).value
- Value for which the factorial is calculated.IllegalArgumentException
- When supplied value is not in range:
0 <= value <= MAX_FIBONACCI_ORDINAL
.public static long fibonacci(int x)
#0: 0 By definition
#1: 1 By definition
#2: 1 For all others it is the sum of the previous two numbers.
#3: 2
#4: 3
#5: 5
#6: 8
#7: 13
#8: 21
#9: 34
x
- The fibonacci number to compute.IllegalArgumentException
- When the supplied x value is < 0.ArithmeticException
- When the supplied x value is larger than
MAX_FIBONACCI_ORDINAL
since the result would not fit into a long.public static CLong.FibonacciIterator fibonacciIterator()
public static long round(long value, int position)
value
- The value to round.position
- The position to which this value should be rounded.
IllegalArgumentException
- When the supplied position is negative.public static long roundUp(long value, int position)
value
- The value to round.position
- The position to which this value should be rounded.
IllegalArgumentException
- When the supplied position is negative.Copyright © 2008–2018. All rights reserved.