Skip to content

Commit

Permalink
Cleaned up code style issues
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-schnell committed Feb 3, 2024
1 parent 252cc1d commit cd05a32
Show file tree
Hide file tree
Showing 56 changed files with 275 additions and 183 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
!.mvn
target
*.log
*.iml
5 changes: 5 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
<artifactId>jakarta.validation-api</artifactId>
</dependency>

<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
</dependency>

<!-- Test -->

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
package org.fuin.objects4j.common;

import jakarta.validation.ConstraintViolation;
import jakarta.validation.constraints.NotEmpty;

import java.io.Serial;
import java.util.Collections;
import java.util.Set;

Expand All @@ -27,8 +29,10 @@
*/
public final class ConstraintViolationException extends RuntimeException {

@Serial
private static final long serialVersionUID = 1L;

@SuppressWarnings("squid:S1948") // Cannot fix as external interface, but Hibernate is serializable
private final Set<ConstraintViolation<Object>> constraintViolations;

/**
Expand All @@ -37,7 +41,7 @@ public final class ConstraintViolationException extends RuntimeException {
* @param message
* Message.
*/
public ConstraintViolationException(final String message) {
public ConstraintViolationException(@NotEmpty final String message) {
super(message);
this.constraintViolations = null;
}
Expand All @@ -50,16 +54,17 @@ public ConstraintViolationException(final String message) {
* @param constraintViolations
* Constraint violations.
*/
public ConstraintViolationException(final String message, final Set<ConstraintViolation<Object>> constraintViolations) {
public ConstraintViolationException(@NotEmpty final String message, @Nullable final Set<ConstraintViolation<Object>> constraintViolations) {
super(message);
this.constraintViolations = constraintViolations;
}

/**
* Returns the constraint violations.
*
* @return Immutable set of constraint violations or <code>null</code> if only a message is available.
* @return Immutable set of constraint violations or {@literal null} if only a message is available.
*/
@SuppressWarnings("squid:S1168") // Won't fix. Needs to be backward compatible.
public final Set<ConstraintViolation<Object>> getConstraintViolations() {
if (constraintViolations == null) {
return null;
Expand Down
99 changes: 52 additions & 47 deletions common/src/main/java/org/fuin/objects4j/common/Contract.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
/**
* Copyright (C) 2015 Michael Schnell. All rights reserved.
* Copyright (C) 2015 Michael Schnell. All rights reserved.
* http://www.fuin.org/
*
* <p>
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at your option) any
* later version.
*
* <p>
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* <p>
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see http://www.gnu.org/licenses/.
*/
package org.fuin.objects4j.common;

import jakarta.annotation.Nullable;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.Validation;
import jakarta.validation.Validator;
Expand All @@ -40,7 +41,7 @@ public final class Contract {
/**
* Sets the validator to use for contract validation. This method is NOT thread safe. It should only be called once per application
* during the initialization phase.
*
*
* @param newValidator
* Set the validator to a new value.
*/
Expand All @@ -51,7 +52,7 @@ public static void setValidator(final Validator newValidator) {
/**
* Returns the validator that is used for contract validation. This method is NOT thread safe - This may lead to concurrent
* initialization of the validator if it's not set yet.
*
*
* @return Current instance - If the validator is not set yet, a new default validator will be created.
*/
public static Validator getValidator() {
Expand All @@ -70,12 +71,12 @@ private Contract() {

/**
* Checks if the value is not null.
*
*
* @param name
* Name of the value for a possible error message.
* @param value
* Value to check.
*
*
* @throws ConstraintViolationException
* The value was null.
*/
Expand All @@ -86,33 +87,33 @@ public static void requireArgNotNull(@NotNull final String name, final Object va
}

/**
* Checks if the value is not <code>null</code> or empty. A single space is considered a valid value.
*
* Checks if the value is not {@literal null} or empty. A single space is considered a valid value.
*
* @param name
* Name of the value for a possible error message.
* @param value
* Value to check.
*
*
* @throws ConstraintViolationException
* The value was null or empty.
*/
public static void requireArgNotEmpty(@NotNull final String name, final String value) throws ConstraintViolationException {
requireArgNotNull(name, value);
if (value.length() < 1) {
if (value.isEmpty()) {
throw new ConstraintViolationException("The argument '" + name + "' cannot be empty");
}
}

/**
* Checks if the length of value is not higher than a give maximum.
*
*
* @param name
* Name of the value for a possible error message.
* @param value
* Value to check.
* @param max
* Max length (inclusive).
*
*
* @throws ConstraintViolationException
* The length was more than <code>max</code>.
*/
Expand All @@ -125,14 +126,14 @@ public static void requireArgMaxLength(@NotNull final String name, @NotNull fina

/**
* Checks if the length of value is not less than a give minimum.
*
*
* @param name
* Name of the value for a possible error message.
* @param value
* Value to check.
* @param min
* Minimal length.
*
*
* @throws ConstraintViolationException
* The length was less than <code>min</code>.
*/
Expand All @@ -145,14 +146,14 @@ public static void requireArgMinLength(@NotNull final String name, @NotNull fina

/**
* Checks if the value is not higher than a give maximum.
*
*
* @param name
* Name of the value for a possible error message.
* @param value
* Value to check.
* @param max
* Max value (inclusive).
*
*
* @throws ConstraintViolationException
* The value was more than <code>max</code>.
*/
Expand All @@ -165,14 +166,14 @@ public static void requireArgMax(@NotNull final String name, @NotNull final long

/**
* Checks if the value is not less than a give minimum.
*
*
* @param name
* Name of the value for a possible error message.
* @param value
* Value to check.
* @param min
* Minimal value (inclusive).
*
*
* @throws ConstraintViolationException
* The value was less than <code>min</code>.
*/
Expand All @@ -185,14 +186,14 @@ public static void requireArgMin(@NotNull final String name, @NotNull final long

/**
* Checks if the given value is valid.
*
*
* @param validator
* Validator to use.
* @param value
* Value to check.
* @param groups
* Group or list of groups targeted for validation (defaults to {@link Default})
*
*
* @throws ConstraintViolationException
* The value is invalid.
*/
Expand All @@ -203,11 +204,15 @@ public static void requireValid(@NotNull final Validator validator, @NotNull fin
if (!constraintViolations.isEmpty()) {
final StringBuilder sb = new StringBuilder();
for (final ConstraintViolation<Object> constraintViolation : constraintViolations) {
if (sb.length() > 0) {
if (!sb.isEmpty()) {
sb.append(", ");
}
sb.append("[" + constraintViolation.getPropertyPath() + "] " + constraintViolation.getMessage() + " {"
+ constraintViolation.getInvalidValue() + "}");
sb.append("[")
.append(constraintViolation.getPropertyPath())
.append("] ")
.append(constraintViolation.getMessage())
.append(" {").append(constraintViolation.getInvalidValue())
.append("}");
}
throw new ConstraintViolationException(sb.toString(), constraintViolations);
}
Expand All @@ -216,12 +221,12 @@ public static void requireValid(@NotNull final Validator validator, @NotNull fin

/**
* Checks if the given value is valid using a default validator.
*
*
* @param value
* Value to check.
* @param groups
* Group or list of groups targeted for validation (defaults to {@link Default})
*
*
* @throws ConstraintViolationException
* The value is invalid.
*/
Expand All @@ -231,38 +236,38 @@ public static void requireValid(@NotNull final Object value, @Nullable final Cla

/**
* Validates the given object.
*
*
* @param validator
* Validator to use.
* @param value
* Value to validate.
* @param groups
* Group or list of groups targeted for validation (defaults to {@link Default})
*
*
* @return List of constraint violations.
*
*
* @param <TYPE>
* Type of the validated object.
*/
@NotNull
public static <TYPE> Set<ConstraintViolation<TYPE>> validate(@NotNull final Validator validator, @Nullable final TYPE value,
@Nullable final Class<?>... groups) {
@Nullable final Class<?>... groups) {
if (value == null) {
return new HashSet<ConstraintViolation<TYPE>>();
return new HashSet<>();
}
return validator.validate(value, groups);
}

/**
* Validates the given object using a default validator.
*
*
* @param value
* Value to validate.
* @param groups
* Group or list of groups targeted for validation (defaults to {@link Default})
*
*
* @return List of constraint violations.
*
*
* @param <TYPE>
* Type of the validated object.
*/
Expand All @@ -273,16 +278,16 @@ public static <TYPE> Set<ConstraintViolation<TYPE>> validate(@Nullable final TYP

/**
* Converts a set of constraint violation into a string list.
*
*
* @param constraintViolations
* Violations to convert to a string.
*
*
* @return List of string representations for all violations.
*
*
* @param <T> Type of list content.
*/
public static <T> List<String> asString(@Nullable final Set<ConstraintViolation<T>> constraintViolations) {
if (constraintViolations == null || constraintViolations.size() == 0) {
if (constraintViolations == null || constraintViolations.isEmpty()) {
return Collections.emptyList();
}
final List<String> list = new ArrayList<>();
Expand All @@ -294,14 +299,14 @@ public static <T> List<String> asString(@Nullable final Set<ConstraintViolation<

/**
* Converts a set of constraint violation into a string.
*
*
* @param constraintViolations
* Violations to convert to a string.
* @param separator
* Separator to use between violations. Defaults to ', ' in case of {@literal null} argument.
*
*
* @return String representation of all violations.
*
*
* @param <T> Type of the root bean.
*/
public static <T> String asString(@Nullable final Set<ConstraintViolation<T>> constraintViolations, @Nullable final String separator) {
Expand All @@ -314,9 +319,9 @@ public static <T> String asString(@Nullable final Set<ConstraintViolation<T>> co
} else {
sepStr = separator;
}
final StringBuffer sb = new StringBuffer();
final StringBuilder sb = new StringBuilder();
for (final ConstraintViolation<T> constraintViolation : constraintViolations) {
if (sb.length() > 0) {
if (!sb.isEmpty()) {
sb.append(sepStr);
}
sb.append(asString(constraintViolation));
Expand All @@ -326,12 +331,12 @@ public static <T> String asString(@Nullable final Set<ConstraintViolation<T>> co

/**
* Returns a constraint violation as string.
*
*
* @param violation
* Violation to convert to a string.
*
*
* @return Text like "SIMPLE_CLASS_NAME.PROPERTY_PATH MESSAGE" or "SIMPLE_CLASS_NAME.PROPERTY_PATH MESSAGE (INVALID_VALUE)".
*
*
* @param <T> Type of the validated root object.
*/
public static <T> String asString(@Nullable final ConstraintViolation<T> violation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Used to express that a method's return value is never <code>null</code> or empty.
* Used to express that a method's return value is never {@literal null} or empty.
*
* @deprecated Use <code>jakarta.validation.constraints.NotNull</code> from bean validation instead.
* It wasn't allowed to use it for a return value, but it is now.
Expand Down
Loading

0 comments on commit cd05a32

Please sign in to comment.