public class Stu extends Object
| Modifier and Type | Field and Description |
|---|---|
static int |
DAY |
static String[] |
EMPTY_STRING_ARRAY |
static String |
ENV_VAR_VERBOSE
Default params set when any of the method was first used.
|
static long |
EPOCH_STARTED |
static int |
HOUR |
static int |
MINUTE |
static int |
SECOND |
static boolean |
VERBOSE_MODE |
| Modifier and Type | Method and Description |
|---|---|
static String |
byteSizeToString(long sizeInByte)
Get bytes in long, and returns human-readable format with 2 digit decimal.
|
static int |
count(String[] s,
String key)
How many times String key was in the String array s.
|
static int |
count(String haystack,
char needle)
Count char c from String s, returns int of how many times char c was used.
|
static int |
count(String haystack,
String needle)
Count how many times the needle is in haystack.
|
static void |
debug(Fx01<String> s) |
static String |
epochToString()
Time since EPOCH_STARTED in string format
|
static String |
epochToString(long epochMillis)
Returns UTC time from epochMillis.
|
static String |
epochToString(long epoch,
int offsetHr)
Return epoch time with offset.
|
static String |
epochToString(long epoch,
int offsetHr,
boolean signed)
Converts epoch milliseconds to human-readable time format.
|
static void |
error(Fx01<String> s) |
static <R> R |
eval(Fx01<R> evalFn)
Evaluate the function without a param
(This is just to reduce code.)
|
static <T1,T2,R> R |
eval(T1 t1,
T2 t2,
Fx21<T1,T2,R> evalFn)
Evaluate the function with a param.
|
static <T,R> R |
eval(T t,
Fx11<T,R> evalFn)
Evaluate the function with a param.
|
static <T,R> R |
evalX(T t,
FxThrow<T,R> evalXFn,
Fx11<Exception,R> fallbackFn)
Evaluate a given function with a param.
|
static char |
first(String s) |
static <T> T |
first(T[] t) |
static <T> void |
forEach(T[] src,
Fx10<T> fx)
Evaluate each T in T[].
|
static <T> void |
forEachSet(T[] src,
FxUnary<T> fx)
Modify array using fx
|
static String |
getBetween(String s,
String prefix,
String suffix)
Get a String between String prefix and String suffix from String s.
|
static String |
getCaller()
Returns caller information in "filename:lineNumber" format.
|
static StackTraceElement |
getCaller(int skip)
Get a StackTraceElement of the caller to find out who called this function.
|
static String |
getConfig(String key) |
static boolean |
getConfig(String key,
boolean fallback)
Check (1) system properties, and then (2) environmental variables.
|
static String |
getConfig(String key,
String fallback) |
static int |
getDigits(int n) |
static int |
getDigits(long n)
Get an integer and calculate number of digits.
|
static long |
getEpoch()
Returns current epoch milliseconds.
|
static int |
getHash(String s)
Get hash from the input string
|
static String |
getNth(String s,
char delim,
int index)
Returns index element from the String s when separated by char delim.
|
static long |
getTimed(Runnable r,
int count)
Run lambda function Runnable r for count times.
|
static void |
info(Fx01<String> s) |
static boolean |
isBetween(String s,
String prefix,
String suffix)
Check if the input has the prefix AND the suffix.
|
static String |
join(String delimiter,
Object... objs) |
static char |
last(String s) |
static <T> T |
last(T[] t) |
static void |
log(String prefix,
String msg)
Whenever static method log() is called, it will print to System.out.
|
static String |
ltrim(String s)
Left trim the string
|
static void |
main(String[] args) |
static <T> T |
mustGet(T t,
T fallback)
If t is null, return fallback value.
|
static <X extends Throwable,T> |
mustGet(T t,
X exception)
If t is null, throw an exception
|
static void |
parseArgs(String[] args,
Fx20<String,String> f)
Handles args such as `--name="Gon"`
|
static void |
println(Object... any) |
static void |
prints(String name,
Object... objs)
Prints any objects in a line per item.
|
static String |
removePrefix(String s,
char prefix)
Remove prefix from the String s
|
static String |
removePrefix(String s,
String prefix)
Remove prefix from the String s
|
static String |
removeSuffix(String s,
char suffix)
Remove the suffix from the String s
|
static String |
removeSuffix(String s,
String suffix)
Remove the suffix from the String s
|
static String |
repeat(char c,
int n)
Take a char c and repeat it n times.
|
static String |
repeat(String s,
int n)
Repeat the input string for n times.
|
static String |
rtrim(String s)
Right trim the string
|
static boolean |
sleep(long ms)
Shortcut function that calls Thread.sleep() but does not require try/catch.
|
static String[] |
subset(String[] src,
int start,
int end)
Creates a subset of String array.
|
static String |
substring(String s,
int idx)
Return the string until index idx.
|
static String |
substring(String s,
int idxFrom,
int idxTo)
A static alternative substring method.
|
static void |
warn(Fx01<String> s) |
public static final String ENV_VAR_VERBOSE
public static final long EPOCH_STARTED
public static final boolean VERBOSE_MODE
public static final int SECOND
public static final int MINUTE
public static final int HOUR
public static final int DAY
public static final String[] EMPTY_STRING_ARRAY
public static String[] subset(String[] src, int start, int end)
E.g.
subset(src, 0, -3) creates a subset from the beggining of array EXCEPT last 3.
subset(src, -3, 0) creates a subset of last 3.
subset(src, 1, 3) creates a subset of 2nd and 3rd (idx 2 and 3) item.
src - Source String arraystart - Index fromend - Index endpublic static String repeat(char c, int n)
c - char to repeatn - number of repeatspublic static String repeat(String s, int n)
s - A string to be repeatedn - Number of repeatspublic static int count(String haystack, char needle)
haystack - String s to be checked for char cneedle - char c to lookuppublic static int count(String haystack, String needle)
haystack - A string to be evaluated.needle - A string to be found.public static int count(String[] s, String key)
s - String arraykey - String to search from the arraypublic static String rtrim(String s)
s - Input stringpublic static String ltrim(String s)
s - Input stringpublic static String substring(String s, int idxFrom, int idxTo)
String s = "hello Gon";
substring(s, -3, 0); // returns Gon
substring(s, -3, -1); // returns Go
s - input stringidxFrom - index of substring fromidxTo - index of substring to; when its value is 0, it means until end of the string.public static String substring(String s, int idx)
s - Input stringidx - Index of the string. This can be negative number as well.public static String getBetween(String s, String prefix, String suffix)
String s = "https://gonn.org/test";
getStringBetween(s, "https://", "/test"); // returns gonn.org
s - Input stringprefix - A prefix to searchsuffix - A suffix to searchpublic static boolean isBetween(String s, String prefix, String suffix)
s - Input stringprefix - A prefix stringsuffix - A suffix stringpublic static String getNth(String s, char delim, int index)
String s = "Gon|Yi|41|Conway|AR|72034";
getNth(s, '|', 3); // returns Conway
s - An input stringdelim - A delimiter char to separateindex - An index to search from the separated input.public static <T> T first(T[] t)
public static char first(String s)
public static <T> T last(T[] t)
public static char last(String s)
public static String removePrefix(String s, String prefix)
s - Input stringprefix - A prefix string to removepublic static String removePrefix(String s, char prefix)
s - Input stringprefix - A prefix char to removepublic static String removeSuffix(String s, String suffix)
s - Input stringsuffix - A suffix string to removepublic static String removeSuffix(String s, char suffix)
s - Input stringsuffix - A suffix char to removepublic static long getEpoch()
public static String epochToString(long epoch, int offsetHr, boolean signed)
epoch - Epoch at milliseconds leveloffsetHr - Any offset in hour. (i.g. -6 for CST) If 0 is given, this will return UTC.signed - Whether to show +/- sign.public static String epochToString(long epoch, int offsetHr)
epoch - epoch timeoffsetHr - UTC offset in hour (e.g. -6 for CST)public static String epochToString(long epochMillis)
epochMillis - epoch millisecondpublic static String epochToString()
public static void prints(String name, Object... objs)
name - Name of the groupobjs - Any object(s)public static void println(Object... any)
public static int getDigits(long n)
n - A number to evaluatepublic static int getDigits(int n)
public static String byteSizeToString(long sizeInByte)
sizeInByte - Size of a file or memory in bytes (B).public static boolean getConfig(String key, boolean fallback)
key - property/variable namefallback - default fallback valuepublic static void parseArgs(String[] args, Fx20<String,String> f)
args - String[] as main argsf - BiConsumer function, when single param such as `--disable` is given, 2nd argument for the BiConsumer
will be null.public static StackTraceElement getCaller(int skip)
skip - how many skips requiredpublic static String getCaller()
public static long getTimed(Runnable r, int count)
r - lambda to runcount - how many times to runpublic static boolean sleep(long ms)
ms - millisecondspublic static void log(String prefix, String msg)
if(isVerbose()) log(" [DEBUG] ", "debug!"); // Usage 1 -- completely manually, not recommended.
if(isVerbose()) info("some info here"); // Usage 2 -- recommended when the message isn't static.
prefix - to be added right after the timestamp.msg - message to print.public static <R> R eval(Fx01<R> evalFn)
R - Output typeevalFn - A lambda function returns Rpublic static <T,R> R eval(T t,
Fx11<T,R> evalFn)
T - Input typeR - Output typet - An input value to be evaluatedevalFn - A lambda function takes T and returns R.public static <T1,T2,R> R eval(T1 t1,
T2 t2,
Fx21<T1,T2,R> evalFn)
T1 - Type of first paramT2 - Type of second paramR - Type of the result.t1 - First param to the lambdat2 - Second param to the lambdaevalFn - A lambda eval functionpublic static <T,R> R evalX(T t,
FxThrow<T,R> evalXFn,
Fx11<Exception,R> fallbackFn)
T - Input typeR - Output typet - An input value to be evaluatedevalXFn - A lambda takes T and returns R, also can throw an exception.fallbackFn - A lambda takes an exception, and returns R.public static <T> T mustGet(T t,
T fallback)
T - output typet - value to examfallback - value to return if t is nullpublic static <X extends Throwable,T> T mustGet(T t, X exception) throws X extends Throwable
X - throwable type'T - input typet - value to examexception - exception to throw if t is nullX - if t is nullX extends Throwablepublic static <T> void forEachSet(T[] src,
FxUnary<T> fx)
T - type of the arraysrc - source arrayfx - lambda which modifies the srcpublic static <T> void forEach(T[] src,
Fx10<T> fx)
T - type of the arraysrc - source arrayfx - lambda takes each T.public static int getHash(String s)
s - Input stringpublic static void main(String[] args)
Copyright © 2023. All rights reserved.