Skip to content

Commit

Permalink
Add Contract annotations.
Browse files Browse the repository at this point in the history
Original Pull Request: #3578
  • Loading branch information
mp911de committed Jan 14, 2025
1 parent 5fafaa4 commit 28a52e0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@
import java.util.Arrays;
import java.util.stream.StreamSupport;

import org.springframework.lang.CheckReturnValue;
import org.springframework.lang.Contract;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
* Specification in the sense of Domain Driven Design to handle Criteria Deletes.
*
* @author Mark Paluch
* @since xxx
* @since 4.0
*/
@FunctionalInterface
public interface DeleteSpecification<T> extends Serializable {
Expand Down Expand Up @@ -81,6 +83,8 @@ static <T> DeleteSpecification<T> where(PredicateSpecification<T> spec) {
* @param other the other {@link DeleteSpecification}.
* @return the conjunction of the specifications.
*/
@Contract("_ -> new")
@CheckReturnValue
default DeleteSpecification<T> and(DeleteSpecification<T> other) {

Assert.notNull(other, "Other specification must not be null");
Expand All @@ -94,6 +98,8 @@ default DeleteSpecification<T> and(DeleteSpecification<T> other) {
* @param other the other {@link PredicateSpecification}.
* @return the conjunction of the specifications.
*/
@Contract("_ -> new")
@CheckReturnValue
default DeleteSpecification<T> and(PredicateSpecification<T> other) {

Assert.notNull(other, "Other specification must not be null");
Expand All @@ -107,6 +113,8 @@ default DeleteSpecification<T> and(PredicateSpecification<T> other) {
* @param other the other {@link DeleteSpecification}.
* @return the disjunction of the specifications.
*/
@Contract("_ -> new")
@CheckReturnValue
default DeleteSpecification<T> or(DeleteSpecification<T> other) {

Assert.notNull(other, "Other specification must not be null");
Expand All @@ -120,6 +128,8 @@ default DeleteSpecification<T> or(DeleteSpecification<T> other) {
* @param other the other {@link PredicateSpecification}.
* @return the disjunction of the specifications.
*/
@Contract("_ -> new")
@CheckReturnValue
default DeleteSpecification<T> or(PredicateSpecification<T> other) {

Assert.notNull(other, "Other specification must not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@
import java.util.Arrays;
import java.util.stream.StreamSupport;

import org.springframework.lang.CheckReturnValue;
import org.springframework.lang.Contract;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
* Specification in the sense of Domain Driven Design.
*
* @author Mark Paluch
* @since xxx
* @since 4.0
*/
public interface PredicateSpecification<T> extends Serializable {

Expand All @@ -54,7 +56,7 @@ static <T> PredicateSpecification<T> all() {
*/
static <T> PredicateSpecification<T> where(PredicateSpecification<T> spec) {

Assert.notNull(spec, "DeleteSpecification must not be null");
Assert.notNull(spec, "PredicateSpecification must not be null");

return spec;
}
Expand All @@ -65,6 +67,8 @@ static <T> PredicateSpecification<T> where(PredicateSpecification<T> spec) {
* @param other the other {@link PredicateSpecification}.
* @return the conjunction of the specifications.
*/
@Contract("_ -> new")
@CheckReturnValue
default PredicateSpecification<T> and(PredicateSpecification<T> other) {

Assert.notNull(other, "Other specification must not be null");
Expand All @@ -78,6 +82,8 @@ default PredicateSpecification<T> and(PredicateSpecification<T> other) {
* @param other the other {@link PredicateSpecification}.
* @return the disjunction of the specifications.
*/
@Contract("_ -> new")
@CheckReturnValue
default PredicateSpecification<T> or(PredicateSpecification<T> other) {

Assert.notNull(other, "Other specification must not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.Arrays;
import java.util.stream.StreamSupport;

import org.springframework.lang.CheckReturnValue;
import org.springframework.lang.Contract;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -75,6 +77,8 @@ static <T> Specification<T> where(PredicateSpecification<T> spec) {
* @return the conjunction of the specifications.
* @since 2.0
*/
@Contract("_ -> new")
@CheckReturnValue
default Specification<T> and(Specification<T> other) {

Assert.notNull(other, "Other specification must not be null");
Expand All @@ -89,6 +93,8 @@ default Specification<T> and(Specification<T> other) {
* @return the conjunction of the specifications.
* @since 2.0
*/
@Contract("_ -> new")
@CheckReturnValue
default Specification<T> and(PredicateSpecification<T> other) {

Assert.notNull(other, "Other specification must not be null");
Expand All @@ -103,6 +109,8 @@ default Specification<T> and(PredicateSpecification<T> other) {
* @return the disjunction of the specifications
* @since 2.0
*/
@Contract("_ -> new")
@CheckReturnValue
default Specification<T> or(Specification<T> other) {

Assert.notNull(other, "Other specification must not be null");
Expand All @@ -117,6 +125,8 @@ default Specification<T> or(Specification<T> other) {
* @return the disjunction of the specifications
* @since 2.0
*/
@Contract("_ -> new")
@CheckReturnValue
default Specification<T> or(PredicateSpecification<T> other) {

Assert.notNull(other, "Other specification must not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@
import java.util.Arrays;
import java.util.stream.StreamSupport;

import org.springframework.lang.CheckReturnValue;
import org.springframework.lang.Contract;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
* Specification in the sense of Domain Driven Design to handle Criteria Updates.
*
* @author Mark Paluch
* @since xxx
* @since 4.0
*/
@FunctionalInterface
public interface UpdateSpecification<T> extends Serializable {
Expand Down Expand Up @@ -103,6 +105,8 @@ static <T> UpdateSpecification<T> where(PredicateSpecification<T> spec) {
* @param other the other {@link UpdateSpecification}.
* @return the conjunction of the specifications.
*/
@Contract("_ -> new")
@CheckReturnValue
default UpdateSpecification<T> and(UpdateSpecification<T> other) {

Assert.notNull(other, "Other specification must not be null");
Expand All @@ -116,6 +120,8 @@ default UpdateSpecification<T> and(UpdateSpecification<T> other) {
* @param other the other {@link PredicateSpecification}.
* @return the conjunction of the specifications.
*/
@Contract("_ -> new")
@CheckReturnValue
default UpdateSpecification<T> and(PredicateSpecification<T> other) {

Assert.notNull(other, "Other specification must not be null");
Expand All @@ -129,6 +135,8 @@ default UpdateSpecification<T> and(PredicateSpecification<T> other) {
* @param other the other {@link UpdateSpecification}.
* @return the disjunction of the specifications.
*/
@Contract("_ -> new")
@CheckReturnValue
default UpdateSpecification<T> or(UpdateSpecification<T> other) {

Assert.notNull(other, "Other specification must not be null");
Expand All @@ -142,6 +150,8 @@ default UpdateSpecification<T> or(UpdateSpecification<T> other) {
* @param other the other {@link PredicateSpecification}.
* @return the disjunction of the specifications.
*/
@Contract("_ -> new")
@CheckReturnValue
default UpdateSpecification<T> or(PredicateSpecification<T> other) {

Assert.notNull(other, "Other specification must not be null");
Expand Down Expand Up @@ -256,6 +266,8 @@ interface UpdateOperation<T> {
* @param other the other {@link UpdateOperation}.
* @return the conjunction of the specifications.
*/
@Contract("_ -> new")
@CheckReturnValue
default UpdateOperation<T> and(UpdateOperation<T> other) {

Assert.notNull(other, "Other UpdateOperation must not be null");
Expand All @@ -272,6 +284,8 @@ default UpdateOperation<T> and(UpdateOperation<T> other) {
* @param specification the {@link PredicateSpecification}.
* @return the conjunction of the specifications.
*/
@Contract("_ -> new")
@CheckReturnValue
default UpdateSpecification<T> where(PredicateSpecification<T> specification) {

Assert.notNull(specification, "PredicateSpecification must not be null");
Expand All @@ -288,6 +302,8 @@ default UpdateSpecification<T> where(PredicateSpecification<T> specification) {
* @param specification the {@link UpdateSpecification}.
* @return the conjunction of the specifications.
*/
@Contract("_ -> new")
@CheckReturnValue
default UpdateSpecification<T> where(UpdateSpecification<T> specification) {

Assert.notNull(specification, "UpdateSpecification must not be null");
Expand All @@ -306,6 +322,7 @@ default UpdateSpecification<T> where(UpdateSpecification<T> specification) {
* @param criteriaBuilder must not be {@literal null}.
*/
void apply(Root<T> root, CriteriaUpdate<T> update, CriteriaBuilder criteriaBuilder);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
/*
* Some JPA providers require {@code ids} to be a {@link Collection} so we must convert if it's not already.
*/
Collection<ID> idCollection = toCollection(ids);
Collection<ID> idCollection = toCollection(ids);
query.setParameter("ids", idCollection);

applyQueryHints(query);
Expand Down Expand Up @@ -732,8 +732,7 @@ protected TypedQuery<T> getQuery(Specification<T> spec, Pageable pageable) {
* @param domainClass must not be {@literal null}.
* @param pageable must not be {@literal null}.
*/
protected <S extends T> TypedQuery<S> getQuery(Specification<S> spec, Class<S> domainClass,
Pageable pageable) {
protected <S extends T> TypedQuery<S> getQuery(Specification<S> spec, Class<S> domainClass, Pageable pageable) {

return getQuery(spec, domainClass, pageable.getSort());
}
Expand Down Expand Up @@ -1074,7 +1073,7 @@ private static long executeCountQuery(TypedQuery<Long> query) {
@SuppressWarnings("rawtypes")
private static final class ByIdsSpecification<T> implements Specification<T> {

@Serial private static final long serialVersionUID = 1L;
@Serial private static final @Serial long serialVersionUID = 1L;

private final JpaEntityInformation<T, ?> entityInformation;

Expand All @@ -1085,6 +1084,7 @@ private static final class ByIdsSpecification<T> implements Specification<T> {
}

@Override
@SuppressWarnings("unchecked")
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

Path<?> path = root.get(entityInformation.getIdAttribute());
Expand All @@ -1103,7 +1103,7 @@ public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuild
*/
private static class ExampleSpecification<T> implements Specification<T> {

@Serial private static final long serialVersionUID = 1L;
@Serial private static final @Serial long serialVersionUID = 1L;

private final Example<T> example;
private final EscapeCharacter escapeCharacter;
Expand Down

0 comments on commit 28a52e0

Please sign in to comment.