Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Collection asserters #1616

Merged
merged 1 commit into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 130 additions & 9 deletions src/main/java/tech/jhipster/lite/error/domain/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

/**
* This class provide utilities for input assertions.
Expand Down Expand Up @@ -61,11 +62,7 @@ public static void notBlank(String field, String input) {
* if the collection is null or empty
*/
public static void notEmpty(String field, Collection<?> collection) {
notNull(field, collection);

if (collection.isEmpty()) {
throw MissingMandatoryValueException.forEmptyValue(field);
}
field(field, collection).notEmpty();
}

/**
Expand Down Expand Up @@ -193,6 +190,29 @@ public static DoubleAsserter field(String field, Double input) {
return new DoubleAsserter(field, input);
}

/**
* Create a fluent asserter for {@link Collection}
*
* <p>
* Usage:
*
* Assert.field("name", name)
* .notEmpty()
* .maxSize(150);
* </pre>
* </code>
* </p>
*
* @param field
* name of the field to check (will be displayed in exception message)
* @param input
* collection to check
* @return A {@link CollectionAsserter} for this field and value
*/
public static <T> CollectionAsserter<T> field(String field, Collection<T> input) {
return new CollectionAsserter<>(field, input);
}

/**
* Create a fluent asserter for an Instant
*
Expand Down Expand Up @@ -376,6 +396,19 @@ private StringAsserter(String field, String value) {
this.value = value;
}

/**
* Ensure that the value is not null
*
* @return The current asserter
* @throws MissingMandatoryValueException
* if the value is null
*/
public StringAsserter notNull() {
Assert.notNull(field, value);

return this;
}

/**
* Ensure that the value is not blank (null, empty or only whitespace)
*
Expand All @@ -384,7 +417,7 @@ private StringAsserter(String field, String value) {
* if the value is blank
*/
public StringAsserter notBlank() {
notNull(field, value);
notNull();

if (value.isBlank()) {
throw MissingMandatoryValueException.forBlankValue(field);
Expand All @@ -394,14 +427,14 @@ public StringAsserter notBlank() {
}

/**
* Ensure that the the input value is at least of the given length
* Ensure that the input value is at least of the given length
*
* @param length
* inclusive min length of the {@link String}
*
* @return The current asserter
* @throws MissingMandatoryValueException
* if the expected length is strceilictly positive and the value is null
* if the expected length is strictly positive and the value is null
* @throws StringTooShortException
* if the value is shorter than min length
*/
Expand All @@ -410,7 +443,7 @@ public StringAsserter minLength(int length) {
return this;
}

notNull(field, value);
notNull();

if (value.length() < length) {
throw StringTooShortException.builder().field(field).value(value).minLength(length).build();
Expand Down Expand Up @@ -843,6 +876,94 @@ private NumberValueTooHighException tooHigh(double ceil) {
}
}

/**
* Asserter dedicated to {@link Collection} assertions
*/
public static class CollectionAsserter<T> {

private final String field;
private final Collection<T> value;

private CollectionAsserter(String field, Collection<T> value) {
this.field = field;
this.value = value;
}

/**
* Ensure that the value is not null
*
* @return The current asserter
* @throws MissingMandatoryValueException
* if the value is null
*/
public CollectionAsserter<T> notNull() {
Assert.notNull(field, value);

return this;
}

/**
* Ensure that the value is not empty (null or empty)
*
* @return The current asserter
* @throws MissingMandatoryValueException
* if the value is null or empty
*/
public CollectionAsserter<T> notEmpty() {
notNull();

if (value.isEmpty()) {
throw MissingMandatoryValueException.forEmptyValue(field);
}

return this;
}

/**
* Ensure that the size of the given input value is not over the given size
*
* @param maxSize
* inclusive max size of the {@link Collection}
* @return The current asserter
* @throws MissingMandatoryValueException
* if the expected size is strictly positive and the value is null
* @throws TooManyElementsException
* if the size of value is over the max size
*/
public CollectionAsserter<T> maxSize(int maxSize) {
if (maxSize <= 0 && value == null) {
return this;
}

notNull();

if (value.size() > maxSize) {
throw TooManyElementsException.builder().field(field).maxSize(maxSize).size(value.size()).build();
}

return this;
}

/**
* Ensure that no element in this {@link Collection} is null
*
* @return The current asserter
* @throws NullElementInCollectionException
* if an element is null
*/
public CollectionAsserter<T> noNullElement() {
if (value == null) {
return this;
}

if (value.stream().anyMatch(Objects::isNull)) {
throw new NullElementInCollectionException(field);
}

return this;
}
}

/**
* Asserter dedicated to instant value
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package tech.jhipster.lite.error.domain;

public class NullElementInCollectionException extends AssertionException {

public NullElementInCollectionException(String field) {
super(message(field));
}

private static String message(String field) {
return new StringBuilder().append("The field \"").append(field).append("\" contains a null element").toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package tech.jhipster.lite.error.domain;

public class TooManyElementsException extends AssertionException {

public TooManyElementsException(CollectionTooLargeBuilder builder) {
super(builder.message());
}

public static CollectionTooLargeBuilder builder() {
return new CollectionTooLargeBuilder();
}

public static class CollectionTooLargeBuilder {

private String field;
private int maxSize;
private int size;

public CollectionTooLargeBuilder field(String field) {
this.field = field;

return this;
}

public CollectionTooLargeBuilder maxSize(int maxSize) {
this.maxSize = maxSize;

return this;
}

public CollectionTooLargeBuilder size(int size) {
this.size = size;

return this;
}

public String message() {
return new StringBuilder()
.append("Size of collection \"")
.append(field)
.append("\" must be at most ")
.append(maxSize)
.append(" but was ")
.append(size)
.toString();
}

public TooManyElementsException build() {
return new TooManyElementsException(this);
}
}
}
Loading