DATACMNS-888 - Added dedicated Sort type to be able to express directions for revisions.

Introduced dedicated RevisionSort property that allows to define the sort order independent of the revision property to be used.

Introduced methods to find out whether a Sort is descending or ascending to avoid having to refer to the enum.
This commit is contained in:
Oliver Gierke
2016-07-26 14:36:00 +02:00
parent 8bc022ebd7
commit acf12423e0
3 changed files with 118 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2014 the original author or authors.
* Copyright 2008-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -196,6 +196,26 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
ASC, DESC;
/**
* Returns whether the direction is ascending.
*
* @return
* @since 1.13
*/
public boolean isAscending() {
return this.equals(ASC);
}
/**
* Returns whether the direction is descending.
*
* @return
* @since 1.13
*/
public boolean isDescending() {
return this.equals(DESC);
}
/**
* Returns the {@link Direction} enum for the given {@link String} value.
*
@@ -350,7 +370,17 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
* @return
*/
public boolean isAscending() {
return this.direction.equals(Direction.ASC);
return this.direction.isAscending();
}
/**
* Returns whether sorting for this property shall be descending.
*
* @return
* @since 1.13
*/
public boolean isDescending() {
return this.direction.isDescending();
}
/**

View File

@@ -0,0 +1,81 @@
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.history;
import org.springframework.data.domain.Sort;
/**
* A dedicated {@link Sort} implementation that allows the definition of the ordering of revisions independently of the
* property name the revision number is held in.
*
* @author Oliver Gierke
* @since 1.13
* @soundtrack Benny Greb's Moving Parts - Soulfood (Live)
*/
public class RevisionSort extends Sort {
private static final long serialVersionUID = 618238321589063537L;
private static final String PROPERTY = "__revisionNumber__";
private static final RevisionSort ASC = new RevisionSort(Direction.ASC);
private static final RevisionSort DESC = new RevisionSort(Direction.DESC);
/**
* Creates a new {@link RevisionSort} using the given direction for sorting by revision number.
*
* @param direction must not be {@literal null}.
*/
private RevisionSort(Direction direction) {
super(direction, PROPERTY);
}
/**
* Creates a {@link RevisionSort} with ascending order for the revision number property, i.e. more recent revisions
* will come last.
*
* @return
*/
public static RevisionSort asc() {
return ASC;
}
/**
* Creates a {@link RevisionSort} with descending order for the revision number property, i.e. more recent revisions
* will come first.
*
* @return
*/
public static RevisionSort desc() {
return DESC;
}
/**
* Returns in which direction to sort revisions for the given {@link Sort} instance. Defaults to
* {@link Direction#ASC}.
*
* @param sort can be {@literal null}.
* @return
*/
public static Direction getRevisionDirection(Sort sort) {
if (sort == null) {
return Direction.ASC;
}
Order order = sort.getOrderFor(PROPERTY);
return order == null ? Direction.ASC : order.getDirection();
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import java.io.Serializable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.history.Revision;
import org.springframework.data.history.RevisionSort;
import org.springframework.data.history.Revisions;
import org.springframework.data.repository.NoRepositoryBean;
@@ -49,10 +50,12 @@ public interface RevisionRepository<T, ID extends Serializable, N extends Number
Revisions<N, T> findRevisions(ID id);
/**
* Returns a {@link Page} of revisions for the entity with the given id.
* Returns a {@link Page} of revisions for the entity with the given id. Note, that it's not guaranteed that
* implementations have to support sorting by all properties.
*
* @param id must not be {@literal null}.
* @param pageable
* @see RevisionSort
* @return
*/
Page<Revision<N, T>> findRevisions(ID id, Pageable pageable);