Last active
January 24, 2022 18:01
-
-
Save esabook/a58b0a33d8c99f5b9b19e8be14276307 to your computer and use it in GitHub Desktop.
Execute code with silent crash mode;Ignore throwable runtime code;Can be used for Getter or Setter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Execute code with silent crash mode<br> | |
* usage:<br> | |
* <pre> | |
* Ignore.of(()-> { | |
* String bankName = o.infoPayment.namaBank.toUpperCase().replace("BANK", ""); | |
* binding.tvBankName.setText(bankName); | |
* }); | |
* </pre> | |
* <p>or</p> | |
* <pre> | |
* Ignore.of( ()-> Integer.parseInt(""), | |
* ()-> getClass().getSuperclass().cast(null), | |
* ()-> {\/*other operation*\/} | |
* ); | |
* | |
* | |
* int o = Ignore.get(() -> 3232/0, 0); | |
* </pre> | |
*/ | |
public final class Ignore { | |
private Ignore(Runner runner) { | |
try { | |
runner.run(); | |
} catch (Exception ignore) { | |
if (BuildConfig.DEBUG) | |
Timber.w(ignore); | |
} | |
} | |
public static void of(Runner... runner) { | |
for (Runner r : runner) { | |
new Ignore(r); | |
} | |
} | |
public static <T> T get(final UncheckedFunction<T> function, T defaultValue) { | |
try { | |
return function.call(); | |
} catch (Exception e) { | |
Timber.w(e); | |
return defaultValue; | |
} | |
} | |
public static <T> T getOrNull(final UncheckedFunction<T>... function) { | |
for (UncheckedFunction<T> f : function) { | |
T ret = getOrNull(f); | |
if (ret != null) return ret; | |
} | |
return null; | |
} | |
public static <T> T getOrNull(final UncheckedFunction<T> function) { | |
return get(function, null); | |
} | |
@FunctionalInterface | |
public interface Runner { | |
void run() throws Exception; | |
} | |
@FunctionalInterface | |
public interface UncheckedFunction<T> { | |
T call() throws Exception; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add adjustment in kotlin version