Skip to content

Commit

Permalink
Implement Specification as enum
Browse files Browse the repository at this point in the history
  • Loading branch information
iselo committed Jan 27, 2025
1 parent 453cacc commit 3275e33
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies {
"org.springframework.boot:spring-boot-starter-web",
"org.springframework.boot:spring-boot-starter-validation",
"org.springframework.boot:spring-boot-starter-webflux",
"org.springframework.boot:spring-boot-starter-data-jpa",
"com.google.guava:guava:33.4.0-jre",
"com.google.code.gson:gson:2.11.0"
).forEach { implementation(it) }
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/org/test/criteria/Customer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.test.criteria;

import lombok.Data;

@Data
public class Customer {

private final Class<?> typeTocken;
}
15 changes: 15 additions & 0 deletions src/main/java/org/test/criteria/CustomerRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.test.criteria;

import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.lang.Nullable;

import java.util.List;
import java.util.UUID;

public interface CustomerRepository
extends JpaRepository<Customer, UUID>, JpaSpecificationExecutor<Customer> {

List<Customer> findAll(@Nullable Specification<Customer> customerSpecification);
}
23 changes: 23 additions & 0 deletions src/main/java/org/test/criteria/CustomerService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.test.criteria;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import static org.springframework.data.jpa.domain.Specification.where;
import static org.test.criteria.CustomerSpecifications.CUSTOMER_HAS_BIRTHDAY;
import static org.test.criteria.CustomerSpecifications.IS_LONG_TERM_CUSTOMER;

public class CustomerService {

private final CustomerRepository repository;

@Autowired
public CustomerService(CustomerRepository repository) {
this.repository = repository;
}

public List<Customer> nothing() {
return repository.findAll(where(CUSTOMER_HAS_BIRTHDAY).and(IS_LONG_TERM_CUSTOMER));
}
}
34 changes: 34 additions & 0 deletions src/main/java/org/test/criteria/CustomerSpecifications.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.test.criteria;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;

import java.time.LocalDate;

enum CustomerSpecifications implements Specification<Customer> {

CUSTOMER_HAS_BIRTHDAY {
@Override
public Predicate toPredicate(Root<Customer> root,
CriteriaQuery<?> query,
CriteriaBuilder criteriaBuilder) {
var birthday = root.<LocalDate>get("birthday");
var today = LocalDate.now();
return criteriaBuilder.equal(birthday, today);
}
},

IS_LONG_TERM_CUSTOMER {
@Override
public Predicate toPredicate(Root<Customer> root,
CriteriaQuery<?> query,
CriteriaBuilder criteriaBuilder) {
var createdAt1 = root.<LocalDate>get("createdAt");
var twoYearsFromNow = LocalDate.now().minusYears(2);
return criteriaBuilder.lessThan(createdAt1, twoYearsFromNow);
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/org/test/criteria/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@NonNullApi
@CheckReturnValue
package org.test.criteria;

import com.google.errorprone.annotations.CheckReturnValue;
import org.springframework.lang.NonNullApi;

0 comments on commit 3275e33

Please sign in to comment.