|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectvoldemort.utils.Utils
public class Utils
Helper functions FTW!
Nested Class Summary | |
---|---|
static interface |
Utils.Posix
|
Field Summary | |
---|---|
static java.util.regex.Pattern |
COMMA_SEP
Pattern for splitting a string based on commas |
static java.lang.String |
NEWLINE
|
Constructor Summary | |
---|---|
Utils()
|
Method Summary | ||
---|---|---|
static void |
assertNotNull(java.lang.Object... objects)
Throw an IllegalArgumentException if any of the given objects are null |
|
static void |
assertNotNull(java.lang.String s,
java.lang.Object... objects)
Throw an IllegalArgumentException if any of the given objects are null |
|
static
|
asSortedList(java.util.Collection<T> c)
Sort a collection to a List |
|
static double |
avgDoubleList(java.util.List<java.lang.Double> list)
Compute the average of a Double list |
|
static long |
avgLongList(java.util.List<java.lang.Long> list)
Compute the average of a Long list |
|
static
|
compareList(java.util.List<T> listA,
java.util.List<T> listB)
Compares two lists |
|
static void |
croak(java.lang.String message)
Print an error and exit with error code 1 |
|
static void |
croak(java.lang.String message,
int errorCode)
Print an error and exit with the given error code |
|
static boolean |
deepEquals(java.lang.Object o1,
java.lang.Object o2)
Determines if two objects are equal as determined by Object.equals(Object) , or "deeply equal" if both are arrays. |
|
static int |
deepHashCode(java.lang.Object o)
Gets hash code of an object, optionally returns hash code based on the "deep contents" of array if the object is an array. |
|
static java.util.List<java.lang.Integer> |
distributeEvenlyIntoList(int listLength,
int numElements)
This method returns a list that "evenly" (within one) distributes some number of elements (peanut butter) over some number of buckets (bread slices). |
|
static java.util.Map<java.lang.Integer,java.lang.Integer> |
distributeEvenlyIntoMap(java.util.Set<java.lang.Integer> mapKeys,
int numElements)
This method returns a map that "evenly" (within one) distributes some number of elements (peanut butter) over some number of buckets (bread slices). |
|
static long |
elapsedTimeNs(long startNs,
long endNs)
Specifically, this utility is to address the fact that System.nanoTime() can sometimes go backwards, due to the fact that it relies on the performance counters |
|
static
|
getAddedInTarget(java.util.Set<T> current,
java.util.Set<T> target)
Returns a set of objects that were added to the target list getAddedInTarget(current, null) - nothing was added, returns null. |
|
static java.util.GregorianCalendar |
getCalendarForNextRun(java.util.GregorianCalendar startTime,
int targetDay,
int targetHour)
Given a start time, computes the next time when the wallclock will reach a certain hour of the day, on a certain day of the week Eg: From today, when is the next Saturday, 12PM ? |
|
static int |
getDayOfTheWeekFromNow(int nDays)
Returns the day of week, 'nDays' from today |
|
static
|
getDeletedInTarget(java.util.Set<T> current,
java.util.Set<T> target)
Returns a set of objects that were deleted in the target set getDeletedInTarget(current, null) - everything was deleted, returns current. |
|
static
|
getEnumFromString(java.lang.Class<T> c,
java.lang.String string)
A common method for all enums since they can't have another base class |
|
static int |
inRange(int value,
int min,
int max)
Return the value v if min <= v <= max, otherwise throw an exception |
|
static boolean |
isReadableDir(java.io.File d)
|
|
static boolean |
isReadableDir(java.lang.String dirName)
|
|
static boolean |
isReadableFile(java.io.File f)
|
|
static boolean |
isReadableFile(java.lang.String fileName)
|
|
static boolean |
isSymLink(java.io.File symlinkFile)
Check if a file is a symbolic link or not |
|
static void |
mkdirs(java.io.File newDir)
Make the directory specified in the parameters. |
|
static void |
move(java.io.File source,
java.io.File dest)
Move the source file to the dest file name. |
|
static java.util.List<java.lang.Integer> |
nodeListToNodeIdList(java.util.List<Node> nodes)
Given a list of nodes, retrieves the list of node ids |
|
static
|
notNull(T t)
Throw an IllegalArgumentException if the argument is null, otherwise just return the argument. |
|
static
|
notNull(T t,
java.lang.String message)
Throw an IllegalArgumentException if the argument is null, otherwise just return the argument. |
|
static java.lang.String |
paddedString(java.lang.String str,
int totalWidth)
|
|
static java.net.URI |
parseUri(java.lang.String uri)
A helper function that wraps the checked parsing exception when creating a URI |
|
static java.util.List<java.lang.Integer> |
removeItemsToSplitListEvenly(java.util.List<java.lang.Integer> inputList,
int maxContiguous)
This method breaks the inputList into distinct lists that are no longer than maxContiguous in length. |
|
static
|
reversed(java.util.List<T> l)
A reversed copy of the given list |
|
static void |
rm(java.io.File file)
Delete the given file |
|
static void |
rm(java.io.File[] files)
Delete an array of files |
|
static void |
rm(java.lang.Iterable<java.io.File> files)
Delete all the given files |
|
static void |
rm(java.lang.String file)
Delete the given file |
|
static int |
safeGetPercentage(float rawNum,
float total)
Computes the percentage, taking care of division by 0. |
|
static double |
safeGetPercentage(long rawNum,
long total)
Computes the percentage, taking care of division by 0 |
|
static
|
sorted(java.util.List<T> l)
Return a copy of the list sorted according to the natural order |
|
static
|
sorted(java.util.List<T> l,
java.util.Comparator<T> comparator)
Return a copy of the list sorted according to the given comparator |
|
static double |
sumDoubleList(java.util.List<java.lang.Double> list)
Compute the sum of a Double list |
|
static long |
sumLongList(java.util.List<java.lang.Long> list)
Computes sum of a Long list |
|
static void |
symlink(java.lang.String filePath,
java.lang.String symLinkPath)
Create a symbolic link to an existing file. |
|
static
|
uncheckedCast(T2 t2)
|
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String NEWLINE
public static final java.util.regex.Pattern COMMA_SEP
Constructor Detail |
---|
public Utils()
Method Detail |
---|
public static void croak(java.lang.String message)
message
- The error to printpublic static void croak(java.lang.String message, int errorCode)
message
- The error to printerrorCode
- The error code to exit withpublic static void rm(java.io.File file)
file
- The file to deletepublic static void rm(java.io.File[] files)
files
- Files to deletepublic static void rm(java.lang.String file)
file
- The file to deletepublic static void rm(java.lang.Iterable<java.io.File> files)
files
- A collection of files to deletepublic static <T extends java.lang.Comparable<? super T>> java.util.List<T> asSortedList(java.util.Collection<T> c)
collection
- to be converted to a sorted listpublic static void symlink(java.lang.String filePath, java.lang.String symLinkPath)
filePath
- Path of the file for whom to create the symbolic linksymLinkPath
- Path of the symbolic linkpublic static void move(java.io.File source, java.io.File dest)
source
- The file to copy fromdest
- The file to copy topublic static void mkdirs(java.io.File newDir)
newDir
- The directory we want to makepublic static boolean isReadableFile(java.lang.String fileName)
public static boolean isReadableFile(java.io.File f)
public static boolean isReadableDir(java.lang.String dirName)
public static boolean isReadableDir(java.io.File d)
public static void assertNotNull(java.lang.Object... objects)
objects
- The objects to testpublic static void assertNotNull(java.lang.String s, java.lang.Object... objects)
s
- The error message to giveobjects
- The objects to testpublic static <T> T notNull(T t, java.lang.String message)
T
- The type of the thingt
- The thing to check for nullness.message
- The message to put in the exception if it is nullpublic static <T> T notNull(T t)
T
- The type of the thingt
- The thing to check for nullness.public static int inRange(int value, int min, int max)
value
- The value to checkmin
- The minimum allowable valuemax
- The maximum allowable value
public static double safeGetPercentage(long rawNum, long total)
public static int safeGetPercentage(float rawNum, float total)
public static long sumLongList(java.util.List<java.lang.Long> list)
Long
list
list
-
public static long avgLongList(java.util.List<java.lang.Long> list)
Long
list
list
-
public static double sumDoubleList(java.util.List<java.lang.Double> list)
Double
list
list
-
public static double avgDoubleList(java.util.List<java.lang.Double> list)
Double
list
list
-
public static int deepHashCode(java.lang.Object o)
If o
is null, 0 is returned; if o
is an array, the
corresponding Arrays.deepHashCode(Object[])
, or
Arrays.hashCode(int[])
or the like is used to calculate the hash
code.
public static boolean deepEquals(java.lang.Object o1, java.lang.Object o2)
Object.equals(Object)
, or "deeply equal" if both are arrays.
If both objects are null, true is returned; if both objects are array,
the corresponding Arrays.deepEquals(Object[], Object[])
, or
Arrays.equals(int[], int[])
or the like are called to determine
equality.
Note that this method does not "deeply" compare the fields of the objects.
public static <T> java.util.Set<T> getAddedInTarget(java.util.Set<T> current, java.util.Set<T> target)
current
- Set of objects present in currenttarget
- Set of partitions present in target
public static <T> java.util.Set<T> getDeletedInTarget(java.util.Set<T> current, java.util.Set<T> target)
current
- Set of objects currently presenttarget
- Set of target objects
public static <T> java.util.List<T> sorted(java.util.List<T> l, java.util.Comparator<T> comparator)
T
- The type of the elements in the listl
- The list to sortcomparator
- The comparator to use for sorting
public static <T extends java.lang.Comparable<T>> java.util.List<T> sorted(java.util.List<T> l)
T
- The type of the elements in the listl
- The list to sort
public static <T> java.util.List<T> reversed(java.util.List<T> l)
T
- The type of the items in the listl
- The list to reverse
public static <T> boolean compareList(java.util.List<T> listA, java.util.List<T> listB)
T
- The type of items in the listlistA
- List 1listB
- List 2
public static java.net.URI parseUri(java.lang.String uri)
uri
- The URI to parse
public static java.lang.String paddedString(java.lang.String str, int totalWidth)
public static <T1,T2> T1 uncheckedCast(T2 t2)
public static boolean isSymLink(java.io.File symlinkFile)
symlinkFile
-
public static java.util.GregorianCalendar getCalendarForNextRun(java.util.GregorianCalendar startTime, int targetDay, int targetHour)
startTime
- start timetargetDay
- day of the week to choosetargetHour
- hour of the day to choose
public static int getDayOfTheWeekFromNow(int nDays)
public static <T extends java.lang.Enum<T>> T getEnumFromString(java.lang.Class<T> c, java.lang.String string)
T
- Enum typec
- enum type. All enums must be all caps.string
- case insensitive
public static long elapsedTimeNs(long startNs, long endNs)
startNs
- endNs
-
public static java.util.List<java.lang.Integer> removeItemsToSplitListEvenly(java.util.List<java.lang.Integer> inputList, int maxContiguous)
inputList
- The list to be broken into separate runs.maxContiguous
- The upper limit on sub-list size
public static java.util.List<java.lang.Integer> distributeEvenlyIntoList(int listLength, int numElements)
listLength
- The number of buckets over which to evenly distribute
the elements.numElements
- The number of elements to distribute.
public static java.util.Map<java.lang.Integer,java.lang.Integer> distributeEvenlyIntoMap(java.util.Set<java.lang.Integer> mapKeys, int numElements)
mapKeys
- The keys of the map over which which to evenly distribute
the elements.numElements
- The number of elements to distribute.
public static java.util.List<java.lang.Integer> nodeListToNodeIdList(java.util.List<Node> nodes)
nodes
- The list of nodes
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |