Skip to content

DATAJPA-1449 - Prepare removal of Specifications #300

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

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
*/
package org.springframework.data.jpa.domain;

import static org.springframework.data.jpa.domain.Specifications.CompositionType.*;
import static org.springframework.data.jpa.domain.SpecificationComposition.*;
import static org.springframework.data.jpa.domain.SpecificationComposition.CompositionType.*;

import java.io.Serializable;

Expand All @@ -35,11 +36,12 @@
* @author Sebastian Staudt
* @author Mark Paluch
*/
@SuppressWarnings("deprecation")
public interface Specification<T> extends Serializable {

long serialVersionUID = 1L;

Specification EMPTY_SPEC = (root, query, criteriaBuilder) -> null;

/**
* Negates the given {@link Specification}.
*
Expand All @@ -49,7 +51,7 @@ public interface Specification<T> extends Serializable {
* @since 2.0
*/
static <T> Specification<T> not(Specification<T> spec) {
return Specifications.negated(spec);
return negated(spec);
}

/**
Expand All @@ -61,7 +63,7 @@ static <T> Specification<T> not(Specification<T> spec) {
* @since 2.0
*/
static <T> Specification<T> where(Specification<T> spec) {
return Specifications.where(spec);
return spec == null ? EMPTY_SPEC : spec;
}

/**
Expand All @@ -72,7 +74,7 @@ static <T> Specification<T> where(Specification<T> spec) {
* @since 2.0
*/
default Specification<T> and(Specification<T> other) {
return Specifications.composed(this, other, AND);
return composed(this, other, AND);
}

/**
Expand All @@ -83,7 +85,7 @@ default Specification<T> and(Specification<T> other) {
* @since 2.0
*/
default Specification<T> or(Specification<T> other) {
return Specifications.composed(this, other, OR);
return composed(this, other, OR);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.springframework.data.jpa.domain;

import org.springframework.lang.Nullable;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;

/**
* Helper class to support specification compositions
*
* @author Sebastian Staudt
* @see Specification
*/
class SpecificationComposition {

/**
* Enum for the composition types for {@link Predicate}s. Can not be turned into lambdas as we need to be
* serializable.
*
* @author Thomas Darimont
*/
enum CompositionType {

AND {
@Override
public Predicate combine(CriteriaBuilder builder, Predicate lhs, Predicate rhs) {
return builder.and(lhs, rhs);
}
},

OR {
@Override
public Predicate combine(CriteriaBuilder builder, Predicate lhs, Predicate rhs) {
return builder.or(lhs, rhs);
}
};

abstract Predicate combine(CriteriaBuilder builder, Predicate lhs, Predicate rhs);
}

static <T> Specification<T> negated(@Nullable Specification<T> spec) {
return (root, query, builder) -> spec == null ? null : builder.not(spec.toPredicate(root, query, builder));
}

static <T> Specification<T> composed(@Nullable Specification<T> lhs, @Nullable Specification<T> rhs, CompositionType compositionType) {

return (root, query, builder) -> {

Predicate otherPredicate = rhs == null ? null : rhs.toPredicate(root, query, builder);
Predicate thisPredicate = lhs == null ? null : lhs.toPredicate(root, query, builder);

return thisPredicate == null ? otherPredicate
: otherPredicate == null ? thisPredicate : compositionType.combine(builder, thisPredicate, otherPredicate);
};
}
}
153 changes: 0 additions & 153 deletions src/main/java/org/springframework/data/jpa/domain/Specifications.java

This file was deleted.

Loading