DATACMNS-281 - Added support to Order for case insensitive sorts.

Modified the Order class to allow an ignore case flag to be set so that case insensitive sorts could be supported.
This commit is contained in:
Kevin Raymond
2013-02-08 16:54:16 -05:00
committed by Oliver Gierke
parent b26f09f3b4
commit a588f43857
2 changed files with 73 additions and 19 deletions

View File

@@ -234,13 +234,16 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
* {@link Sort}
*
* @author Oliver Gierke
* @author Kevin Raymond
*/
public static class Order implements Serializable {
private static final long serialVersionUID = 1522511010900108987L;
private static final boolean DEFAULT_IGNORE_CASE = false;
private final Direction direction;
private final String property;
private final boolean ignoreCase;
/**
* Creates a new {@link Order} instance. if order is {@literal null} then order defaults to
@@ -251,12 +254,7 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
*/
public Order(Direction direction, String property) {
if (!StringUtils.hasText(property)) {
throw new IllegalArgumentException("Property must not null or empty!");
}
this.direction = direction == null ? DEFAULT_DIRECTION : direction;
this.property = property;
this(direction, property, DEFAULT_IGNORE_CASE);
}
/**
@@ -269,6 +267,25 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
this(DEFAULT_DIRECTION, property);
}
/**
* Creates a new {@link Order} instance. if order is {@literal null} then order defaults to
* {@link Sort#DEFAULT_DIRECTION}
*
* @param direction can be {@literal null}, will default to {@link Sort#DEFAULT_DIRECTION}
* @param property must not be {@literal null} or empty.
* @param ignoreCase true if sorting should be case insensitive. false if sorting should be case sensitive.
*/
private Order(Direction direction, String property, boolean ignoreCase) {
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;
}
/**
* @deprecated use {@link Sort#Sort(Direction, List)} instead.
*/
@@ -309,6 +326,15 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
return this.direction.equals(Direction.ASC);
}
/**
* Returns whether or not the sort will be case sensitive.
*
* @return
*/
public boolean isIgnoreCase() {
return ignoreCase;
}
/**
* Returns a new {@link Order} with the given {@link Order}.
*
@@ -329,6 +355,15 @@ public class Sort implements Iterable<org.springframework.data.domain.Sort.Order
return new Sort(this.direction, properties);
}
/**
* Returns a new {@link Order} with case insensitive sorting enabled.
*
* @return
*/
public Order ignoreCase() {
return new Order(direction, property, true);
}
/*
* (non-Javadoc)
* @see java.lang.Object#hashCode()

View File

@@ -1,19 +1,18 @@
/*
* Copyright 2008-2010 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
*
* Copyright 2008-2013 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.
* 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.domain;
import static org.hamcrest.CoreMatchers.*;
@@ -21,11 +20,13 @@ import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Sort.Order;
/**
* Unit test for {@link Sort}.
*
* @author Oliver Gierke
* @author Kevin Raymond
*/
public class SortUnitTests {
@@ -98,4 +99,22 @@ public class SortUnitTests {
Sort sort = new Sort("foo").and(null);
assertThat(sort, hasItem(new Sort.Order("foo")));
}
/**
* @see DATACMNS-281
* @author Kevin Raymond
*/
@Test
public void configuresIgnoreCaseForOrder() {
assertThat(new Order(Direction.ASC, "foo").ignoreCase().isIgnoreCase(), is(true));
}
/**
* @see DATACMNS-281
* @author Kevin Raymond
*/
@Test
public void orderDoesNotIgnoreCaseByDefault() {
assertThat(new Order(Direction.ASC, "foo").isIgnoreCase(), is(false));
}
}