Skip to content

Commit a588f43

Browse files
kraymondkscodrotbohm
authored andcommitted
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.
1 parent b26f09f commit a588f43

File tree

2 files changed

+73
-19
lines changed

2 files changed

+73
-19
lines changed

src/main/java/org/springframework/data/domain/Sort.java

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,13 +234,16 @@ public static Direction fromStringOrNull(String value) {
234234
* {@link Sort}
235235
*
236236
* @author Oliver Gierke
237+
* @author Kevin Raymond
237238
*/
238239
public static class Order implements Serializable {
239240

240241
private static final long serialVersionUID = 1522511010900108987L;
242+
private static final boolean DEFAULT_IGNORE_CASE = false;
241243

242244
private final Direction direction;
243245
private final String property;
246+
private final boolean ignoreCase;
244247

245248
/**
246249
* Creates a new {@link Order} instance. if order is {@literal null} then order defaults to
@@ -251,12 +254,7 @@ public static class Order implements Serializable {
251254
*/
252255
public Order(Direction direction, String property) {
253256

254-
if (!StringUtils.hasText(property)) {
255-
throw new IllegalArgumentException("Property must not null or empty!");
256-
}
257-
258-
this.direction = direction == null ? DEFAULT_DIRECTION : direction;
259-
this.property = property;
257+
this(direction, property, DEFAULT_IGNORE_CASE);
260258
}
261259

262260
/**
@@ -269,6 +267,25 @@ public Order(String property) {
269267
this(DEFAULT_DIRECTION, property);
270268
}
271269

270+
/**
271+
* Creates a new {@link Order} instance. if order is {@literal null} then order defaults to
272+
* {@link Sort#DEFAULT_DIRECTION}
273+
*
274+
* @param direction can be {@literal null}, will default to {@link Sort#DEFAULT_DIRECTION}
275+
* @param property must not be {@literal null} or empty.
276+
* @param ignoreCase true if sorting should be case insensitive. false if sorting should be case sensitive.
277+
*/
278+
private Order(Direction direction, String property, boolean ignoreCase) {
279+
280+
if (!StringUtils.hasText(property)) {
281+
throw new IllegalArgumentException("Property must not null or empty!");
282+
}
283+
284+
this.direction = direction == null ? DEFAULT_DIRECTION : direction;
285+
this.property = property;
286+
this.ignoreCase = ignoreCase;
287+
}
288+
272289
/**
273290
* @deprecated use {@link Sort#Sort(Direction, List)} instead.
274291
*/
@@ -309,6 +326,15 @@ public boolean isAscending() {
309326
return this.direction.equals(Direction.ASC);
310327
}
311328

329+
/**
330+
* Returns whether or not the sort will be case sensitive.
331+
*
332+
* @return
333+
*/
334+
public boolean isIgnoreCase() {
335+
return ignoreCase;
336+
}
337+
312338
/**
313339
* Returns a new {@link Order} with the given {@link Order}.
314340
*
@@ -329,6 +355,15 @@ public Sort withProperties(String... properties) {
329355
return new Sort(this.direction, properties);
330356
}
331357

358+
/**
359+
* Returns a new {@link Order} with case insensitive sorting enabled.
360+
*
361+
* @return
362+
*/
363+
public Order ignoreCase() {
364+
return new Order(direction, property, true);
365+
}
366+
332367
/*
333368
* (non-Javadoc)
334369
* @see java.lang.Object#hashCode()

src/test/java/org/springframework/data/domain/SortUnitTests.java

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
/*
2-
* Copyright 2008-2010 the original author or authors.
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5-
* use this file except in compliance with the License. You may obtain a copy of
6-
* the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
2+
* Copyright 2008-2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12-
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13-
* License for the specific language governing permissions and limitations under
14-
* the License.
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
1515
*/
16-
1716
package org.springframework.data.domain;
1817

1918
import static org.hamcrest.CoreMatchers.*;
2019
import static org.junit.Assert.*;
2120

2221
import org.junit.Test;
2322
import org.springframework.data.domain.Sort.Direction;
23+
import org.springframework.data.domain.Sort.Order;
2424

2525
/**
2626
* Unit test for {@link Sort}.
2727
*
2828
* @author Oliver Gierke
29+
* @author Kevin Raymond
2930
*/
3031
public class SortUnitTests {
3132

@@ -98,4 +99,22 @@ public void handlesAdditionalNullSort() {
9899
Sort sort = new Sort("foo").and(null);
99100
assertThat(sort, hasItem(new Sort.Order("foo")));
100101
}
102+
103+
/**
104+
* @see DATACMNS-281
105+
* @author Kevin Raymond
106+
*/
107+
@Test
108+
public void configuresIgnoreCaseForOrder() {
109+
assertThat(new Order(Direction.ASC, "foo").ignoreCase().isIgnoreCase(), is(true));
110+
}
111+
112+
/**
113+
* @see DATACMNS-281
114+
* @author Kevin Raymond
115+
*/
116+
@Test
117+
public void orderDoesNotIgnoreCaseByDefault() {
118+
assertThat(new Order(Direction.ASC, "foo").isIgnoreCase(), is(false));
119+
}
101120
}

0 commit comments

Comments
 (0)