public static < T > T precondition ( T reference , Predicate < T > predicate , Class < ? extends RuntimeException > ex , String msg ) throws RuntimeException { if ( predicate . test ( reference ) ) { return reference ; } final Constructor < ? extends RuntimeException > constructor ; try { constructor = ex . getConstructor ( String . class ) ; throw constructor . newInstance ( msg ) ; } catch ( RuntimeException e ) { throw e ; } catch ( Exception e ) { throw new RuntimeException ( msg ) ; } }
Test a Predicate against a reference value and if it fails throw a runtime exception with a message .