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 a0075ad
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,36 @@ 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.
*
* @param columns the columns to sort on.
* @return a new Sort instance which sorts on the given columns.
*/
public static Sort by(Column column) {
return new Sort().and(column);
}

/**
* 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(List<Column> columns) {
return new Sort().and(columns);
}

/**
* Sort by the given columns, in ascending order. Equivalent to {@link #by(String...)}.
*
Expand Down Expand Up @@ -243,16 +273,54 @@ public Sort direction(Direction direction) {
return this;
}

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

/**
* 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);
if (columns != null) {
for (Column c : columns) {
this.columns.add(c);
}
}
return this;
}

/**
* Add a sort column
*
* @param column the column to sort on with his properties
* @return this instance, modified.
*/
public Sort and(List<Column> columns) {
this.columns.addAll(columns);
return this;
}

/**
* Adds a sort column, in ascending order.
*
* @param name the new column to sort on, in ascending order.
* @return this instance, modified.
* @see #and(String, Direction)
* @see #and(Column...)
*/
public Sort and(String name) {
columns.add(new Column(name));
return this;
return and(new Column(name));
}

/**
Expand All @@ -262,10 +330,10 @@ public Sort and(String name) {
* @param direction the direction to sort on.
* @return this instance, modified.
* @see #and(String)
* @see #and(Column...)
*/
public Sort and(String name, Direction direction) {
columns.add(new Column(name, direction));
return this;
return and(new Column(name, direction));
}

/**
Expand All @@ -275,6 +343,7 @@ public Sort and(String name, Direction direction) {
* @param nullPrecedence the null precedence to use.
* @return this instance, modified.
* @see #and(String)
* @see #and(Column...)
*/
public Sort and(String name, NullPrecedence nullPrecedence) {
return and(name, Direction.Ascending, nullPrecedence);
Expand All @@ -288,10 +357,10 @@ public Sort and(String name, NullPrecedence nullPrecedence) {
* @param nullPrecedence the null precedence to use.
* @return this instance, modified.
* @see #and(String)
* @see #and(Column...)
*/
public Sort and(String name, Direction direction, NullPrecedence nullPrecedence) {
columns.add(new Column(name, direction, nullPrecedence));
return this;
return and(new Column(name, direction, nullPrecedence));
}

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

Sort sort1 = Sort.by("name", "status");
List<Person> order1 = Arrays.asList(person3, person2, person1);
Sort.Column[] columns = {
new Sort.Column("name", Sort.Direction.Descending),
new Sort.Column("status", Sort.Direction.Ascending) };
List<Column> columnList = new ArrayList(
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(Sort.by(columnList)).list();
Assertions.assertEquals(order1, list);

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

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

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

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

columns={new Sort.Column("name",Sort.Direction.Ascending),new Sort.Column("status",Sort.Direction.Descending)};
columnList = new ArrayList(
new Sort.Column("name", Sort.Direction.Ascending),
new Sort.Column("status", Sort.Direction.Descending));

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

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

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

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

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

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

Expand Down

0 comments on commit a0075ad

Please sign in to comment.