DATAJPA-1449 - Removal of Specifications.
`Specifications` is deprecated since 2.0. This removes it completely. * Moves static helper code into a private helper class. * Removes references to `Specifications` from `Specification`. Original pull request: #300.
This commit is contained in:
committed by
Jens Schauder
parent
580d2a65c0
commit
4c17e295ab
@@ -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;
|
||||
|
||||
@@ -35,11 +36,12 @@ import org.springframework.lang.Nullable;
|
||||
* @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}.
|
||||
*
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -61,7 +63,7 @@ public interface Specification<T> extends Serializable {
|
||||
* @since 2.0
|
||||
*/
|
||||
static <T> Specification<T> where(Specification<T> spec) {
|
||||
return Specifications.where(spec);
|
||||
return spec == null ? EMPTY_SPEC : spec;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -72,7 +74,7 @@ public interface Specification<T> extends Serializable {
|
||||
* @since 2.0
|
||||
*/
|
||||
default Specification<T> and(Specification<T> other) {
|
||||
return Specifications.composed(this, other, AND);
|
||||
return composed(this, other, AND);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,7 +85,7 @@ public interface Specification<T> extends Serializable {
|
||||
* @since 2.0
|
||||
*/
|
||||
default Specification<T> or(Specification<T> other) {
|
||||
return Specifications.composed(this, other, OR);
|
||||
return composed(this, other, OR);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,153 +0,0 @@
|
||||
/*
|
||||
* Copyright 2008-2018 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.data.jpa.domain;
|
||||
|
||||
import static org.springframework.data.jpa.domain.Specifications.CompositionType.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
/**
|
||||
* Helper class to easily combine {@link Specification} instances.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
* @author Sebastian Staudt
|
||||
* @author Mark Paluch
|
||||
* @deprecated since 2.0, use factory methods on {@link Specification} instead.
|
||||
*/
|
||||
@Deprecated
|
||||
public class Specifications<T> implements Specification<T>, Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private final @Nullable Specification<T> spec;
|
||||
|
||||
/**
|
||||
* Creates a new {@link Specifications} wrapper for the given {@link Specification}.
|
||||
*
|
||||
* @param spec can be {@literal null}.
|
||||
*/
|
||||
Specifications(@Nullable Specification<T> spec) {
|
||||
this.spec = spec;
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple static factory method to add some syntactic sugar around a {@link Specification}.
|
||||
*
|
||||
* @deprecated since 2.0, use {@link Specification#where} instead
|
||||
* @param <T> type parameter for the specification parameter.
|
||||
* @param spec can be {@literal null}.
|
||||
* @return a new Specifcations instance. Guaranteed to be not {@code null}.
|
||||
*/
|
||||
@Deprecated
|
||||
public static <T> Specifications<T> where(@Nullable Specification<T> spec) {
|
||||
return new Specifications<>(spec);
|
||||
}
|
||||
|
||||
/**
|
||||
* ANDs the given {@link Specification} to the current one.
|
||||
*
|
||||
* @deprecated since 2.0, use {@link Specification#and} instead
|
||||
* @param other can be {@literal null}.
|
||||
* @return a new Specifications instance combining this and the parameter instance. Guaranteed to be not {@code null}.
|
||||
*/
|
||||
@Deprecated
|
||||
public Specifications<T> and(@Nullable Specification<T> other) {
|
||||
return new Specifications<>(composed(spec, other, AND));
|
||||
}
|
||||
|
||||
/**
|
||||
* ORs the given specification to the current one.
|
||||
*
|
||||
* @deprecated since 2.0, use {@link Specification#or} instead
|
||||
* @param other can be {@literal null}.
|
||||
* @return a new Specifications instance combining this and the parameter instance. Guaranteed to be not {@code null}.
|
||||
*/
|
||||
@Deprecated
|
||||
public Specifications<T> or(@Nullable Specification<T> other) {
|
||||
return new Specifications<>(composed(spec, other, OR));
|
||||
}
|
||||
|
||||
/**
|
||||
* Negates the given {@link Specification}.
|
||||
*
|
||||
* @deprecated since 2.0, use {@link Specification#not} instead
|
||||
* @param <T> type parameter for the specification parameter.
|
||||
* @param spec can be {@literal null}.
|
||||
* @return a new Specifications instance combining this and the parameter instance. Guaranteed to be not {@code null}.
|
||||
*/
|
||||
@Deprecated
|
||||
public static <T> Specifications<T> not(@Nullable Specification<T> spec) {
|
||||
return new Specifications<>(negated(spec));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.jpa.domain.Specification#toPredicate(javax.persistence.criteria.Root, javax.persistence.criteria.CriteriaQuery, javax.persistence.criteria.CriteriaBuilder)
|
||||
*/
|
||||
@Nullable
|
||||
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
|
||||
return spec == null ? null : spec.toPredicate(root, query, builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,145 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2018 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.data.jpa.domain;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.data.jpa.domain.Specifications.*;
|
||||
import static org.springframework.util.SerializationUtils.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
|
||||
/**
|
||||
* @author Oliver Gierke
|
||||
* @author Thomas Darimont
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class SpecificationsUnitTests {
|
||||
|
||||
Specification<Object> mockSpec;
|
||||
@Mock(extraInterfaces = Serializable.class) Root<Object> root;
|
||||
@Mock(extraInterfaces = Serializable.class) CriteriaQuery<?> query;
|
||||
@Mock(extraInterfaces = Serializable.class) CriteriaBuilder builder;
|
||||
|
||||
@Mock(extraInterfaces = Serializable.class) Predicate predicate;
|
||||
|
||||
@Before
|
||||
@SuppressWarnings("unchecked")
|
||||
public void setUp() {
|
||||
|
||||
mockSpec = (Specification<Object>) mock(Specification.class, withSettings().serializable());
|
||||
|
||||
when(mockSpec.toPredicate(root, query, builder)).thenReturn(predicate);
|
||||
}
|
||||
|
||||
@Test // DATAJPA-300
|
||||
public void createsSpecificationsFromNull() {
|
||||
|
||||
Specifications<Object> specification = where(null);
|
||||
assertThat(specification, is(notNullValue()));
|
||||
assertThat(specification.toPredicate(root, query, builder), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test // DATAJPA-300
|
||||
public void negatesNullSpecToNull() {
|
||||
|
||||
Specifications<Object> specification = not((Specification<Object>) null);
|
||||
|
||||
assertThat(specification, is(notNullValue()));
|
||||
assertThat(specification.toPredicate(root, query, builder), is(nullValue()));
|
||||
}
|
||||
|
||||
@Test // DATAJPA-300
|
||||
public void andConcatenatesSpecToNullSpec() {
|
||||
|
||||
Specifications<Object> specification = where(null);
|
||||
specification = specification.and(mockSpec);
|
||||
|
||||
assertThat(specification, is(notNullValue()));
|
||||
assertThat(specification.toPredicate(root, query, builder), is(predicate));
|
||||
}
|
||||
|
||||
@Test // DATAJPA-300
|
||||
public void andConcatenatesNullSpecToSpec() {
|
||||
|
||||
Specifications<Object> specification = where(mockSpec);
|
||||
specification = specification.and(null);
|
||||
|
||||
assertThat(specification, is(notNullValue()));
|
||||
assertThat(specification.toPredicate(root, query, builder), is(predicate));
|
||||
}
|
||||
|
||||
@Test // DATAJPA-300
|
||||
public void orConcatenatesSpecToNullSpec() {
|
||||
|
||||
Specifications<Object> specification = where(null);
|
||||
specification = specification.or(mockSpec);
|
||||
|
||||
assertThat(specification, is(notNullValue()));
|
||||
assertThat(specification.toPredicate(root, query, builder), is(predicate));
|
||||
}
|
||||
|
||||
@Test // DATAJPA-300
|
||||
public void orConcatenatesNullSpecToSpec() {
|
||||
|
||||
Specifications<Object> specification = where(mockSpec);
|
||||
specification = specification.or(null);
|
||||
|
||||
assertThat(specification, is(notNullValue()));
|
||||
assertThat(specification.toPredicate(root, query, builder), is(predicate));
|
||||
}
|
||||
|
||||
@Test // DATAJPA-523
|
||||
public void specificationsShouldBeSerializable() {
|
||||
|
||||
Specifications<Object> specification = where(mockSpec);
|
||||
specification = specification.and(mockSpec);
|
||||
|
||||
assertThat(specification, is(notNullValue()));
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Specifications<Object> transferedSpecification = (Specifications<Object>) deserialize(serialize(specification));
|
||||
|
||||
assertThat(transferedSpecification, is(notNullValue()));
|
||||
}
|
||||
|
||||
@Test // DATAJPA-523
|
||||
public void complexSpecificationsShouldBeSerializable() {
|
||||
|
||||
Specifications<Object> specification = where(mockSpec);
|
||||
specification = Specifications.not(specification.and(mockSpec).or(mockSpec));
|
||||
|
||||
assertThat(specification, is(notNullValue()));
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Specifications<Object> transferedSpecification = (Specifications<Object>) deserialize(serialize(specification));
|
||||
|
||||
assertThat(transferedSpecification, is(notNullValue()));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user