/** * <p>Evaluate the given {@code predicate} using {@code value} as input.</p> * * <p>The function throws {@link InvariantViolationException} if the predicate * is false.</p> * * @param value The value * @param condition The predicate * @param <T> The type of values * * @return value * * @throws InvariantViolationException If the predicate is false */ public static <T> T checkInvariant( final T value, final ContractConditionType<T> condition) throws InvariantViolationException { return checkInvariant(value, condition.predicate(), condition.describer()); }
/** * <p>Evaluate the given {@code predicate} using {@code value} as input.</p> * * <p>The function throws {@link InvariantViolationException} if the predicate * is false.</p> * * @param value The value * @param condition The predicate * @param <T> The type of values * * @return value * * @throws InvariantViolationException If the predicate is false */ public static <T> T checkInvariant( final T value, final ContractConditionType<T> condition) throws InvariantViolationException { return checkInvariant(value, condition.predicate(), condition.describer()); }
/** * <p>Evaluate the given {@code predicate} using {@code value} as input.</p> * * <p>The function throws {@link PreconditionViolationException} if the * predicate is false.</p> * * @param value The value * @param condition The predicate * @param <T> The type of values * * @return value * * @throws PreconditionViolationException If the predicate is false */ public static <T> T checkPrecondition( final T value, final ContractConditionType<T> condition) throws PreconditionViolationException { return checkPrecondition( value, condition.predicate(), condition.describer()); }
/** * <p>Evaluate the given {@code predicate} using {@code value} as input.</p> * * <p>The function throws {@link PostconditionViolationException} if the * predicate is false.</p> * * @param value The value * @param condition The predicate * @param <T> The type of values * * @return value * * @throws PostconditionViolationException If the predicate is false */ public static <T> T checkPostcondition( final T value, final ContractConditionType<T> condition) throws PostconditionViolationException { return checkPostcondition( value, condition.predicate(), condition.describer()); }
/** * Fill a builder with attribute values from the provided {@code ContractConditionType} instance. * Regular attribute values will be replaced with those from the given instance. * Absent optional values will not replace present values. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ public final Builder<T> from(ContractConditionType<T> instance) { Objects.requireNonNull(instance, "instance"); setPredicate(instance.predicate()); setDescriber(instance.describer()); return this; }
/** * <p>Evaluate the given {@code predicate} using {@code value} as input.</p> * * <p>The function throws {@link PostconditionViolationException} if the * predicate is false.</p> * * @param value The value * @param condition The predicate * @param <T> The type of values * * @return value * * @throws PostconditionViolationException If the predicate is false */ public static <T> T checkPostcondition( final T value, final ContractConditionType<T> condition) throws PostconditionViolationException { return checkPostcondition( value, condition.predicate(), condition.describer()); }
/** * Fill a builder with attribute values from the provided {@code ContractConditionType} instance. * Regular attribute values will be replaced with those from the given instance. * Absent optional values will not replace present values. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ public final Builder<T> from(ContractConditionType<T> instance) { Objects.requireNonNull(instance, "instance"); setPredicate(instance.predicate()); setDescriber(instance.describer()); return this; }
/** * <p>Evaluate the given {@code predicate} using {@code value} as input.</p> * * <p>The function throws {@link PreconditionViolationException} if the * predicate is false.</p> * * @param value The value * @param condition The predicate * @param <T> The type of values * * @return value * * @throws PreconditionViolationException If the predicate is false */ public static <T> T checkPrecondition( final T value, final ContractConditionType<T> condition) throws PreconditionViolationException { return checkPrecondition( value, condition.predicate(), condition.describer()); }
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; }
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; }