diff --git a/README.md b/README.md index bcfe915..92a98dd 100644 --- a/README.md +++ b/README.md @@ -261,12 +261,12 @@ public class SpringQueryFilter implements ISpringQueryFilter { private String sort; @Override - public int getPage() { + public int getComputedPage() { return Math.max(Optional.ofNullable(page).orElse(0), 0); } @Override - public int getPageSize() { + public int getComputedSize() { return Math.clamp(Optional.ofNullable(pageSize).orElse(DEFAULT_PAGE_SIZE), MIN_PAGE_SIZE, MAX_PAGE_SIZE); } diff --git a/changelog.md b/changelog.md index 0f5778f..f01f926 100644 --- a/changelog.md +++ b/changelog.md @@ -5,6 +5,16 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html) +## [Unreleased] + +### Fixed + +- Add missing module-info.java. + +### Removed + +- Lombok dependency. + ## [1.1.1] 2024/11/18 ### Fixed diff --git a/pom.xml b/pom.xml index c927d69..4303cbf 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.github.zorin95670 spring-query-filter - 1.1.1 + 1.1.2-SNAPSHOT jar spring-query-filter @@ -73,14 +73,6 @@ test - - - org.projectlombok - lombok - 1.18.34 - provided - - org.junit.jupiter diff --git a/src/main/java/io/github/zorin95670/exception/SpringQueryFilterException.java b/src/main/java/io/github/zorin95670/exception/SpringQueryFilterException.java index 637e6af..e47258c 100644 --- a/src/main/java/io/github/zorin95670/exception/SpringQueryFilterException.java +++ b/src/main/java/io/github/zorin95670/exception/SpringQueryFilterException.java @@ -1,8 +1,5 @@ package io.github.zorin95670.exception; -import lombok.Data; -import lombok.EqualsAndHashCode; - /** * Exception thrown to indicate an error occurred while processing query filters in the Spring Query Filter library. * Provides additional details about the filter type, query parameter name, and the associated value, @@ -11,8 +8,6 @@ * Extends {@link RuntimeException}, allowing users to handle or propagate this exception as needed. *

*/ -@Data -@EqualsAndHashCode(callSuper = false) public class SpringQueryFilterException extends RuntimeException { /** @@ -98,4 +93,31 @@ public SpringQueryFilterException(final String message, this.queryParameterName = queryParameterName; this.queryParameterValue = queryParameterValue; } + + /** + * Gets the query filter type that caused the exception. + * + * @return the query filter type + */ + public String getQueryFilterType() { + return queryFilterType; + } + + /** + * Gets the query parameter name that caused the exception. + * + * @return the query parameter name + */ + public String getQueryParameterName() { + return queryParameterName; + } + + /** + * Gets the query parameter value that caused the exception. + * + * @return the query parameter value + */ + public String getQueryParameterValue() { + return queryParameterValue; + } } diff --git a/src/main/java/io/github/zorin95670/predicate/PredicateFilter.java b/src/main/java/io/github/zorin95670/predicate/PredicateFilter.java index 7b39f5c..ecb20e2 100644 --- a/src/main/java/io/github/zorin95670/predicate/PredicateFilter.java +++ b/src/main/java/io/github/zorin95670/predicate/PredicateFilter.java @@ -5,8 +5,6 @@ import jakarta.persistence.criteria.Expression; import jakarta.persistence.criteria.Predicate; import jakarta.persistence.criteria.Root; -import lombok.Getter; -import lombok.Setter; import java.util.Arrays; import java.util.stream.IntStream; @@ -34,8 +32,6 @@ public abstract class PredicateFilter implements IPredicateFilter { /** * The name of the query parameter, representing the field to filter. */ - @Getter - @Setter private String name; /** @@ -76,6 +72,24 @@ public void setIsNotOperator(final int index, final boolean state) { this.isNotOperators[index] = state; } + /** + * Gets the name of the query parameter. + * + * @return the name of the query parameter + */ + public String getName() { + return name; + } + + /** + * Sets the name of the query parameter. + * + * @param name the name of the query parameter + */ + public void setName(final String name) { + this.name = name; + } + /** * Retrieves a specific value from the parameter list. * diff --git a/src/main/java/io/github/zorin95670/predicate/PredicateOperator.java b/src/main/java/io/github/zorin95670/predicate/PredicateOperator.java index 7291e9b..a53b2c7 100644 --- a/src/main/java/io/github/zorin95670/predicate/PredicateOperator.java +++ b/src/main/java/io/github/zorin95670/predicate/PredicateOperator.java @@ -1,7 +1,5 @@ package io.github.zorin95670.predicate; -import lombok.Getter; - /** * Enum representing different predicate operators used for query filtering. *

@@ -79,9 +77,17 @@ public enum PredicateOperator { /** * Operator value. */ - @Getter private final String value; + /** + * Gets the operator value. + * + * @return the operator value. + */ + public String getValue() { + return value; + } + /** * Constructor to assign the string value to each operator. * diff --git a/src/main/java/io/github/zorin95670/query/ISpringQueryFilter.java b/src/main/java/io/github/zorin95670/query/ISpringQueryFilter.java index b2156b0..f7fdf99 100644 --- a/src/main/java/io/github/zorin95670/query/ISpringQueryFilter.java +++ b/src/main/java/io/github/zorin95670/query/ISpringQueryFilter.java @@ -19,14 +19,14 @@ public interface ISpringQueryFilter { * * @return the page number (0-based index). */ - int getPage(); + int getComputedPage(); /** * Retrieves the page size for pagination. * * @return the number of records per page. */ - int getPageSize(); + int getComputedPageSize(); /** * Retrieves the field by which results should be ordered. @@ -81,9 +81,9 @@ default Pageable getPageable(String defaultOrder) { Sort sort = this.getOrderBy(defaultOrder); if (sort != null) { - return PageRequest.of(this.getPage(), this.getPageSize(), sort); + return PageRequest.of(this.getComputedPage(), this.getComputedPageSize(), sort); } - return PageRequest.of(this.getPage(), this.getPageSize()); + return PageRequest.of(this.getComputedPage(), this.getComputedPageSize()); } } diff --git a/src/main/java/io/github/zorin95670/query/SpringQueryFilter.java b/src/main/java/io/github/zorin95670/query/SpringQueryFilter.java index 96d0134..303e212 100644 --- a/src/main/java/io/github/zorin95670/query/SpringQueryFilter.java +++ b/src/main/java/io/github/zorin95670/query/SpringQueryFilter.java @@ -1,16 +1,11 @@ package io.github.zorin95670.query; -import lombok.AllArgsConstructor; -import lombok.NoArgsConstructor; - import java.util.Optional; /** * Class implementing {@link ISpringQueryFilter} for managing pagination and sorting parameters in a Spring application. * This class provides default values for pagination and handles sorting direction based on the `sort` field. */ -@NoArgsConstructor -@AllArgsConstructor public class SpringQueryFilter implements ISpringQueryFilter { /** @@ -41,42 +36,93 @@ public class SpringQueryFilter implements ISpringQueryFilter { /** * Field name by which results should be ordered. Can be set to null. */ - private String order; + private String order = null; /** * Sorting direction. Should be "asc" for ascending order; any other value is treated as descending. */ - private String sort; + private String sort = null; /** - * Retrieves the page number for pagination, defaulting to 0 if the page is null or less than 0. + * Default no-argument constructor. + */ + public SpringQueryFilter() { + } + + /** + * Constructor to initialize all properties. * - * @return the page number, constrained to be non-negative. + * @param page the page number (0-based index). + * @param pageSize the number of records per page. + * @param order the field name by which results should be ordered. + * @param sort the sorting direction (ascending or descending). */ - @Override - public int getPage() { - return Math.max(Optional.ofNullable(page).orElse(0), 0); + public SpringQueryFilter(final Integer page, final Integer pageSize, final String order, final String sort) { + this.page = page; + this.pageSize = pageSize; + this.order = order; + this.sort = sort; } /** - * Retrieves the page size for pagination, constrained between 1 and 10. - * Defaults to 1 if page size is null or less than 1. + * Returns the page number for pagination (0-based index). + * Defaults to 0 if not specified. * - * @return the page size, bounded by a minimum of 1 and a maximum of 10. + * @return the page number (0-based index) + */ + public Integer getPage() { + return page; + } + + /** + * Returns the number of records per page. + * Defaults to 1 if not specified. + * + * @return the number of records per page + */ + public Integer getPageSize() { + return pageSize; + } + + /** + * Returns the field name by which results should be ordered. + * Can be set to null. + * + * @return the field name for ordering, or null if not specified + */ + public String getOrder() { + return order; + } + + /** + * Returns the sorting direction. + * Should be "asc" for ascending order; any other value is treated as descending. + * + * @return the sorting direction, either "asc" for ascending or any other value for descending + */ + public String getSort() { + return sort; + } + + /** + * Retrieves the page number for pagination, defaulting to 0 if the page is null or less than 0. + * + * @return the page number, constrained to be non-negative. */ @Override - public int getPageSize() { - return Math.clamp(Optional.ofNullable(pageSize).orElse(DEFAULT_PAGE_SIZE), MIN_PAGE_SIZE, MAX_PAGE_SIZE); + public int getComputedPage() { + return Math.max(Optional.ofNullable(getPage()).orElse(0), 0); } /** - * Retrieves the field name by which results should be ordered. + * Retrieves the page size for pagination, constrained between 1 and 10. + * Defaults to 1 if page size is null or less than 1. * - * @return the order field, or null if not set. + * @return the page size, bounded by a minimum of 1 and a maximum of 10. */ @Override - public String getOrder() { - return this.order; + public int getComputedPageSize() { + return Math.clamp(Optional.ofNullable(getPageSize()).orElse(DEFAULT_PAGE_SIZE), MIN_PAGE_SIZE, MAX_PAGE_SIZE); } /** @@ -86,6 +132,6 @@ public String getOrder() { */ @Override public boolean isAscendantSort() { - return "asc".equalsIgnoreCase(sort); + return "asc".equalsIgnoreCase(getSort()); } } diff --git a/src/main/java/io/github/zorin95670/specification/SpringQueryFilterSpecification.java b/src/main/java/io/github/zorin95670/specification/SpringQueryFilterSpecification.java index 3ff9c39..fb9762b 100644 --- a/src/main/java/io/github/zorin95670/specification/SpringQueryFilterSpecification.java +++ b/src/main/java/io/github/zorin95670/specification/SpringQueryFilterSpecification.java @@ -15,8 +15,6 @@ import jakarta.persistence.criteria.CriteriaQuery; import jakarta.persistence.criteria.Predicate; import jakarta.persistence.criteria.Root; -import lombok.Getter; -import lombok.Setter; import org.springframework.data.jpa.domain.Specification; import java.lang.reflect.Field; @@ -49,8 +47,7 @@ public class SpringQueryFilterSpecification implements Specification { * This map is used to dynamically build the filtering predicates based on the provided values for each field. *

*/ - @Setter - private Map> filters; + private final Map> filters; /** * The class of the entity to filter. @@ -58,9 +55,7 @@ public class SpringQueryFilterSpecification implements Specification { * This class is used to reflect on the fields of the entity and apply filters accordingly. *

*/ - @Getter - @Setter - private Class entityClass; + private final Class entityClass; /** * Constructs a new specification with the provided entity class and filters. @@ -69,8 +64,26 @@ public class SpringQueryFilterSpecification implements Specification { * @param filters The map of filters for field names and values. */ public SpringQueryFilterSpecification(final Class entityClass, final Map> filters) { - setEntityClass(entityClass); - setFilters(filters); + this.entityClass = entityClass; + this.filters = filters; + } + + /** + * Gets the map of filters. + * + * @return the map of filters + */ + public Map> getFilters() { + return filters; + } + + /** + * Gets the class of the entity to filter. + * + * @return the class of the entity + */ + public Class getEntityClass() { + return entityClass; } /** diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java new file mode 100644 index 0000000..3413beb --- /dev/null +++ b/src/main/java/module-info.java @@ -0,0 +1,10 @@ +module io.github.zorin95670 { + exports io.github.zorin95670.exception; + exports io.github.zorin95670.predicate; + exports io.github.zorin95670.query; + exports io.github.zorin95670.specification; + + requires transitive jakarta.persistence; + requires spring.data.commons; + requires spring.data.jpa; +} diff --git a/src/test/java/io/github/zorin95670/specification/MyEntity.java b/src/test/java/io/github/zorin95670/specification/MyEntity.java index 22b13b6..ee486bf 100644 --- a/src/test/java/io/github/zorin95670/specification/MyEntity.java +++ b/src/test/java/io/github/zorin95670/specification/MyEntity.java @@ -6,13 +6,12 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; -import lombok.Data; import java.util.Date; +import java.util.Objects; import java.util.UUID; @Entity -@Data public class MyEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -45,4 +44,81 @@ public class MyEntity { @Column private String unfilteredField; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public Date getDate() { + return date; + } + + public void setDate(Date date) { + this.date = date; + } + + public UUID getUuid() { + return uuid; + } + + public void setUuid(UUID uuid) { + this.uuid = uuid; + } + + public int getNumberInteger() { + return numberInteger; + } + + public void setNumberInteger(int numberInteger) { + this.numberInteger = numberInteger; + } + + public float getNumberFloat() { + return numberFloat; + } + + public void setNumberFloat(float numberFloat) { + this.numberFloat = numberFloat; + } + + public double getNumberDouble() { + return numberDouble; + } + + public void setNumberDouble(double numberDouble) { + this.numberDouble = numberDouble; + } + + public String getUnfilteredField() { + return unfilteredField; + } + + public void setUnfilteredField(String unfilteredField) { + this.unfilteredField = unfilteredField; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + MyEntity myEntity = (MyEntity) o; + return Objects.equals(id, myEntity.id); + } + + @Override + public int hashCode() { + return Objects.hash(id); + } } diff --git a/src/test/java/io/github/zorin95670/specification/SpringQueryFilterSpecificationTest.java b/src/test/java/io/github/zorin95670/specification/SpringQueryFilterSpecificationTest.java index aab25c4..febdd8d 100644 --- a/src/test/java/io/github/zorin95670/specification/SpringQueryFilterSpecificationTest.java +++ b/src/test/java/io/github/zorin95670/specification/SpringQueryFilterSpecificationTest.java @@ -15,7 +15,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import javax.swing.text.html.parser.Entity; import java.sql.Timestamp; import java.util.Date; import java.util.HashMap; @@ -49,7 +48,7 @@ MyEntity createEntity(int number, UUID uuid) { @Test @DisplayName("Test getPredicateFilter: should return valid Predicate") void testGetPredicateFilter() { - var specification = new SpringQueryFilterSpecification<>(Entity.class, new HashMap<>()); + var specification = new SpringQueryFilterSpecification<>(MyEntity.class, new HashMap<>()); assertEquals(StringPredicateFilter.class, specification.getPredicateFilter(String.class, "name", "value").getClass()); assertEquals(DatePredicateFilter.class, specification.getPredicateFilter(Date.class, "name", "value").getClass()); @@ -64,7 +63,7 @@ void testGetPredicateFilter() { @Test @DisplayName("Test getPredicateFilter: should throw exception on unknown type") void testGetPredicateFilterThrowException() { - var specification = new SpringQueryFilterSpecification<>(Entity.class, new HashMap<>()); + var specification = new SpringQueryFilterSpecification<>(MyEntity.class, new HashMap<>()); SpringQueryFilterException exception = null;