public final class Util extends Object
Modifier and Type | Field and Description |
---|---|
static double |
INVERSE_GOLDEN
The inverse golden ratio as a fraction.
|
static long |
INVERSE_GOLDEN_U64
The inverse golden ratio as an unsigned long.
|
static double |
LOG2
The natural logarithm of 2.0.
|
static double |
LONG_MAX_VALUE_AS_DOUBLE
Long.MAX_VALUE as a double.
|
static String |
LS
The java line separator character as a String.
|
static char |
TAB
The tab character
|
Modifier and Type | Method and Description |
---|---|
static int |
bitAt(long number,
int bitPos)
Returns a one if the bit at bitPos is a one, otherwise zero.
|
static int |
bytesToInt(byte[] arr)
Returns an int extracted from a Little-Endian byte array.
|
static long |
bytesToLong(byte[] arr)
Returns a long extracted from a Little-Endian byte array.
|
static String |
bytesToString(byte[] arr,
boolean signed,
boolean littleEndian,
String sep)
Returns a string view of a byte array
|
static double |
ceilingPowerBaseOfDouble(double base,
double n)
Returns the ceiling of a given n given a base, where the ceiling is an integral power of the base.
|
static int |
ceilingPowerOf2(int n)
Computes the int ceiling power of 2 within the range [1, 2^30].
|
static long |
ceilingPowerOf2(long n)
Computes the long ceiling power of 2 within the range [1, 2^62].
|
static String |
characterPad(String s,
int fieldLength,
char padChar,
boolean postpend)
Prepend or postpend the given string with the given character to fill the given field length.
|
static void |
checkBounds(long reqOff,
long reqLen,
long allocSize)
Check the requested offset and length against the allocated size.
|
static void |
checkIfMultipleOf8AndGT0(long v,
String argName)
Checks if parameter v is a multiple of 8 and greater than zero.
|
static void |
checkIfPowerOf2(long n,
String argName)
Checks the given long argument if it is a positive integer power of 2.
|
static void |
checkProbability(double p,
String argName)
Checks the given parameter to make sure it is positive and between 0.0 inclusive and 1.0
inclusive.
|
static int |
exactLog2OfInt(int powerOf2)
Returns the log2 of the given int value if it is an exact power of 2 and greater than zero.
|
static int |
exactLog2OfInt(int powerOf2,
String argName)
Returns the log2 of the given int value if it is an exact power of 2 and greater than zero.
|
static int |
exactLog2OfLong(long powerOf2)
Returns the log2 of the given long value if it is an exact power of 2 and greater than zero.
|
static int |
exactLog2OfLong(long powerOf2,
String argName)
Returns the log2 of the given long value if it is an exact power of 2 and greater than zero.
|
static double |
floorPowerBaseOfDouble(double base,
double n)
Computes the floor of a given n given base, where the floor is an integral power of the base.
|
static int |
floorPowerOf2(int n)
Computes the floor power of 2 given n is in the range [1, 2^31-1].
|
static long |
floorPowerOf2(long n)
Computes the floor power of 2 given n is in the range [1, 2^63-1].
|
static byte[] |
intToBytes(int v,
byte[] arr)
Returns a Little-Endian byte array extracted from the given int.
|
static double |
invPow2(int e)
Computes the inverse integer power of 2: 1/(2^e) = 2^(-e).
|
static boolean |
isEven(long n)
Returns true if given n is even.
|
static boolean |
isLessThanUnsigned(long n1,
long n2)
Unsigned compare with longs.
|
static boolean |
isMultipleOf8AndGT0(long v)
Returns true if v is a multiple of 8 and greater than zero
|
static boolean |
isOdd(long n)
Returns true if given n is odd.
|
static boolean |
isPowerOf2(long n)
Returns true if given long argument is exactly a positive power of 2.
|
static <T> boolean |
le(Object item1,
Object item2,
Comparator<? super T> c)
Is item1 Less-Than-Or-Equal-To item2
|
static double |
log2(double value)
The log2(value)
|
static double |
logBaseOfX(double base,
double x)
Returns the logbase(x).
|
static byte[] |
longToBytes(long v,
byte[] arr)
Returns a Little-Endian byte array extracted from the given long.
|
static String |
longToFixedLengthString(long number,
int length)
Converts the given number to a string prepended with spaces, if necessary, to
match the given length.
|
static String |
longToHexBytes(long v)
Returns a string of spaced hex bytes in Big-Endian order.
|
static <T> boolean |
lt(Object item1,
Object item2,
Comparator<? super T> c)
Is item1 Less-Than item2
|
static <T> Object |
maxT(Object item1,
Object item2,
Comparator<? super T> c)
Finds the maximum of two generic items
|
static String |
milliSecToString(long mS)
Returns the given time in milliseconds formatted as Hours:Min:Sec.mSec
|
static <T> Object |
minT(Object item1,
Object item2,
Comparator<? super T> c)
Finds the minimum of two generic items
|
static String |
nanoSecToString(long nS)
Returns the given time in nanoseconds formatted as Sec.mSec_uSec_nSec
|
static int |
numberOfLeadingOnes(long v)
Returns the number of one bits preceding the highest-order ("leftmost") zero-bit in the
two's complement binary representation of the specified long value, or 64 if the value is equal
to minus one.
|
static int |
numberOfTrailingOnes(long v)
Returns the number of one bits following the lowest-order ("rightmost") zero-bit in the
two's complement binary representation of the specified long value, or 64 if the value is equal
to minus one.
|
static int |
numDigits(long n)
Computes the number of decimal digits of the number n
|
static double |
powerSeriesNextDouble(int ppb,
double curPoint,
boolean roundToLong,
double logBase)
Computes the next larger double in the power series
point = logBase( i / ppb ) given the current point in the series.
|
static long |
pwr2SeriesNext(int ppo,
long curPoint)
Computes the next larger integer point in the power series
point = 2( i / ppo ) given the current point in the series.
|
static int |
pwr2SeriesPrev(int ppo,
int curPoint)
Computes the previous, smaller integer point in the power series
point = 2( i / ppo ) given the current point in the series.
|
static String |
zeroPad(String s,
int fieldLength)
Prepend the given string with zeros.
|
public static final String LS
public static final char TAB
public static final double LOG2
public static final long INVERSE_GOLDEN_U64
public static final double INVERSE_GOLDEN
public static final double LONG_MAX_VALUE_AS_DOUBLE
public static int bytesToInt(byte[] arr)
arr
- the given byte arraypublic static long bytesToLong(byte[] arr)
arr
- the given byte arraypublic static byte[] intToBytes(int v, byte[] arr)
v
- the given intarr
- a given array of 4 bytes that will be returned with the datapublic static byte[] longToBytes(long v, byte[] arr)
v
- the given longarr
- a given array of 8 bytes that will be returned with the datapublic static String longToHexBytes(long v)
v
- the given longpublic static String bytesToString(byte[] arr, boolean signed, boolean littleEndian, String sep)
arr
- the given byte arraysigned
- set true if you want the byte values signed.littleEndian
- set true if you want Little-Endian ordersep
- the separator string between bytespublic static String nanoSecToString(long nS)
nS
- the given nanosecondspublic static String milliSecToString(long mS)
mS
- the given millisecondspublic static String zeroPad(String s, int fieldLength)
s
- the given stringfieldLength
- desired total field length including the given stringpublic static String characterPad(String s, int fieldLength, char padChar, boolean postpend)
s
- the given stringfieldLength
- the desired field lengthpadChar
- the desired pad characterpostpend
- if true append the pacCharacters to the end of the string.public static void checkIfMultipleOf8AndGT0(long v, String argName)
v
- The parameter to checkargName
- This name will be part of the error message if the check fails.public static boolean isMultipleOf8AndGT0(long v)
v
- The parameter to checkpublic static boolean isPowerOf2(long n)
n
- The input argument.public static void checkIfPowerOf2(long n, String argName)
n
- The input long argument must be a positive integer power of 2.argName
- Used in the thrown exception. It may be null.SketchesArgumentException
- if not a positive integer power of 2.public static int ceilingPowerOf2(int n)
For:
n
- The input int argument.public static long ceilingPowerOf2(long n)
For:
n
- The input long argument.public static int floorPowerOf2(int n)
For:
n
- The given int argument.public static long floorPowerOf2(long n)
For:
n
- The given long argument.public static double invPow2(int e)
e
- a positive value between 0 and 1023 inclusivepublic static long pwr2SeriesNext(int ppo, long curPoint)
int maxP = 1024;
int minP = 1;
int ppo = 2;
for (int p = minP; p <= maxP; p = pwr2LawNext(ppo, p)) {
System.out.print(p + " ");
}
//generates the following series:
//1 2 3 4 6 8 11 16 23 32 45 64 91 128 181 256 362 512 724 1024
ppo
- Points-Per-Octave, or the number of points per integer powers of 2 in the series.curPoint
- the current point of the series. Must be ≥ 1.public static int pwr2SeriesPrev(int ppo, int curPoint)
int maxP = 1024;
int minP = 1;
int ppo = 2;
for (int p = maxP; p >= minP; p = pwr2LawPrev(ppo, p)) {
System.out.print(p + " ");
}
//generates the following series:
//1024 724 512 362 256 181 128 91 64 45 32 23 16 11 8 6 4 3 2 1
ppo
- Points-Per-Octave, or the number of points per integer powers of 2 in the series.curPoint
- the current point of the series. Must be ≥ 1.public static double powerSeriesNextDouble(int ppb, double curPoint, boolean roundToLong, double logBase)
double maxP = 1024.0;
double minP = 1.0;
int ppb = 2;
double logBase = 2.0;
for (double p = minP; p <= maxP; p = powerSeriesNextDouble(ppb, p, true, logBase)) {
System.out.print(p + " ");
}
//generates the following series:
//1 2 3 4 6 8 11 16 23 32 45 64 91 128 181 256 362 512 724 1024
ppb
- Points-Per-Base, or the number of points per integer powers of base in the series.curPoint
- the current point of the series. Must be ≥ 1.0.roundToLong
- if true the output will be rounded to the nearest long.logBase
- the desired base of the logarithmspublic static double ceilingPowerBaseOfDouble(double base, double n)
ceilingPowerOf2(int)
for values
less than one. I.e., if n < 1, the result is 1.
The formula is: baseceiling(logbase(x))
base
- The number in the expression ⌈basen⌉.n
- The input argument.public static double floorPowerBaseOfDouble(double base, double n)
floorPowerOf2(int)
for values
less than one. I.e., if n < 1, the result is 1.
The formula is: basefloor(logbase(x))
base
- The number in the expression ⌊basen⌋.n
- The input argument.public static double log2(double value)
value
- the given valuepublic static double logBaseOfX(double base, double x)
base
- The number in the expression log(x) / log(base).x
- the given valuepublic static int numberOfTrailingOnes(long v)
v
- the value whose number of trailing ones is to be computed.public static int numberOfLeadingOnes(long v)
v
- the value whose number of leading ones is to be computed.public static int exactLog2OfInt(int powerOf2, String argName)
powerOf2
- must be a power of 2 and greater than zero.argName
- the argument name used in the exception if thrown.SketchesArgumentException
- if not a power of 2 nor greater than zero.public static int exactLog2OfLong(long powerOf2, String argName)
powerOf2
- must be a power of 2 and greater than zero.argName
- the argument name used in the exception if thrown.SketchesArgumentException
- if not a power of 2 nor greater than zero.public static int exactLog2OfInt(int powerOf2)
powerOf2
- must be a power of 2 and greater than zero.public static int exactLog2OfLong(long powerOf2)
powerOf2
- must be a power of 2 and greater than zero.public static void checkBounds(long reqOff, long reqLen, long allocSize)
0 <= reqOff <= reqLen <= reqOff + reqLen <= allocSize
.
If this equation is violated an SketchesArgumentException
will be thrown.reqOff
- the requested offsetreqLen
- the requested lengthallocSize
- the allocated size.public static void checkProbability(double p, String argName)
p
- See Sampling Probability, pargName
- Used in the thrown exception.public static boolean isLessThanUnsigned(long n1, long n2)
n1
- A long to be treated as if unsigned.n2
- A long to be treated as if unsigned.public static boolean isEven(long n)
n
- the given npublic static boolean isOdd(long n)
n
- the given npublic static final int bitAt(long number, int bitPos)
number
- the number to examinebitPos
- the given zero-based bit position, where the least significant
bit is at position zero.public static int numDigits(long n)
n
- the given numberpublic static String longToFixedLengthString(long number, int length)
For example, assume a sequence of integers from 1 to 1000. The largest value has four decimal digits. Convert the entire sequence of strings to the form " 1" to "1000". When these strings are sorted they will be in numerical sequence: " 1", " 2", ... "1000".
number
- the given numberlength
- the desired string length.public static <T> Object minT(Object item1, Object item2, Comparator<? super T> c)
T
- the typeitem1
- item oneitem2
- item twoc
- the given comparatorpublic static <T> Object maxT(Object item1, Object item2, Comparator<? super T> c)
T
- the typeitem1
- item oneitem2
- item twoc
- the given comparatorpublic static <T> boolean lt(Object item1, Object item2, Comparator<? super T> c)
T
- the typeitem1
- item oneitem2
- item twoc
- the given comparatorpublic static <T> boolean le(Object item1, Object item2, Comparator<? super T> c)
T
- the typeitem1
- item oneitem2
- item twoc
- the given comparatorCopyright © 2015–2024 The Apache Software Foundation. All rights reserved.