public class Common extends Object
| Modifier and Type | Field and Description |
|---|---|
static int |
DAY |
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 int |
count(String[] s,
String key)
How many times String key was in the String array s.
|
static int |
count(String s,
char c)
Count char c from String s, returns int of how many times char c was used.
|
static int |
count(String s,
String search) |
static void |
debug(java.util.function.Supplier<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(java.util.function.Supplier<String> s) |
static char |
first(String s) |
static <T> T |
first(T[] t) |
static <T> T |
get(java.util.function.Supplier<T> f)
Evaluates the given function and return the result.
|
static String |
getByteSizeString(long numOfBytes)
Get bytes in long, and returns human-readable format with 2 digit decimal.
|
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(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 string
|
static String |
getNth(String s,
char delim,
int index)
Returns index element from the String s when separated by char delim.
|
static String |
getStringBetween(String s,
String prefix,
String suffix)
Get a String between String prefix and String suffix from String s.
|
static long |
getTimed(Runnable r,
int count)
Run lambda function Runnable r for count times.
|
static void |
info(java.util.function.Supplier<String> s) |
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) |
static void |
main(String[] args) |
static void |
parseArgs(String[] args,
java.util.function.BiConsumer<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) |
static String |
removePrefix(String s,
String prefix) |
static String |
removeSuffix(String s,
char suffix) |
static String |
removeSuffix(String s,
String suffix) |
static String |
repeat(char c,
int n)
Take a char c and repeat it n times, store it as a String.
|
static String |
repeat(String s,
int n) |
static String |
rtrim(String s) |
static boolean |
sleep(long ms)
Shortcut function that calls Thread.sleep() but does not require try/catch.
|
static String |
substring(String s,
int to) |
static String |
substring(String s,
int idxFrom,
int idxTo)
A static alternative substring method.
|
static void |
warn(java.util.function.Supplier<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 String repeat(char c, int n)
c - char to repeatn - number of repeatspublic static int count(String s, char c)
s - String s to be checked for char cc - char c to lookuppublic static int count(String[] s, String key)
s - String arraykey - String to search from the arraypublic 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 - idxFrom - index of substring fromidxTo - index of substring to; when its value is 0, it means until end of the string.public static String getStringBetween(String s, String prefix, String suffix)
String s = "https://gonn.org/test";
getStringBetween(s, "https://", "/test"); // returns gonn.org
s - String fromprefix - prefix to searchsuffix - suffix to searchpublic static String getNth(String s, char delim, int index)
String s = "Gon|Yi|41|Conway|AR|72034";
getNth(s, '|', 3) // returns Conway
s - input String sdelim - delimiter char to separateindex - an index to search from the separated String s.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 long getEpoch()
public static String epochToString(long epoch, int offsetHr, boolean signed)
epoch - Epoch millisecondsoffsetHr - 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)
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 - number to evaluatepublic static String getByteSizeString(long numOfBytes)
numOfBytes - of a file or memory in byte (B).public static boolean getConfig(String key, boolean fallback)
key - property/variable namefallback - default fallback valuepublic static void parseArgs(String[] args, java.util.function.BiConsumer<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 void debug(java.util.function.Supplier<String> s)
public static void info(java.util.function.Supplier<String> s)
public static void warn(java.util.function.Supplier<String> s)
public static void error(java.util.function.Supplier<String> s)
public static <T> T get(java.util.function.Supplier<T> f)
boolean xb = Gava.get(()->true);
String xs = Gava.get(()->"blah");
int xi = Gava.get(()->123);
T - anyf - supplier function that takes type T.public static int getHash(String s)
public static void main(String[] args)
Copyright © 2023. All rights reserved.