Skip to content

Commit

Permalink
Overload KiwiSort#of factory method with String direction
Browse files Browse the repository at this point in the history
Add overloads of KiwiSort#of factory method to accept direction as a
String. Also add an overload to allow caller to specify a Locale for
converting the given String value to uppercase.

Closes #772
  • Loading branch information
sleberknight committed Sep 1, 2022
1 parent edd156b commit b1ea07b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
40 changes: 39 additions & 1 deletion src/main/java/org/kiwiproject/spring/data/KiwiSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,44 @@ public static Direction fromString(String value, Locale locale) {
private boolean ignoreCase;
private boolean ascending;

/**
* Create a new instance.
* <p>
* If you want to specify that the sort is not case-sensitive, you can immediately call the
* {@link #ignoringCase()} in a fluent style.
*
* @param property the property the sort is applied to
* @param direction the sort direction as a String, which must resolve via {@link Direction#fromString(String)}
* in the default {@link Locale}
* @return a new instance
* @throws IllegalArgumentException if property is blank or direction is blank or invalid
*/
public static KiwiSort of(String property, String direction) {
return of(property, direction, Locale.getDefault());
}

/**
* Create a new instance.
* <p>
* If you want to specify that the sort is not case-sensitive, you can immediately call the
* {@link #ignoringCase()} in a fluent style.
*
* @param property the property the sort is applied to
* @param direction the sort direction as a String, which must resolve via {@link Direction#fromString(String)}
* in the given {@link Locale}
* @param locale the Locale to use to uppercase the value
* @return a new instance
* @throws IllegalArgumentException if property is blank or direction is blank or invalid
*/
public static KiwiSort of(String property, String direction, Locale locale) {
checkArgumentNotBlank(property);
checkArgumentNotBlank(direction);
checkArgumentNotNull(locale);

var directionEnum = Direction.fromString(direction, locale);
return of(property, directionEnum);
}

/**
* Create a new instance.
* <p>
Expand All @@ -109,7 +147,7 @@ public static KiwiSort of(String property, KiwiSort.Direction direction) {
}

/**
* Specifies that the sort is <em>not</em> case sensitive, i.e. it ignores case.
* Specifies that the sort is <em>not</em> case-sensitive, i.e. it ignores case.
*
* @return this instance, for method chaining
*/
Expand Down
27 changes: 25 additions & 2 deletions src/test/java/org/kiwiproject/spring/data/KiwiSortTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
class KiwiSortTest {

@Test
void shouldCreateNewInstanceUsingFactoryMethod(SoftAssertions softly) {
void shouldCreateNewInstanceUsing_DirectionEnumFactoryMethod(SoftAssertions softly) {
var sort = KiwiSort.of("someProperty", KiwiSort.Direction.DESC);

softly.assertThat(sort.getProperty()).isEqualTo("someProperty");
Expand All @@ -36,14 +36,37 @@ void shouldCreateNewInstanceUsingFactoryMethod(SoftAssertions softly) {
softly.assertThat(sort.isIgnoreCase()).isFalse();
}

@Test
void shouldCreateNewInstanceUsing_DirectionStringFactoryMethod(SoftAssertions softly) {
var sort = KiwiSort.of("someProperty", "asc");

softly.assertThat(sort.getProperty()).isEqualTo("someProperty");
softly.assertThat(sort.getDirection()).isEqualTo("ASC");
softly.assertThat(sort.isAscending()).isTrue();
softly.assertThat(sort.isIgnoreCase()).isFalse();
}

@ParameterizedTest
@CsvSource({
" , ASC",
" '' , ASC",
" ' ' , DESC",
" lastName, ",
})
void shouldValidateArgumentsWhenUsing_DirectionEnumFactoryMethod(String property, KiwiSort.Direction direction) {
assertThatIllegalArgumentException()
.isThrownBy(() -> KiwiSort.of(property, direction));
}

@ParameterizedTest
@CsvSource({
" , ASC",
" '' , ASC",
" ' ' , DESC",
" lastName, ",
" lastName, DIAGONALLY",
})
void shouldValidateArgumentsWhenUsingFactoryMethod(String property, KiwiSort.Direction direction) {
void shouldValidateArgumentsWhenUsing_DirectionStringFactoryMethod(String property, String direction) {
assertThatIllegalArgumentException()
.isThrownBy(() -> KiwiSort.of(property, direction));
}
Expand Down

0 comments on commit b1ea07b

Please sign in to comment.