public class Utils extends Object
| Constructor and Description |
|---|
Utils() |
| Modifier and Type | Method and Description |
|---|---|
static String |
bytesToReadable(double numOfBytes)
Display the number of bytes in a human readable way.
|
static Throwable |
catchThrowable(NonArgFunction function)
Simplify the codes to catch an unchecked exception, usually the RuntimeException.
|
static Thread |
createAndStartThread(Runnable r)
Simplify this common practice to one line.
|
static String |
getSubString(String str,
String delimiter,
int n1,
int n2)
Get the sub String between the n1th and the n2th delimiter
index begins from 0
E.g.
|
static void |
ifElse(boolean flag,
NonArgFunction fnc1,
NonArgFunction fnc2)
If flag is true, run function1, else, run function2
Simplify the usually 5 lines code to one line
|
static void |
ifNotNull(Object obj,
NonArgFunction fnc)
If obj is not null, run fnc.
|
static void |
ifThen(boolean flag,
NonArgFunction fnc)
If flag is true, run function.
|
static int |
intsSum(int... numbers)
get the sum value of an array of int numbers.
|
static String |
numberToReadableString(long num)
returns the human readable String of a number with proper units
|
static void |
printlnWithThreadName(String message)
println the message with the thread name prefixed.
|
static void |
repeatRun(int times,
Consumer<Integer> c)
Simplify the code to repeatedly run a piece of code for N times.
|
static void |
repeatRun(int times,
NonArgFunction function)
Simplifies the code to repeatedly run a piece of code for N times.
|
static void |
threadJoin(Thread thr)
Make the codes for thread.join() in one line by
swallowing the InterruptedException (just call e.printStackTrace())
|
static void |
threadSleep(long millis)
Make the codes for Thread.sleep(millis) in one line by
swallowing the InterruptedException (just call e.printStackTrace())
|
public static void printlnWithThreadName(String message)
public static Throwable catchThrowable(NonArgFunction function)
function - public static void repeatRun(int times,
NonArgFunction function)
times - function - public static void repeatRun(int times,
Consumer<Integer> c)
times - function - public static String bytesToReadable(double numOfBytes)
numOfBytes - public static String numberToReadableString(long num)
num - public static void threadSleep(long millis)
millis - public static Thread createAndStartThread(Runnable r)
r - the Runnable to run in the newly created threadpublic static void threadJoin(Thread thr)
the - thread to joinpublic static String getSubString(String str, String delimiter, int n1, int n2)
public static void ifNotNull(Object obj, NonArgFunction fnc)
obj - fnc - public static void ifThen(boolean flag,
NonArgFunction fnc)
flag - fnc - public static void ifElse(boolean flag,
NonArgFunction fnc1,
NonArgFunction fnc2)
flag - fnc1 - fnc2 - public static int intsSum(int... numbers)
numbers - Copyright © 2018. All rights reserved.