diff --git a/src/main/java/org/mybatis/dynamic/sql/util/StringUtilities.java b/src/main/java/org/mybatis/dynamic/sql/util/StringUtilities.java index 25a7e390c..aad161f0c 100644 --- a/src/main/java/org/mybatis/dynamic/sql/util/StringUtilities.java +++ b/src/main/java/org/mybatis/dynamic/sql/util/StringUtilities.java @@ -15,10 +15,6 @@ */ package org.mybatis.dynamic.sql.util; -import java.util.function.Function; - -import org.jspecify.annotations.Nullable; - public interface StringUtilities { static String spaceAfter(String in) { @@ -29,10 +25,6 @@ static String spaceBefore(String in) { return " " + in; //$NON-NLS-1$ } - static @Nullable String safelyUpperCase(@Nullable String s) { - return s == null ? null : s.toUpperCase(); - } - static String toCamelCase(String inputString) { StringBuilder sb = new StringBuilder(); @@ -62,7 +54,13 @@ static String formatConstantForSQL(String in) { return "'" + escaped + "'"; //$NON-NLS-1$ //$NON-NLS-2$ } - static Function mapToUpperCase(Function f) { - return f.andThen(String::toUpperCase); + static T upperCaseIfPossible(T value) { + if (value instanceof String) { + @SuppressWarnings("unchecked") + T t = (T) ((String) value).toUpperCase(); + return t; + } + + return value; } } diff --git a/src/main/java/org/mybatis/dynamic/sql/where/condition/IsInCaseInsensitive.java b/src/main/java/org/mybatis/dynamic/sql/where/condition/IsInCaseInsensitive.java index dc3b0f8f1..bc8bfbdf9 100644 --- a/src/main/java/org/mybatis/dynamic/sql/where/condition/IsInCaseInsensitive.java +++ b/src/main/java/org/mybatis/dynamic/sql/where/condition/IsInCaseInsensitive.java @@ -38,7 +38,7 @@ public static IsInCaseInsensitive empty() { } protected IsInCaseInsensitive(Collection values) { - super(values); + super(values.stream().map(StringUtilities::upperCaseIfPossible).toList()); } @Override @@ -57,19 +57,6 @@ public IsInCaseInsensitive filter(Predicate predicate) { return filterSupport(predicate, IsInCaseInsensitive::new, this, IsInCaseInsensitive::empty); } - /** - * If renderable, apply the mapping to the value and return a new condition with the new value. Else return a - * condition that will not render (this). - * - *

This function DOES NOT automatically transform values to uppercase, so it potentially creates a - * case-sensitive query. For String conditions you can use {@link StringUtilities#mapToUpperCase(Function)} - * to add an uppercase transform after your mapping function. - * - * @param mapper a mapping function to apply to the value, if renderable - * @param type of the new condition - * @return a new condition with the result of applying the mapper to the value of this condition, - * if renderable, otherwise a condition that will not render. - */ @Override public IsInCaseInsensitive map(Function mapper) { return mapSupport(mapper, IsInCaseInsensitive::new, IsInCaseInsensitive::empty); @@ -80,7 +67,6 @@ public static IsInCaseInsensitive of(String... values) { } public static IsInCaseInsensitive of(Collection values) { - // Keep the null safe upper case utility for backwards compatibility in case someone passes in a null - return new IsInCaseInsensitive<>(values.stream().map(StringUtilities::safelyUpperCase).toList()); + return new IsInCaseInsensitive<>(values); } } diff --git a/src/main/java/org/mybatis/dynamic/sql/where/condition/IsInCaseInsensitiveWhenPresent.java b/src/main/java/org/mybatis/dynamic/sql/where/condition/IsInCaseInsensitiveWhenPresent.java index 283e62fc7..fee942f7d 100644 --- a/src/main/java/org/mybatis/dynamic/sql/where/condition/IsInCaseInsensitiveWhenPresent.java +++ b/src/main/java/org/mybatis/dynamic/sql/where/condition/IsInCaseInsensitiveWhenPresent.java @@ -39,7 +39,7 @@ public static IsInCaseInsensitiveWhenPresent empty() { } protected IsInCaseInsensitiveWhenPresent(Collection values) { - super(values); + super(values.stream().filter(Objects::nonNull).map(StringUtilities::upperCaseIfPossible).toList()); } @Override @@ -53,19 +53,6 @@ public IsInCaseInsensitiveWhenPresent filter(Predicate predicate) IsInCaseInsensitiveWhenPresent::empty); } - /** - * If renderable, apply the mapping to the value and return a new condition with the new value. Else return a - * condition that will not render (this). - * - *

This function DOES NOT automatically transform values to uppercase, so it potentially creates a - * case-sensitive query. For String conditions you can use {@link StringUtilities#mapToUpperCase(Function)} - * to add an uppercase transform after your mapping function. - * - * @param mapper a mapping function to apply to the value, if renderable - * @param type of the new condition - * @return a new condition with the result of applying the mapper to the value of this condition, - * if renderable, otherwise a condition that will not render. - */ @Override public IsInCaseInsensitiveWhenPresent map(Function mapper) { return mapSupport(mapper, IsInCaseInsensitiveWhenPresent::new, IsInCaseInsensitiveWhenPresent::empty); @@ -76,7 +63,6 @@ public static IsInCaseInsensitiveWhenPresent of(@Nullable String... valu } public static IsInCaseInsensitiveWhenPresent of(Collection<@Nullable String> values) { - return new IsInCaseInsensitiveWhenPresent<>( - values.stream().filter(Objects::nonNull).map(String::toUpperCase).toList()); + return new IsInCaseInsensitiveWhenPresent<>(values); } } diff --git a/src/main/java/org/mybatis/dynamic/sql/where/condition/IsLikeCaseInsensitive.java b/src/main/java/org/mybatis/dynamic/sql/where/condition/IsLikeCaseInsensitive.java index 6b569b32d..e4d31c126 100644 --- a/src/main/java/org/mybatis/dynamic/sql/where/condition/IsLikeCaseInsensitive.java +++ b/src/main/java/org/mybatis/dynamic/sql/where/condition/IsLikeCaseInsensitive.java @@ -44,7 +44,7 @@ public static IsLikeCaseInsensitive empty() { } protected IsLikeCaseInsensitive(T value) { - super(value); + super(StringUtilities.upperCaseIfPossible(value)); } @Override @@ -57,26 +57,12 @@ public IsLikeCaseInsensitive filter(Predicate predicate) { return filterSupport(predicate, IsLikeCaseInsensitive::empty, this); } - /** - * If renderable, apply the mapping to the value and return a new condition with the new value. Else return a - * condition that will not render (this). - * - *

This function DOES NOT automatically transform values to uppercase, so it potentially creates a - * case-sensitive query. For String conditions you can use {@link StringUtilities#mapToUpperCase(Function)} - * to add an uppercase transform after your mapping function. - * - * @param mapper a mapping function to apply to the value, if renderable - * @param type of the new condition - * @return a new condition with the result of applying the mapper to the value of this condition, - * if renderable, otherwise a condition that will not render. - */ @Override public IsLikeCaseInsensitive map(Function mapper) { return mapSupport(mapper, IsLikeCaseInsensitive::new, IsLikeCaseInsensitive::empty); } public static IsLikeCaseInsensitive of(String value) { - // Keep the null safe upper case utility for backwards compatibility in case someone passes in a null - return new IsLikeCaseInsensitive<>(StringUtilities.safelyUpperCase(value)); + return new IsLikeCaseInsensitive<>(value); } } diff --git a/src/main/java/org/mybatis/dynamic/sql/where/condition/IsNotInCaseInsensitive.java b/src/main/java/org/mybatis/dynamic/sql/where/condition/IsNotInCaseInsensitive.java index 8731d5b64..4486d1df7 100644 --- a/src/main/java/org/mybatis/dynamic/sql/where/condition/IsNotInCaseInsensitive.java +++ b/src/main/java/org/mybatis/dynamic/sql/where/condition/IsNotInCaseInsensitive.java @@ -38,7 +38,7 @@ public static IsNotInCaseInsensitive empty() { } protected IsNotInCaseInsensitive(Collection values) { - super(values); + super(values.stream().map(StringUtilities::upperCaseIfPossible).toList()); } @Override @@ -57,19 +57,6 @@ public IsNotInCaseInsensitive filter(Predicate predicate) { return filterSupport(predicate, IsNotInCaseInsensitive::new, this, IsNotInCaseInsensitive::empty); } - /** - * If renderable, apply the mapping to the value and return a new condition with the new value. Else return a - * condition that will not render (this). - * - *

This function DOES NOT automatically transform values to uppercase, so it potentially creates a - * case-sensitive query. For String conditions you can use {@link StringUtilities#mapToUpperCase(Function)} - * to add an uppercase transform after your mapping function. - * - * @param mapper a mapping function to apply to the value, if renderable - * @param type of the new condition - * @return a new condition with the result of applying the mapper to the value of this condition, - * if renderable, otherwise a condition that will not render. - */ @Override public IsNotInCaseInsensitive map(Function mapper) { return mapSupport(mapper, IsNotInCaseInsensitive::new, IsNotInCaseInsensitive::empty); @@ -80,7 +67,6 @@ public static IsNotInCaseInsensitive of(String... values) { } public static IsNotInCaseInsensitive of(Collection values) { - // Keep the null safe upper case utility for backwards compatibility in case someone passes in a null - return new IsNotInCaseInsensitive<>(values.stream().map(StringUtilities::safelyUpperCase).toList()); + return new IsNotInCaseInsensitive<>(values); } } diff --git a/src/main/java/org/mybatis/dynamic/sql/where/condition/IsNotInCaseInsensitiveWhenPresent.java b/src/main/java/org/mybatis/dynamic/sql/where/condition/IsNotInCaseInsensitiveWhenPresent.java index 438e1c5ed..992a231eb 100644 --- a/src/main/java/org/mybatis/dynamic/sql/where/condition/IsNotInCaseInsensitiveWhenPresent.java +++ b/src/main/java/org/mybatis/dynamic/sql/where/condition/IsNotInCaseInsensitiveWhenPresent.java @@ -39,7 +39,7 @@ public static IsNotInCaseInsensitiveWhenPresent empty() { } protected IsNotInCaseInsensitiveWhenPresent(Collection values) { - super(values); + super(values.stream().filter(Objects::nonNull).map(StringUtilities::upperCaseIfPossible).toList()); } @Override @@ -53,19 +53,6 @@ public IsNotInCaseInsensitiveWhenPresent filter(Predicate predicat this, IsNotInCaseInsensitiveWhenPresent::empty); } - /** - * If renderable, apply the mapping to the value and return a new condition with the new value. Else return a - * condition that will not render (this). - * - *

This function DOES NOT automatically transform values to uppercase, so it potentially creates a - * case-sensitive query. For String conditions you can use {@link StringUtilities#mapToUpperCase(Function)} - * to add an uppercase transform after your mapping function. - * - * @param mapper a mapping function to apply to the value, if renderable - * @param type of the new condition - * @return a new condition with the result of applying the mapper to the value of this condition, - * if renderable, otherwise a condition that will not render. - */ @Override public IsNotInCaseInsensitiveWhenPresent map(Function mapper) { return mapSupport(mapper, IsNotInCaseInsensitiveWhenPresent::new, IsNotInCaseInsensitiveWhenPresent::empty); @@ -76,7 +63,6 @@ public static IsNotInCaseInsensitiveWhenPresent of(@Nullable String... v } public static IsNotInCaseInsensitiveWhenPresent of(Collection<@Nullable String> values) { - return new IsNotInCaseInsensitiveWhenPresent<>( - values.stream().filter(Objects::nonNull).map(String::toUpperCase).toList()); + return new IsNotInCaseInsensitiveWhenPresent<>(values); } } diff --git a/src/main/java/org/mybatis/dynamic/sql/where/condition/IsNotLikeCaseInsensitive.java b/src/main/java/org/mybatis/dynamic/sql/where/condition/IsNotLikeCaseInsensitive.java index 8eb65d772..715a19a8d 100644 --- a/src/main/java/org/mybatis/dynamic/sql/where/condition/IsNotLikeCaseInsensitive.java +++ b/src/main/java/org/mybatis/dynamic/sql/where/condition/IsNotLikeCaseInsensitive.java @@ -44,7 +44,7 @@ public static IsNotLikeCaseInsensitive empty() { } protected IsNotLikeCaseInsensitive(T value) { - super(value); + super(StringUtilities.upperCaseIfPossible(value)); } @Override @@ -57,26 +57,12 @@ public IsNotLikeCaseInsensitive filter(Predicate predicate) { return filterSupport(predicate, IsNotLikeCaseInsensitive::empty, this); } - /** - * If renderable, apply the mapping to the value and return a new condition with the new value. Else return a - * condition that will not render (this). - * - *

This function DOES NOT automatically transform values to uppercase, so it potentially creates a - * case-sensitive query. For String conditions you can use {@link StringUtilities#mapToUpperCase(Function)} - * to add an uppercase transform after your mapping function. - * - * @param mapper a mapping function to apply to the value, if renderable - * @param type of the new condition - * @return a new condition with the result of applying the mapper to the value of this condition, - * if renderable, otherwise a condition that will not render. - */ @Override public IsNotLikeCaseInsensitive map(Function mapper) { return mapSupport(mapper, IsNotLikeCaseInsensitive::new, IsNotLikeCaseInsensitive::empty); } public static IsNotLikeCaseInsensitive of(String value) { - // Keep the null safe upper case utility for backwards compatibility in case someone passes in a null - return new IsNotLikeCaseInsensitive<>(StringUtilities.safelyUpperCase(value)); + return new IsNotLikeCaseInsensitive<>(value); } } diff --git a/src/test/java/org/mybatis/dynamic/sql/util/StringUtilitiesTest.java b/src/test/java/org/mybatis/dynamic/sql/util/StringUtilitiesTest.java index 1c54cafc9..e1d8c137c 100644 --- a/src/test/java/org/mybatis/dynamic/sql/util/StringUtilitiesTest.java +++ b/src/test/java/org/mybatis/dynamic/sql/util/StringUtilitiesTest.java @@ -38,4 +38,16 @@ void testNumeric() { String input = "USER%NAME%3"; assertThat(StringUtilities.toCamelCase(input)).isEqualTo("userName3"); } + + @Test + void testUpperCaseInteger() { + Integer i = StringUtilities.upperCaseIfPossible(3); + assertThat(i).isEqualTo(3); + } + + @Test + void testUpperCaseString() { + String i = StringUtilities.upperCaseIfPossible("fred"); + assertThat(i).isEqualTo("FRED"); + } } diff --git a/src/test/java/org/mybatis/dynamic/sql/where/condition/FilterAndMapTest.java b/src/test/java/org/mybatis/dynamic/sql/where/condition/FilterAndMapTest.java index 7d4e4505e..bd2e56fbb 100644 --- a/src/test/java/org/mybatis/dynamic/sql/where/condition/FilterAndMapTest.java +++ b/src/test/java/org/mybatis/dynamic/sql/where/condition/FilterAndMapTest.java @@ -24,7 +24,6 @@ import org.junit.jupiter.api.Test; import org.mybatis.dynamic.sql.SqlBuilder; -import org.mybatis.dynamic.sql.util.StringUtilities; class FilterAndMapTest { @Test @@ -543,31 +542,17 @@ void testMappingAnEmptyListCondition() { assertThat(filtered).isSameAs(mapped); } - @Test - void testIsInCaseInsensitiveWhenPresentMap() { - var cond = SqlBuilder.isInCaseInsensitiveWhenPresent("Fred", "Wilma"); - var mapped = cond.map(s -> s + " Flintstone"); - assertThat(mapped.values().toList()).containsExactly("FRED Flintstone", "WILMA Flintstone"); - } - @Test void testIsInCaseInsensitiveWhenPresentMapCaseInsensitive() { var cond = SqlBuilder.isInCaseInsensitiveWhenPresent("Fred", "Wilma"); - var mapped = cond.map(StringUtilities.mapToUpperCase(s -> s + " Flintstone")); - assertThat(mapped.values().toList()).containsExactly("FRED FLINTSTONE", "WILMA FLINTSTONE"); - } - - @Test - void testIsNotInCaseInsensitiveWhenPresentMap() { - var cond = SqlBuilder.isNotInCaseInsensitiveWhenPresent("Fred", "Wilma"); var mapped = cond.map(s -> s + " Flintstone"); - assertThat(mapped.values().toList()).containsExactly("FRED Flintstone", "WILMA Flintstone"); + assertThat(mapped.values().toList()).containsExactly("FRED FLINTSTONE", "WILMA FLINTSTONE"); } @Test void testIsNotInCaseInsensitiveWhenPresentMapCaseInsensitive() { var cond = SqlBuilder.isNotInCaseInsensitiveWhenPresent("Fred", "Wilma"); - var mapped = cond.map(StringUtilities.mapToUpperCase(s -> s + " Flintstone")); + var mapped = cond.map(s -> s + " Flintstone"); assertThat(mapped.values().toList()).containsExactly("FRED FLINTSTONE", "WILMA FLINTSTONE"); } }