Skip to content

Commit

Permalink
feat: make the creation of new QuerySpec easier
Browse files Browse the repository at this point in the history
Make the creation of new QuerySpec instances easier by using chained
Accessors. This make the feature not binary compatible, but given that
we're just returning QuerySpecs instead of void, everything should
compile, thus this is not a breaking change.

Closes #93
  • Loading branch information
mlopezFC committed Jul 1, 2024
1 parent f734c52 commit a852b5d
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

@Accessors(chain=true)
public class QuerySpec {

public enum Order {
Expand All @@ -51,19 +52,22 @@ public enum Order {
@Getter
private final Collection<Constraint> constraints = new ArrayList<>();

public void addOrder(String property, Order order) {
public QuerySpec addOrder(String property, Order order) {
if (this.orders == null) {
this.orders = new LinkedHashMap<>();
}
this.orders.put(property, order);
return this;
}

public void addOrder(String property) {
public QuerySpec addOrder(String property) {
addOrder(property, Order.ASC);
return this;
}

public void addConstraint(Constraint constraint) {
public QuerySpec addConstraint(Constraint constraint) {
this.constraints.add(constraint);
return this;
}

@Override
Expand Down Expand Up @@ -107,18 +111,20 @@ public Integer getMaxResult() {
* @return the same query instance
* @throws IllegalArgumentException if the argument is negative
*/
public void setFirstResult(Integer firstResult) {
public QuerySpec setFirstResult(Integer firstResult) {
if (firstResult!=null && firstResult<0) throw new IllegalArgumentException();
this.firstResult = firstResult;
return this;
}

/**
* Set the maximum number of results to retrieve.
* @param maxResult maximum number of results to retrieve (may be null)
* @throws IllegalArgumentException if the argument is negative
*/
public void setMaxResult(Integer maxResult) {
public QuerySpec setMaxResult(Integer maxResult) {
if (maxResult!=null && maxResult<0) throw new IllegalArgumentException();
this.maxResult = maxResult;
return this;
}
}

0 comments on commit a852b5d

Please sign in to comment.