Skip to content

Commit

Permalink
Adding in the expect method and updating some formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
littleclay committed Oct 3, 2017
1 parent 6815896 commit 5ede0ec
Show file tree
Hide file tree
Showing 86 changed files with 364 additions and 89 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ To install, you can simply include the dependency from Maven Central:
<dependency>
<groupId>com.redfin</groupId>
<artifactId>validity</artifactId>
<version>4.0.1</version>
<version>4.1.0</version>
</dependency>
```

Expand All @@ -39,8 +39,11 @@ Be careful when using primitive boolean validation that they return the given su
For example, `validate.that(false).isFalse()` will return `false`, not true.
If the validation were to fail then it wouldn't return false, but would rather throw an exception.

For best effect, you should statically import the static `Validity` method entry point.
For best effect, you should statically import the static `Validity` method entry points.
Note that the `validate()` method will throw `IllegalArgumentException`s upon validation failure.
Failures after calling the `expect()` method will throw `IllegalStateException`s.
```java
import static com.redfin.validity.Validity.expect;
import static com.redfin.validity.Validity.validate;
```

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<groupId>com.redfin</groupId>
<artifactId>validity</artifactId>
<version>4.0.1</version>
<version>4.1.0</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
* @param <F> the type of the implementing sub-class.
*/
public abstract class AbstractVerifiableFactory<X extends Throwable,
F extends AbstractVerifiableFactory<X, F>> {
F extends AbstractVerifiableFactory<X, F>> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
* @param <X> the type of {@link Throwable} the {@link #fail(String, Object, String)} method
* will throw.
*/
public final class DefaultValidityFailedValidationExecutor<X extends Throwable> implements FailedValidationExecutor<X> {
public final class DefaultValidityFailedValidationExecutor<X extends Throwable>
implements FailedValidationExecutor<X> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Constants
Expand Down
49 changes: 49 additions & 0 deletions src/main/java/com/redfin/validity/ExpectVerifiableFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright: (c) 2016 Redfin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.redfin.validity;

/**
* A default implementation of the {@link AbstractVerifiableFactory} for the Validity library
* for throwing IllegalStateExceptions.
*/
public final class ExpectVerifiableFactory
extends AbstractVerifiableFactory<IllegalStateException, ExpectVerifiableFactory> {

/**
* Create a new {@link AbstractVerifiableFactory} instance with the given message and failed validation
* executor.
*
* @param message the String message to pre-pend the failure message with, if necessary.
* May be null.
* @param failedValidationExecutor the {@link FailedValidationExecutor} to use in case
* of failed validation.
* May not be null.
*
* @throws NullPointerException if failedValidationExecutor is null.
*/
public ExpectVerifiableFactory(String message,
FailedValidationExecutor<IllegalStateException> failedValidationExecutor) {
super(message, failedValidationExecutor);
}

@Override
protected ExpectVerifiableFactory getFactory(String message,
FailedValidationExecutor<IllegalStateException> failedValidationExecutor) {
return new ExpectVerifiableFactory(message,
failedValidationExecutor);
}
}
20 changes: 16 additions & 4 deletions src/main/java/com/redfin/validity/Validity.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ public final class Validity {
*/

private static final FailedValidationExecutor<IllegalArgumentException> VERIFY_FAILURE;
private static final ValidityVerifiableFactory NO_MESSAGE_INSTANCE;
private static final FailedValidationExecutor<IllegalStateException> EXPECT_FAILURE;
private static final ValidityVerifiableFactory NO_MESSAGE_VERIFY_INSTANCE;
private static final ExpectVerifiableFactory NO_MESSAGE_EXPECT_INSTANCE;

static {
VERIFY_FAILURE = new DefaultValidityFailedValidationExecutor<>(IllegalArgumentException::new);
NO_MESSAGE_INSTANCE = new ValidityVerifiableFactory(null, VERIFY_FAILURE);
NO_MESSAGE_VERIFY_INSTANCE = new ValidityVerifiableFactory(null, VERIFY_FAILURE);
EXPECT_FAILURE = new DefaultValidityFailedValidationExecutor<>(IllegalStateException::new);
NO_MESSAGE_EXPECT_INSTANCE = new ExpectVerifiableFactory(null, EXPECT_FAILURE);
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -46,10 +50,18 @@ public final class Validity {

/**
* @return a {@link Validity} instance with the default message
* prefix.
* prefix that throws {@link IllegalArgumentException}s on failure.
*/
public static ValidityVerifiableFactory validate() {
return NO_MESSAGE_INSTANCE;
return NO_MESSAGE_VERIFY_INSTANCE;
}

/**
* @return a {@link Validity} instance with the default message prefix
* that throws {@link IllegalStateException}s on failure.
*/
public static ExpectVerifiableFactory expect() {
return NO_MESSAGE_EXPECT_INSTANCE;
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
* A class implementing the {@link DoublePredicate} interface.
* This allows for the {@link DoublePredicate} behavior but with a nice, human-readable toString output.
*/
public final class DescriptiveDoublePredicate extends AbstractDescriptivePredicate implements DoublePredicate {
public final class DescriptiveDoublePredicate
extends AbstractDescriptivePredicate
implements DoublePredicate {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
* A class implementing the {@link IntPredicate} interface.
* This allows for the {@link IntPredicate} behavior but with a nice, human-readable toString output.
*/
public final class DescriptiveIntPredicate extends AbstractDescriptivePredicate implements IntPredicate {
public final class DescriptiveIntPredicate
extends AbstractDescriptivePredicate
implements IntPredicate {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
* A class implementing the {@link LongPredicate} interface.
* This allows for the {@link LongPredicate} behavior but with a nice, human-readable toString output.
*/
public final class DescriptiveLongPredicate extends AbstractDescriptivePredicate implements LongPredicate {
public final class DescriptiveLongPredicate
extends AbstractDescriptivePredicate
implements LongPredicate {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
*
* @param <T> the type that this predicate will test.
*/
public final class DescriptivePredicate<T> extends AbstractDescriptivePredicate implements Predicate<T> {
public final class DescriptivePredicate<T>
extends AbstractDescriptivePredicate
implements Predicate<T> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Fields
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
* Must be a {@link Comparable} type.
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public abstract class AbstractVerifiableComparable<T extends Comparable<T>, X extends Throwable> extends AbstractVerifiableObject<T, X> {
public abstract class AbstractVerifiableComparable<T extends Comparable<T>, X extends Throwable>
extends AbstractVerifiableObject<T, X> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Instance Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
* @param <T> the type of the subject being validated.
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public abstract class AbstractVerifiableComparableNumber<T extends Number & Comparable<T>, X extends Throwable> extends AbstractVerifiableComparable<T, X> {
public abstract class AbstractVerifiableComparableNumber<T extends Number & Comparable<T>, X extends Throwable>
extends AbstractVerifiableComparable<T, X> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Instance Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
*
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableArray<E, X extends Throwable> extends AbstractVerifiableObject<E[], X> {
public final class VerifiableArray<E, X extends Throwable>
extends AbstractVerifiableObject<E[], X> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Instance Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
*
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableBooleanArray<X extends Throwable> extends AbstractVerifiableObject<boolean[], X> {
public final class VerifiableBooleanArray<X extends Throwable>
extends AbstractVerifiableObject<boolean[], X> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Instance Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
*
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableByteArray<X extends Throwable> extends AbstractVerifiableObject<byte[], X> {
public final class VerifiableByteArray<X extends Throwable>
extends AbstractVerifiableObject<byte[], X> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Instance Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
*
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableCharArray<X extends Throwable> extends AbstractVerifiableObject<char[], X> {
public final class VerifiableCharArray<X extends Throwable>
extends AbstractVerifiableObject<char[], X> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Instance Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
*
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableDoubleArray<X extends Throwable> extends AbstractVerifiableObject<double[], X> {
public final class VerifiableDoubleArray<X extends Throwable>
extends AbstractVerifiableObject<double[], X> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Instance Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
*
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableFloatArray<X extends Throwable> extends AbstractVerifiableObject<float[], X> {
public final class VerifiableFloatArray<X extends Throwable>
extends AbstractVerifiableObject<float[], X> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Instance Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
*
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableIntArray<X extends Throwable> extends AbstractVerifiableObject<int[], X> {
public final class VerifiableIntArray<X extends Throwable>
extends AbstractVerifiableObject<int[], X> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Instance Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
*
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableLongArray<X extends Throwable> extends AbstractVerifiableObject<long[], X> {
public final class VerifiableLongArray<X extends Throwable>
extends AbstractVerifiableObject<long[], X> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Instance Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
*
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableShortArray<X extends Throwable> extends AbstractVerifiableObject<short[], X> {
public final class VerifiableShortArray<X extends Throwable>
extends AbstractVerifiableObject<short[], X> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Instance Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
*
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableBoolean<X extends Throwable> extends AbstractVerifiableObject<Boolean, X> {
public final class VerifiableBoolean<X extends Throwable>
extends AbstractVerifiableObject<Boolean, X> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Instance Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
*
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableByte<X extends Throwable> extends AbstractVerifiableComparableNumber<Byte, X> {
public final class VerifiableByte<X extends Throwable>
extends AbstractVerifiableComparableNumber<Byte, X> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Constants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
*
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableCharacter<X extends Throwable> extends AbstractVerifiableComparable<Character, X> {
public final class VerifiableCharacter<X extends Throwable>
extends AbstractVerifiableComparable<Character, X> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Instance Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
* @param <T> the type of the class object being verified.
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableClass<T, X extends Throwable> extends AbstractVerifiableObject<Class<T>, X> {
public final class VerifiableClass<T, X extends Throwable>
extends AbstractVerifiableObject<Class<T>, X> {

/**
* Create a new {@link VerifiableClass} instance with the given values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
*
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableCollection<E, T extends Collection<E>, X extends Throwable> extends AbstractVerifiableObject<T, X> {
public final class VerifiableCollection<E, T extends Collection<E>, X extends Throwable>
extends AbstractVerifiableObject<T, X> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Instance Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
*
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableDouble<X extends Throwable> extends AbstractVerifiableComparableNumber<Double, X> {
public final class VerifiableDouble<X extends Throwable>
extends AbstractVerifiableComparableNumber<Double, X> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Constants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
*
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableDuration<X extends Throwable> extends AbstractVerifiableComparable<Duration, X> {
public final class VerifiableDuration<X extends Throwable>
extends AbstractVerifiableComparable<Duration, X> {

/**
* Create a new {@link AbstractVerifiableObject} instance with the given values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
*
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableFloat<X extends Throwable> extends AbstractVerifiableComparableNumber<Float, X> {
public final class VerifiableFloat<X extends Throwable>
extends AbstractVerifiableComparableNumber<Float, X> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Constants
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
*
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableInstant<X extends Throwable> extends AbstractVerifiableComparable<Instant, X> {
public final class VerifiableInstant<X extends Throwable>
extends AbstractVerifiableComparable<Instant, X> {

/**
* Create a new {@link AbstractVerifiableComparable} instance with the given values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
*
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableInteger<X extends Throwable> extends AbstractVerifiableComparableNumber<Integer, X> {
public final class VerifiableInteger<X extends Throwable>
extends AbstractVerifiableComparableNumber<Integer, X> {

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Constants
Expand Down
Loading

0 comments on commit 5ede0ec

Please sign in to comment.