Skip to content

Commit

Permalink
Allow panache sorting by Sort.Column
Browse files Browse the repository at this point in the history
This feature simplify the usage of `io.quarkus.panache.common.Sort`
in a stream context.

Example with the parameters `List.of("-name", "status")`:

Before:

  ```java
  public Sort getSort(List<String> sorts) {
    var sort = Sort.empty();

    sorts.stream()
      .map(fields -> fields.startsWith("-")
          ? new Sort.Column(fields.substring(1), Sort.Direction.Descending)
          : new Sort.Column(fields, Sort.Direction.Ascending))
      .forEach(c -> sort.and(c.getName(), c.getDirection()));

    return sort;
  ```

After:

  ```java
  public Sort getSort(List<String> sorts) {
    return sorts.stream()
      .map(fields -> fields.startsWith("-")
          ? new Sort.Column(fields.substring(1), Sort.Direction.Descending)
          : new Sort.Column(fields, Sort.Direction.Ascending))
      .reduce(Sort::by);
  ```
  • Loading branch information
ggrebert committed Nov 14, 2024
1 parent 7fa4088 commit de6dc47
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,16 @@ public static Sort by(String... columns) {
return sort;
}

/**
* Sort by the given columns.
*
* @param columns the columns to sort on.
* @return a new Sort instance which sorts on the given columns.
*/
public static Sort by(Column column, Column... columns) {
return new Sort().and(column, columns);
}

/**
* Sort by the given columns, in ascending order. Equivalent to {@link #by(String...)}.
*
Expand Down Expand Up @@ -290,7 +300,20 @@ public Sort and(String name, NullPrecedence nullPrecedence) {
* @see #and(String)
*/
public Sort and(String name, Direction direction, NullPrecedence nullPrecedence) {
columns.add(new Column(name, direction, nullPrecedence));
return and(new Column(name, direction, nullPrecedence));
}

/**
* Add a sort column
*
* @param column the column to sort on with his properties
* @return this instance, modified.
*/
public Sort and(Column column, Column... columns) {
this.columns.add(column);
for (Column c : columns) {
this.columns.add(c);
}
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,10 +518,22 @@ private void testSorting() {

Sort sort1 = Sort.by("name", "status");
List<Person> order1 = Arrays.asList(person3, person2, person1);
List<Sort.Column> columns = Arrays.asList(
new Sort.Column("name", Sort.Direction.Descending),
new Sort.Column("status", Sort.Direction.Ascending));

List<Person> list = Person.findAll(sort1).list();
Assertions.assertEquals(order1, list);

list = Person.findAll(Sort.by(columns)).list();
Assertions.assertEquals(order1, list);

list = Person.findAll(Sort.empty().and(columns)).list();
Assertions.assertEquals(order1, list);

list = Person.findAll(columns.stream().reduce(Sort::by)).list();
Assertions.assertEquals(order1, list);

list = Person.listAll(sort1);
Assertions.assertEquals(order1, list);

Expand All @@ -534,6 +546,19 @@ private void testSorting() {
list = Person.find("name", sort2, "stef").list();
Assertions.assertEquals(order2, list);

columns = Arrays.asList(
new Sort.Column("name", Sort.Direction.Descending),
new Sort.Column("status", Sort.Direction.Ascending));

list = Person.find("name", Sort.by(columns), "stef").list();
Assertions.assertEquals(order2, list);

list = Person.findAll(columns.stream().reduce(Sort::by)).list();
Assertions.assertEquals(order2, list);

list = Person.find("name", Sort.empty().and(columns), "stef").list();
Assertions.assertEquals(order2, list);

list = Person.list("name", sort2, "stef");
Assertions.assertEquals(order2, list);

Expand Down

0 comments on commit de6dc47

Please sign in to comment.