Skip to content

Instantly share code, notes, and snippets.

@esabook
Last active January 24, 2022 18:01
Show Gist options
  • Save esabook/a58b0a33d8c99f5b9b19e8be14276307 to your computer and use it in GitHub Desktop.
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
/**
* 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;
}
}
@esabook
Copy link
Author

esabook commented Jan 24, 2022

Add adjustment in kotlin version

class Ignore private constructor(runner: Runner) {


    init {
        try {
            runner.run()
        } catch (ignore: Exception) {
            if (BuildConfig.DEBUG)
                Timber.w(ignore)
        }
    }

    fun interface Runner {
        @Throws(Exception::class)
        fun run()
    }

    fun interface UncheckedFunction<T> {
        @Throws(Exception::class)
        fun call(): T
    }

    companion object {

        /**
         * Serializing multiple call until last of `then()`
         * ```
         * Ignore.of{throw NullPointerException()}
         *       .then{val a = 1/0}
         *       .then{pre = "falseTrue".toBooleanStrict()}
         *       .then{pre = true}
         * ```
         * @return Ignore
         */
        fun Ignore.then(runner: Runner): Ignore = of(runner)

        /**
         * Serializing multiple call until last of `item`
         * ```
         * Ignore.of(
         *            {throw NullPointerException()},
         *            {val a = 1/0},
         *            {pre = "falseTrue".toBooleanStrict()}
         *            )
         * ```
         */
        @JvmStatic
        fun of(vararg runner: Runner) {
            for (r in runner) {
                Ignore(r)
            }
        }

        /**
         * Shorthand of try-catch, without dummy catch(){}
         * ```Ignore.of{val a = 1/0}```
         */
        @JvmStatic
        fun of(runner: Runner): Ignore {
            return Ignore(runner)
        }

        /**
         * ```
         * val defaultValue = true
         * val pre: Boolean = Ignore[{"null".toBooleanStrict()}, defaultValue]
         * ```
         */
        @JvmStatic
        operator fun <T> get(
            function: UncheckedFunction<T>,
            @androidx.annotation.Nullable defaultValue: T
        ): T {
            return try {
                function.call()
            } catch (e: Exception) {
                Timber.w(e)
                defaultValue
            }
        }

        /**
         * ```
         * val defaultValue = true
         * val pre: Boolean = Ignore[{"null".toBooleanStrict()}, defaultValue]
         * ```
         */
        @JvmStatic
        operator fun get(function: UncheckedFunction<Boolean>, defaultValue: Boolean): Boolean {
            return try {
                function.call()
            } catch (e: Exception) {
                Timber.w(e)
                defaultValue
            }
        }

        /**
         * get first nonNull result, from @see getOrNull{}
         * 
         * ```
         * val defaultValue = true
         * val actualValue = false
         * val pre: Boolean = Ignore.getOrNull(
         *     {"null".toBooleanStrictOrNull()!!},
         *     {null},
         *     {null as Boolean},
         *     {actualValue}
         *     )?: defaultValue
         *     ```
         */
        @JvmStatic
        fun <T> getOrNull(vararg function: UncheckedFunction<T>): T? {
            for (f in function) {
                val ret: T? = getOrNull(f)
                if (ret != null) return ret
            }
            return null
        }

        /**
         * get runtime result of T, or null if throw occurred
         * ```
         * val defaultValue = true
         * val pre: Boolean = Ignore.getOrNull{"null".toBooleanStrictOrNull()!!} ?: defaultValue
         * ```
         */
        @JvmStatic
        fun <T> getOrNull(function: UncheckedFunction<T>): T? {
            return try {
                function.call()
            } catch (e: Exception) {
                null
            }
        }
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment