Skip to content

Commit

Permalink
Adding in verifiers for the Instant and Duration types
Browse files Browse the repository at this point in the history
  • Loading branch information
littleclay committed Dec 8, 2016
1 parent 99f2b2e commit 587d818
Show file tree
Hide file tree
Showing 7 changed files with 495 additions and 31 deletions.
2 changes: 1 addition & 1 deletion 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>2.1.0</version>
<version>2.2.0</version>
</dependency>
```

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>1.0-SNAPSHOT</version>
<version>2.2.0</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>

Expand Down
91 changes: 62 additions & 29 deletions src/main/java/com/redfin/validity/VerifiableFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
import com.redfin.validity.verifiers.objects.VerifiableClass;
import com.redfin.validity.verifiers.objects.VerifiableCollection;
import com.redfin.validity.verifiers.objects.VerifiableDouble;
import com.redfin.validity.verifiers.objects.VerifiableDuration;
import com.redfin.validity.verifiers.objects.VerifiableFloat;
import com.redfin.validity.verifiers.objects.VerifiableInstant;
import com.redfin.validity.verifiers.objects.VerifiableInteger;
import com.redfin.validity.verifiers.objects.VerifiableLong;
import com.redfin.validity.verifiers.objects.VerifiableObject;
Expand All @@ -45,6 +47,9 @@
import com.redfin.validity.verifiers.primitives.VerifiablePrimitiveInt;
import com.redfin.validity.verifiers.primitives.VerifiablePrimitiveLong;
import com.redfin.validity.verifiers.primitives.VerifiablePrimitiveShort;

import java.time.Duration;
import java.time.Instant;
import java.util.Collection;

/**
Expand Down Expand Up @@ -242,6 +247,10 @@ public VerifiablePrimitiveShort<X> that(short subject) {
// Objects
// - - - - - - - - - - - - - - - - - - - - - -

// - - - - - - - - - - - - - - - - -
// Boxed Primitive Types
// - - - - - - - - - - - - - - - - -

/**
* @param subject the object to perform validation on.
* @return a {@link VerifiableBoolean} instance for the given subject.
Expand All @@ -266,25 +275,6 @@ public VerifiableCharacter<X> that(Character subject) {
return new VerifiableCharacter<>(failedValidationExecutor, subject, message);
}

/**
* @param subject the object to perform validation on.
* @param <T> the class being validated.
* @return a {@link VerifiableClass} instance for the given subject.
*/
public <T> VerifiableClass<T, X> that(Class<T> subject) {
return new VerifiableClass<>(failedValidationExecutor, subject, message);
}

/**
* @param subject the object to perform validation on.
* @param <E> the type of the objects in the collection.
* @param <T> the type of the Collection (e.g. list, map, etc).
* @return a {@link VerifiableCollection} instance for the given subject.
*/
public <E, T extends Collection<E>> VerifiableCollection<E, T, X> that(T subject) {
return new VerifiableCollection<>(failedValidationExecutor, subject, message);
}

/**
* @param subject the object to perform validation on.
* @return a {@link VerifiableDouble} instance for the given subject.
Expand Down Expand Up @@ -318,23 +308,54 @@ public VerifiableLong<X> that(Long subject) {
}

/**
* This is the default object validation for subjects that
* don't match any of the other pre-defined types.
*
* @param subject the object to perform validation on.
* @param <T> the type of the subject.
* @return a {@link VerifiableObject} instance for the given subject.
* @return a {@link VerifiableShort} instance for the given subject.
*/
public <T> VerifiableObject<T, X> that(T subject) {
return new VerifiableObject<>(failedValidationExecutor, subject, message);
public VerifiableShort<X> that(Short subject) {
return new VerifiableShort<>(failedValidationExecutor, subject, message);
}

// - - - - - - - - - - - - - - - - -
// Time Object Types
// - - - - - - - - - - - - - - - - -

/**
* @param subject the object to perform validation on.
* @return a {@link VerifiableShort} instance for the given subject.
* @return a {@link VerifiableDuration} instance for the given subject.
*/
public VerifiableShort<X> that(Short subject) {
return new VerifiableShort<>(failedValidationExecutor, subject, message);
public VerifiableDuration<X> that(Duration subject) {
return new VerifiableDuration<>(failedValidationExecutor, subject, message);
}

/**
* @param subject the object to perform validation on.
* @return a {@link VerifiableInstant} instance for the given subject.
*/
public VerifiableInstant<X> that(Instant subject) {
return new VerifiableInstant<>(failedValidationExecutor, subject, message);
}

// - - - - - - - - - - - - - - - - -
// Other Object Types
// - - - - - - - - - - - - - - - - -

/**
* @param subject the object to perform validation on.
* @param <T> the class being validated.
* @return a {@link VerifiableClass} instance for the given subject.
*/
public <T> VerifiableClass<T, X> that(Class<T> subject) {
return new VerifiableClass<>(failedValidationExecutor, subject, message);
}

/**
* @param subject the object to perform validation on.
* @param <E> the type of the objects in the collection.
* @param <T> the type of the Collection (e.g. list, map, etc).
* @return a {@link VerifiableCollection} instance for the given subject.
*/
public <E, T extends Collection<E>> VerifiableCollection<E, T, X> that(T subject) {
return new VerifiableCollection<>(failedValidationExecutor, subject, message);
}

/**
Expand All @@ -345,6 +366,18 @@ public VerifiableString<X> that(String subject) {
return new VerifiableString<>(failedValidationExecutor, subject, message);
}

/**
* This is the default object validation for subjects that
* don't match any of the other pre-defined types.
*
* @param subject the object to perform validation on.
* @param <T> the type of the subject.
* @return a {@link VerifiableObject} instance for the given subject.
*/
public <T> VerifiableObject<T, X> that(T subject) {
return new VerifiableObject<>(failedValidationExecutor, subject, message);
}

// --------------------------------------------------------------
// Not a value type enforcement
// --------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.verifiers.objects;

import com.redfin.validity.FailedValidationExecutor;
import com.redfin.validity.verifiers.AbstractVerifiableComparable;
import com.redfin.validity.verifiers.AbstractVerifiableObject;

import java.time.Duration;

/**
* Concrete class for verifying {@link Duration} subjects.
*
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableDuration<X extends Throwable> extends AbstractVerifiableComparable<Duration, X> {

/**
* Create a new {@link AbstractVerifiableObject} instance with the given values.
*
* @param failedValidationExecutor the {@link FailedValidationExecutor} to be called
* on validation failure.
* May not be null.
* @param subject the subject to be validated.
* May be null.
* @param message the String custom message to pre-pend a failure with.
* May be null.
*
* @throws NullPointerException if failedValidationExecutor is null.
*/
public VerifiableDuration(FailedValidationExecutor<X> failedValidationExecutor, Duration subject, String message) {
super(failedValidationExecutor, subject, message);
}

/**
* @return the subject if it is zero.
* @throws X if the subject is null or is not zero.
*/
public Duration isZero() throws X {
Duration subject = getSubject();
if (null == subject || !subject.isZero()) {
fail("t -> t.isZero()");
}
return subject;
}

/**
* @return the subject if it is not zero.
* @throws X if the subject is null or is zero.
*/
public Duration isNotZero() throws X {
Duration subject = getSubject();
if (null == subject || subject.isZero()) {
fail("t -> !t.isZero()");
}
return subject;
}

/**
* @return the subject if it is not zero and negative.
* @throws X if the subject is null, is zero, or is positive.
*/
public Duration isStrictlyNegative() throws X {
Duration subject = getSubject();
if (null == subject || !subject.isNegative()) {
fail("t -> t.isNegative()");
}
return subject;
}

/**
* @return the subject if it is not zero and positive.
* @throws X if the subject is null, is zero, or is negative.
*/
public Duration isStrictlyPositive() throws X {
Duration subject = getSubject();
if (null == subject || subject.isZero() || subject.isNegative()) {
fail("t -> !t.isZero() && !t.isNegative()");
}
return subject;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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.verifiers.objects;

import com.redfin.validity.FailedValidationExecutor;
import com.redfin.validity.verifiers.AbstractVerifiableComparable;

import java.time.Instant;

/**
* Concrete class for verifying {@link Instant} subjects.
*
* @param <X> the type of {@link Throwable} to be thrown on validation failure.
*/
public final class VerifiableInstant<X extends Throwable> extends AbstractVerifiableComparable<Instant, X> {

/**
* Create a new {@link AbstractVerifiableComparable} instance with the given values.
*
* @param failedValidationExecutor the {@link FailedValidationExecutor} to be called
* on validation failure.
* May not be null.
* @param subject the subject to be validated.
* May be null.
* @param message the String custom message to pre-pend a failure with.
* May be null.
*
* @throws NullPointerException if failedValidationExecutor is null.
*/
public VerifiableInstant(FailedValidationExecutor<X> failedValidationExecutor, Instant subject, String message) {
super(failedValidationExecutor, subject, message);
}
}
Loading

0 comments on commit 587d818

Please sign in to comment.