static Violations maybeAllocate( final Violations violations, final int count) { return violations == null ? new Violations(count) : violations; }
private static <T> PostconditionViolationException failed( final T value, final Violations violations) { final StringBuilder sb = new StringBuilder(128); sb.append("Postcondition violation."); sb.append(System.lineSeparator()); sb.append(" Received: "); sb.append(value); sb.append(System.lineSeparator()); sb.append(" Violated conditions: "); sb.append(System.lineSeparator()); final String[] messages = violations.messages(); for (int index = 0; index < messages.length; ++index) { if (messages[index] != null) { sb.append(" ["); sb.append(index); sb.append("]: "); sb.append(messages[index]); sb.append(System.lineSeparator()); } } throw new PostconditionViolationException( sb.toString(), violations.count()); }
static <T> Violations innerCheckAll( final T value, final ContractConditionType<T>[] conditions) { Violations violations = null; for (int index = 0; index < conditions.length; ++index) { final ContractConditionType<T> condition = conditions[index]; final Predicate<T> predicate = condition.predicate(); final boolean ok; final int count = conditions.length; try { ok = predicate.test(value); } catch (final Throwable e) { violations = maybeAllocate(violations, count); violations.messages()[index] = failedPredicate(e); violations.countUp(); continue; } if (!ok) { violations = maybeAllocate(violations, count); violations.messages()[index] = applyDescriberChecked(value, condition.describer()); violations.countUp(); } } return violations; }
/** * A specialized version of {@link #checkPrecondition(Object, boolean, * Function)} that does not mention an input value. * * @param condition The predicate * @param message The predicate description * * @throws PreconditionViolationException Iff {@code predicate == false} */ public static void checkPrecondition( final boolean condition, final String message) throws PreconditionViolationException { if (!condition) { throw failed("<unspecified>", singleViolation(message)); } }
/** * A {@code long} specialized version of {@link #checkPreconditions(Object, * ContractConditionType[])} * * @param value The value * @param conditions The conditions the value must obey * * @return value * * @throws PreconditionViolationException If any of the conditions are false */ public static long checkPreconditionsL( final long value, final ContractLongConditionType... conditions) throws PreconditionViolationException { final Violations violations = innerCheckAllLong(value, conditions); if (violations != null) { throw failed(Long.valueOf(value), violations); } return value; }
/** * A {@code double} specialized version of {@link #checkPostconditions(Object, * ContractConditionType[])} * * @param value The value * @param conditions The conditions the value must obey * * @return value * * @throws PostconditionViolationException If any of the conditions are false */ public static double checkPostconditionsD( final double value, final ContractDoubleConditionType... conditions) throws PostconditionViolationException { final Violations violations = innerCheckAllDouble(value, conditions); if (violations != null) { throw failed(Double.valueOf(value), violations); } return value; }
/** * An {@code int} specialized version of {@link #checkPreconditions(Object, * ContractConditionType[])} * * @param value The value * @param conditions The conditions the value must obey * * @return value * * @throws PreconditionViolationException If any of the conditions are false */ public static int checkPreconditionsI( final int value, final ContractIntConditionType... conditions) throws PreconditionViolationException { final Violations violations = innerCheckAllInt(value, conditions); if (violations != null) { throw failed(Integer.valueOf(value), violations); } return value; }
/** * <p>Evaluate all of the given {@code conditions} using {@code value} as * input.</p> * * <p>All of the conditions are evaluated and the function throws {@link * PreconditionViolationException} if any of the conditions are false, or * raise an exception that is not of type {@link Error}. Exceptions of type * {@link Error} are propagated immediately, without any further contract * checking.</p> * * @param value The value * @param conditions The set of conditions * @param <T> The type of values * * @return value * * @throws PreconditionViolationException If any of the conditions are false */ @SafeVarargs public static <T> T checkPreconditions( final T value, final ContractConditionType<T>... conditions) throws PreconditionViolationException { final Violations violations = innerCheckAll(value, conditions); if (violations != null) { throw failed(value, violations); } return value; }
/** * A specialized version of {@link #checkPrecondition(Object, boolean, * Function)} that does not mention an input value. * * @param condition The predicate * @param message The predicate description * * @throws PreconditionViolationException Iff {@code predicate == false} */ public static void checkPrecondition( final boolean condition, final String message) throws PreconditionViolationException { if (!condition) { throw failed("<unspecified>", singleViolation(message)); } }
/** * A {@code long} specialized version of {@link #checkPostconditions(Object, * ContractConditionType[])} * * @param value The value * @param conditions The conditions the value must obey * * @return value * * @throws PostconditionViolationException If any of the conditions are false */ public static long checkPostconditionsL( final long value, final ContractLongConditionType... conditions) throws PostconditionViolationException { final Violations violations = innerCheckAllLong(value, conditions); if (violations != null) { throw failed(Long.valueOf(value), violations); } return value; }
/** * A {@code double} specialized version of {@link #checkPostconditions(Object, * ContractConditionType[])} * * @param value The value * @param conditions The conditions the value must obey * * @return value * * @throws PostconditionViolationException If any of the conditions are false */ public static double checkPostconditionsD( final double value, final ContractDoubleConditionType... conditions) throws PostconditionViolationException { final Violations violations = innerCheckAllDouble(value, conditions); if (violations != null) { throw failed(Double.valueOf(value), violations); } return value; }
/** * An {@code int} specialized version of {@link #checkPreconditions(Object, * ContractConditionType[])} * * @param value The value * @param conditions The conditions the value must obey * * @return value * * @throws PreconditionViolationException If any of the conditions are false */ public static int checkPreconditionsI( final int value, final ContractIntConditionType... conditions) throws PreconditionViolationException { final Violations violations = innerCheckAllInt(value, conditions); if (violations != null) { throw failed(Integer.valueOf(value), violations); } return value; }
/** * <p>Evaluate all of the given {@code conditions} using {@code value} as * input.</p> * * <p>All of the conditions are evaluated and the function throws {@link * InvariantViolationException} if any of the conditions are false, or raise * an exception that is not of type {@link Error}. Exceptions of type {@link * Error} are propagated immediately, without any further contract * checking.</p> * * @param value The value * @param conditions The set of conditions * @param <T> The type of values * * @return value * * @throws InvariantViolationException If any of the conditions are false */ @SafeVarargs public static <T> T checkInvariants( final T value, final ContractConditionType<T>... conditions) throws InvariantViolationException { final Violations violations = innerCheckAll(value, conditions); if (violations != null) { throw failed(value, violations); } return value; }
static <T> Violations innerCheckAll( final T value, final ContractConditionType<T>[] conditions) { Violations violations = null; for (int index = 0; index < conditions.length; ++index) { final ContractConditionType<T> condition = conditions[index]; final Predicate<T> predicate = condition.predicate(); final boolean ok; final int count = conditions.length; try { ok = predicate.test(value); } catch (final Throwable e) { violations = maybeAllocate(violations, count); violations.messages()[index] = failedPredicate(e); violations.countUp(); continue; } if (!ok) { violations = maybeAllocate(violations, count); violations.messages()[index] = applyDescriberChecked(value, condition.describer()); violations.countUp(); } } return violations; }
/** * A specialized version of {@link #checkInvariant(Object, boolean, Function)} * that does not mention an input value. * * @param condition The predicate * @param message The predicate description * * @throws InvariantViolationException Iff {@code predicate == false} */ public static void checkInvariant( final boolean condition, final String message) throws InvariantViolationException { if (!condition) { throw failed("<unspecified>", singleViolation(message)); } }
private static <T> PostconditionViolationException failed( final T value, final Violations violations) { final StringBuilder sb = new StringBuilder(128); sb.append("Postcondition violation."); sb.append(System.lineSeparator()); sb.append(" Received: "); sb.append(value); sb.append(System.lineSeparator()); sb.append(" Violated conditions: "); sb.append(System.lineSeparator()); final String[] messages = violations.messages(); for (int index = 0; index < messages.length; ++index) { if (messages[index] != null) { sb.append(" ["); sb.append(index); sb.append("]: "); sb.append(messages[index]); sb.append(System.lineSeparator()); } } throw new PostconditionViolationException( sb.toString(), violations.count()); }
/** * A {@code long} specialized version of {@link #checkPostconditions(Object, * ContractConditionType[])} * * @param value The value * @param conditions The conditions the value must obey * * @return value * * @throws PostconditionViolationException If any of the conditions are false */ public static long checkPostconditionsL( final long value, final ContractLongConditionType... conditions) throws PostconditionViolationException { final Violations violations = innerCheckAllLong(value, conditions); if (violations != null) { throw failed(Long.valueOf(value), violations); } return value; }
/** * A {@code double} specialized version of {@link #checkPreconditions(Object, * ContractConditionType[])} * * @param value The value * @param conditions The conditions the value must obey * * @return value * * @throws PreconditionViolationException If any of the conditions are false */ public static double checkPreconditionsD( final double value, final ContractDoubleConditionType... conditions) throws PreconditionViolationException { final Violations violations = innerCheckAllDouble(value, conditions); if (violations != null) { throw failed(Double.valueOf(value), violations); } return value; }
/** * An {@code int} specialized version of {@link #checkInvariants(Object, * ContractConditionType[])} * * @param value The value * @param conditions The conditions the value must obey * * @return value * * @throws InvariantViolationException If any of the conditions are false */ public static int checkInvariantsI( final int value, final ContractIntConditionType... conditions) throws InvariantViolationException { final Violations violations = innerCheckAllInt(value, conditions); if (violations != null) { throw failed(Integer.valueOf(value), violations); } return value; }
/** * <p>Evaluate all of the given {@code conditions} using {@code value} as * input.</p> * * <p>All of the conditions are evaluated and the function throws {@link * PostconditionViolationException} if any of the conditions are false, or * raise an exception that is not of type {@link Error}. Exceptions of type * {@link Error} are propagated immediately, without any further contract * checking.</p> * * @param value The value * @param conditions The set of conditions * @param <T> The type of values * * @return value * * @throws PostconditionViolationException If any of the conditions are false */ @SafeVarargs public static <T> T checkPostconditions( final T value, final ContractConditionType<T>... conditions) throws PostconditionViolationException { final Violations violations = innerCheckAll(value, conditions); if (violations != null) { throw failed(value, violations); } return value; }