voldemort.utils
Class Utils

java.lang.Object
  extended by voldemort.utils.Utils

public class Utils
extends java.lang.Object

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
<T extends java.lang.Comparable<? super T>>
java.util.List<T>
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
<T> boolean
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
<T> java.util.Set<T>
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
<T> java.util.Set<T>
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
<T extends java.lang.Enum<T>>
T
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
<T> T
notNull(T t)
          Throw an IllegalArgumentException if the argument is null, otherwise just return the argument.
static
<T> T
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
<T> java.util.List<T>
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
<T extends java.lang.Comparable<T>>
java.util.List<T>
sorted(java.util.List<T> l)
          Return a copy of the list sorted according to the natural order
static
<T> java.util.List<T>
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
<T1,T2> T1
uncheckedCast(T2 t2)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NEWLINE

public static final java.lang.String NEWLINE

COMMA_SEP

public static final java.util.regex.Pattern COMMA_SEP
Pattern for splitting a string based on commas

Constructor Detail

Utils

public Utils()
Method Detail

croak

public static void croak(java.lang.String message)
Print an error and exit with error code 1

Parameters:
message - The error to print

croak

public static void croak(java.lang.String message,
                         int errorCode)
Print an error and exit with the given error code

Parameters:
message - The error to print
errorCode - The error code to exit with

rm

public static void rm(java.io.File file)
Delete the given file

Parameters:
file - The file to delete

rm

public static void rm(java.io.File[] files)
Delete an array of files

Parameters:
files - Files to delete

rm

public static void rm(java.lang.String file)
Delete the given file

Parameters:
file - The file to delete

rm

public static void rm(java.lang.Iterable<java.io.File> files)
Delete all the given files

Parameters:
files - A collection of files to delete

asSortedList

public static <T extends java.lang.Comparable<? super T>> java.util.List<T> asSortedList(java.util.Collection<T> c)
Sort a collection to a List

Parameters:
collection - to be converted to a sorted list

symlink

public static void symlink(java.lang.String filePath,
                           java.lang.String symLinkPath)
Create a symbolic link to an existing file. Also deletes the existing symbolic link if it exists

Parameters:
filePath - Path of the file for whom to create the symbolic link
symLinkPath - Path of the symbolic link

move

public static void move(java.io.File source,
                        java.io.File dest)
Move the source file to the dest file name. If there is a file or directory at dest it will be overwritten. If the source file does not exist or cannot be copied and exception will be thrown exist

Parameters:
source - The file to copy from
dest - The file to copy to

mkdirs

public static void mkdirs(java.io.File newDir)
Make the directory specified in the parameters. If it exists, see if we can write to it

Parameters:
newDir - The directory we want to make

isReadableFile

public static boolean isReadableFile(java.lang.String fileName)
Returns:
true iff the argument is the name of a readable file

isReadableFile

public static boolean isReadableFile(java.io.File f)
Returns:
true iff the argument is a readable file

isReadableDir

public static boolean isReadableDir(java.lang.String dirName)
Returns:
true iff the argument is the name of a readable directory

isReadableDir

public static boolean isReadableDir(java.io.File d)
Returns:
true iff the argument is a readable directory

assertNotNull

public static void assertNotNull(java.lang.Object... objects)
Throw an IllegalArgumentException if any of the given objects are null

Parameters:
objects - The objects to test

assertNotNull

public static void assertNotNull(java.lang.String s,
                                 java.lang.Object... objects)
Throw an IllegalArgumentException if any of the given objects are null

Parameters:
s - The error message to give
objects - The objects to test

notNull

public static <T> T notNull(T t,
                            java.lang.String message)
Throw an IllegalArgumentException if the argument is null, otherwise just return the argument. Useful for assignment as in this.thing = Utils.notNull(thing);

Type Parameters:
T - The type of the thing
Parameters:
t - The thing to check for nullness.
message - The message to put in the exception if it is null

notNull

public static <T> T notNull(T t)
Throw an IllegalArgumentException if the argument is null, otherwise just return the argument. Useful for assignment as in this.thing = Utils.notNull(thing);

Type Parameters:
T - The type of the thing
Parameters:
t - The thing to check for nullness.

inRange

public static int inRange(int value,
                          int min,
                          int max)
Return the value v if min <= v <= max, otherwise throw an exception

Parameters:
value - The value to check
min - The minimum allowable value
max - The maximum allowable value
Returns:
The value, if it is in the range

safeGetPercentage

public static double safeGetPercentage(long rawNum,
                                       long total)
Computes the percentage, taking care of division by 0


safeGetPercentage

public static int safeGetPercentage(float rawNum,
                                    float total)
Computes the percentage, taking care of division by 0.

Returns:
number between 0-100+

sumLongList

public static long sumLongList(java.util.List<java.lang.Long> list)
Computes sum of a Long list

Parameters:
list -
Returns:
sum of the list

avgLongList

public static long avgLongList(java.util.List<java.lang.Long> list)
Compute the average of a Long list

Parameters:
list -
Returns:

sumDoubleList

public static double sumDoubleList(java.util.List<java.lang.Double> list)
Compute the sum of a Double list

Parameters:
list -
Returns:

avgDoubleList

public static double avgDoubleList(java.util.List<java.lang.Double> list)
Compute the average of a Double list

Parameters:
list -
Returns:

deepHashCode

public 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.

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.


deepEquals

public 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.

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.


getAddedInTarget

public static <T> java.util.Set<T> 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.
getAddedInTarget(null, target) - everything in target was added, return target.
getAddedInTarget(null, null) - neither added nor deleted, return null.
getAddedInTarget(current, target)) - returns new partition not found in current.

Parameters:
current - Set of objects present in current
target - Set of partitions present in target
Returns:
A set of added partitions in target or empty set

getDeletedInTarget

public static <T> java.util.Set<T> 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.
getDeletedInTarget(null, target) - everything in target was added, return target.
getDeletedInTarget(null, null) - neither added nor deleted, return empty set.
getDeletedInTarget(current, target)) - returns deleted partition not found in target.

Parameters:
current - Set of objects currently present
target - Set of target objects
Returns:
A set of deleted objects in target or empty set

sorted

public static <T> java.util.List<T> sorted(java.util.List<T> l,
                                           java.util.Comparator<T> comparator)
Return a copy of the list sorted according to the given comparator

Type Parameters:
T - The type of the elements in the list
Parameters:
l - The list to sort
comparator - The comparator to use for sorting
Returns:
A sorted copy of the list

sorted

public static <T extends java.lang.Comparable<T>> java.util.List<T> sorted(java.util.List<T> l)
Return a copy of the list sorted according to the natural order

Type Parameters:
T - The type of the elements in the list
Parameters:
l - The list to sort
Returns:
A sorted copy of the list

reversed

public static <T> java.util.List<T> reversed(java.util.List<T> l)
A reversed copy of the given list

Type Parameters:
T - The type of the items in the list
Parameters:
l - The list to reverse
Returns:
The list, reversed

compareList

public static <T> boolean compareList(java.util.List<T> listA,
                                      java.util.List<T> listB)
Compares two lists

Type Parameters:
T - The type of items in the list
Parameters:
listA - List 1
listB - List 2
Returns:
Returns a boolean comparing the lists

parseUri

public static java.net.URI parseUri(java.lang.String uri)
A helper function that wraps the checked parsing exception when creating a URI

Parameters:
uri - The URI to parse
Returns:
a URI object.

paddedString

public static java.lang.String paddedString(java.lang.String str,
                                            int totalWidth)

uncheckedCast

public static <T1,T2> T1 uncheckedCast(T2 t2)

isSymLink

public static boolean isSymLink(java.io.File symlinkFile)
Check if a file is a symbolic link or not

Parameters:
symlinkFile -
Returns:
true if File is symlink else false

getCalendarForNextRun

public 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 ?

Parameters:
startTime - start time
targetDay - day of the week to choose
targetHour - hour of the day to choose
Returns:
calendar object representing the target time

getDayOfTheWeekFromNow

public static int getDayOfTheWeekFromNow(int nDays)
Returns the day of week, 'nDays' from today

Returns:
Calendar constant representing the day of the week

getEnumFromString

public static <T extends java.lang.Enum<T>> T getEnumFromString(java.lang.Class<T> c,
                                                                java.lang.String string)
A common method for all enums since they can't have another base class

Type Parameters:
T - Enum type
Parameters:
c - enum type. All enums must be all caps.
string - case insensitive
Returns:
corresponding enum, or null

elapsedTimeNs

public 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

Parameters:
startNs -
endNs -
Returns:
0 if endNs < startNs, delta otherwise

removeItemsToSplitListEvenly

public 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. It does so by removing elements from the inputList. This method removes the minimum necessary items to achieve the goal. This method chooses items to remove that minimize the length of the maximum remaining run. E.g. given an inputList of 20 elements and maxContiguous=8, this method will return the 2 elements that break the inputList into 3 runs of 6 items. (As opposed to 2 elements that break the inputList into two runs of eight items and one run of two items.)

Parameters:
inputList - The list to be broken into separate runs.
maxContiguous - The upper limit on sub-list size
Returns:
A list of Integers to be removed from inputList to achieve the maxContiguous goal.

distributeEvenlyIntoList

public 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).

Parameters:
listLength - The number of buckets over which to evenly distribute the elements.
numElements - The number of elements to distribute.
Returns:
A list of size breadSlices, each integer entry of which indicates the number of elements.

distributeEvenlyIntoMap

public 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).

Parameters:
mapKeys - The keys of the map over which which to evenly distribute the elements.
numElements - The number of elements to distribute.
Returns:
A Map with keys specified by breadSlices each integer entry of which indicates the number of elements

nodeListToNodeIdList

public static java.util.List<java.lang.Integer> nodeListToNodeIdList(java.util.List<Node> nodes)
Given a list of nodes, retrieves the list of node ids

Parameters:
nodes - The list of nodes
Returns:
Returns a list of node ids


Jay Kreps, Roshan Sumbaly, Alex Feinberg, Bhupesh Bansal, Lei Gao, Chinmay Soman, Vinoth Chandar, Zhongjie Wu