Add ifCan utility method

This commit is contained in:
2024-03-29 08:26:40 -05:00
parent d0de637dee
commit f3efb341fc
2 changed files with 42 additions and 0 deletions

View File

@ -139,4 +139,28 @@ public class ObjectUtils
return (b);
}
/*******************************************************************************
** Utility to test a chained unsafe expression CAN get to the end and return true.
**
** e.g., instead of:
** if(a && a.b && a.b.c && a.b.c.d)
** we can do:
** if(ifCan(() -> a.b.c.d))
**
** Note - if the supplier returns null, that counts as false!
*******************************************************************************/
public static boolean ifCan(UnsafeSupplier<Boolean, ? extends Throwable> supplier)
{
try
{
return supplier.get();
}
catch(Throwable t)
{
return (false);
}
}
}