/**
* Enumeration for sort directions.
* Sort
*/
public static enum Direction {
//ASC ,DESC
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);
}
public static Direction fromString(String value) {
try {
return Direction.valueOf(value.toUpperCase(Locale.US));
} catch (Exception e) {
throw new IllegalArgumentException(String.format(
"Invalid value '%s' for orders given! Has to be either 'desc' or 'asc' (case insensitive).", value), e);
}
}
public static Optional fromOptionalString(String value) {
try {
return Optional.of(fromString(value));
} catch (IllegalArgumentException e) {
return Optional.empty();
}
}
}
2.2、Orderクラス OrderクラスはSortクラスの内部クラスです
//sort
public static class Order implements Serializable {
private static final long serialVersionUID = 1522511010900108987L;
private static final boolean DEFAULT_IGNORE_CASE = false;
private static final NullHandling DEFAULT_NULL_HANDLING = NullHandling.NATIVE;
private final Direction direction;
private final String property;
private final boolean ignoreCase;
private final NullHandling nullHandling;
public Order(@Nullable Direction direction, String property) {
this(direction, property, DEFAULT_IGNORE_CASE, DEFAULT_NULL_HANDLING);
}
public Order(@Nullable Direction direction, String property, NullHandling nullHandlingHint) {
this(direction, property, DEFAULT_IGNORE_CASE, nullHandlingHint);
}
@Deprecated
public Order(String property) {
this(DEFAULT_DIRECTION, property);
}
public static Order by(String property) {
return new Order(DEFAULT_DIRECTION, property);
}
public static Order asc(String property) {
return new Order(Direction.ASC, property, DEFAULT_NULL_HANDLING);
}
public static Order desc(String property) {
return new Order(Direction.DESC, property, DEFAULT_NULL_HANDLING);
}
private Order(@Nullable Direction direction, String property, boolean ignoreCase, NullHandling nullHandling) {
if (!StringUtils.hasText(property)) {
throw new IllegalArgumentException("Property must not null or empty!");
}
this.direction = direction == null ? DEFAULT_DIRECTION : direction;
this.property = property;
this.ignoreCase = ignoreCase;
this.nullHandling = nullHandling;
}
public Direction getDirection() {
return direction;
}
public String getProperty() {
return property;
}
public boolean isAscending() {
return this.direction.isAscending();
}
public boolean isDescending() {
return this.direction.isDescending();
}
public boolean isIgnoreCase() {
return ignoreCase;
}
public Order with(Direction direction) {
return new Order(direction, this.property, this.ignoreCase, this.nullHandling);
}
public Order withProperty(String property) {
return new Order(this.direction, property, this.ignoreCase, this.nullHandling);
}
public Sort withProperties(String... properties) {
return Sort.by(this.direction, properties);
}
public Order ignoreCase() {
return new Order(direction, property, true, nullHandling);
}
public Order with(NullHandling nullHandling) {
return new Order(direction, this.property, ignoreCase, nullHandling);
}
public Order nullsFirst() {
return with(NullHandling.NULLS_FIRST);
}
public Order nullsLast() {
return with(NullHandling.NULLS_LAST);
}
public Order nullsNative() {
return with(NullHandling.NATIVE);
}
public NullHandling getNullHandling() {
return nullHandling;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + direction.hashCode();
result = 31 * result + property.hashCode();
result = 31 * result + (ignoreCase ? 1 : 0);
result = 31 * result + nullHandling.hashCode();
return result;
}
@Override
public boolean equals(@Nullable Object obj) {
if (this == obj) {
return true;
}
if (!(obj instanceof Order)) {
return false;
}
Order that = (Order) obj;
return this.direction.equals(that.direction) && this.property.equals(that.property)
&& this.ignoreCase == that.ignoreCase && this.nullHandling.equals(that.nullHandling);
}
@Override
public String toString() {
String result = String.format("%s: %s", property, direction);
if (!NullHandling.NATIVE.equals(nullHandling)) {
result += ", " + nullHandling;
}
if (ignoreCase) {
result += ", ignoring case";
}
return result;
}
}
2.3.Sortソートクラス 【Sortクラスの一部ソース】
public class Sort implements Streamable, Serializable {
private static final long serialVersionUID = 5737186511678863905L;
//
private static final Sort UNSORTED = Sort.by(new Order[0]);
public static final Direction DEFAULT_DIRECTION = Direction.ASC;
private final List orders;
//@Deprecated ,
@Deprecated
public Sort(Order... orders) {
this(Arrays.asList(orders));
}
//@Deprecated ,
@Deprecated
public Sort(List orders) {
Assert.notNull(orders, "Orders must not be null!");
this.orders = Collections.unmodifiableList(orders);
}
//@Deprecated ,
@Deprecated
public Sort(String... properties) {
this(DEFAULT_DIRECTION, properties);
}
public Sort(Direction direction, String... properties) {
this(direction, properties == null ? new ArrayList<>() : Arrays.asList(properties));
}
public Sort(Direction direction, List properties) {
if (properties == null || properties.isEmpty()) {
throw new IllegalArgumentException("You have to provide at least one property to sort by!");
}
this.orders = new ArrayList<>(properties.size());
for (String property : properties) {
this.orders.add(new Order(direction, property));
}
}
public static Sort by(String... properties) {
Assert.notNull(properties, "Properties must not be null!");
return properties.length == 0 ? Sort.unsorted() : new Sort(properties);
}
public static Sort by(List orders) {
Assert.notNull(orders, "Orders must not be null!");
return orders.isEmpty() ? Sort.unsorted() : new Sort(orders);
}
public static Sort by(Order... orders) {
Assert.notNull(orders, "Orders must not be null!");
return new Sort(orders);
}
public static Sort by(Direction direction, String... properties) {
Assert.notNull(direction, "Direction must not be null!");
Assert.notNull(properties, "Properties must not be null!");
Assert.isTrue(properties.length > 0, "At least one property must be given!");
return Sort.by(Arrays.stream(properties)//
.map(it -> new Order(direction, it))//
.collect(Collectors.toList()));
}
public static Sort unsorted() {
return UNSORTED;
}
public Sort descending() {
return withDirection(Direction.DESC);
}
public Sort ascending() {
return withDirection(Direction.ASC);
}
public boolean isSorted() {
return !orders.isEmpty();
}
public boolean isUnsorted() {
return !isSorted();
}
//............................. ...................................
}
3、応用例 3.1、実体類
@Entity
@Table(name="tb_label")
public class Label implements Serializable {
@Id
private String id;//
private String labelname;//
private String state;//
private Long count;//
private Long fans;//
private String recommend;//
public Label() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLabelname() {
return labelname;
}
public void setLabelname(String labelname) {
this.labelname = labelname;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Long getCount() {
return count;
}
public void setCount(Long count) {
this.count = count;
}
public Long getFans() {
return fans;
}
public void setFans(Long fans) {
this.fans = fans;
}
public String getRecommend() {
return recommend;
}
public void setRecommend(String recommend) {
this.recommend = recommend;
}
@Override
public String toString() {
return "Label{" +
"id='" + id + '\'' +
", labelname='" + labelname + '\'' +
", state='" + state + '\'' +
", count=" + count +
", fans=" + fans +
", recommend='" + recommend + '\'' +
'}';
}
}