Skip to content

Little optimization that creates less garbage #919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,5 @@ static String toCamelCase(String inputString) {
static String formatConstantForSQL(String in) {
String escaped = in.replace("'", "''"); //$NON-NLS-1$ //$NON-NLS-2$
return "'" + escaped + "'"; //$NON-NLS-1$ //$NON-NLS-2$

}
}
13 changes: 0 additions & 13 deletions src/main/java/org/mybatis/dynamic/sql/util/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,10 @@
*/
package org.mybatis.dynamic.sql.util;

import java.util.Collection;
import java.util.Objects;
import java.util.stream.Stream;

import org.jspecify.annotations.NonNull;
import org.jspecify.annotations.Nullable;

public interface Utilities {
static long safelyUnbox(@Nullable Long l) {
return l == null ? 0 : l;
}

static <T> Stream<@NonNull T> filterNullValues(Stream<@Nullable T> values) {
return values.filter(Objects::nonNull);
}

static <T> Collection<@NonNull T> removeNullElements(Collection<@Nullable T> values) {
return filterNullValues(values.stream()).toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public static IsInCaseInsensitive of(String... values) {
}

public static IsInCaseInsensitive of(Collection<String> values) {
// Keep the null safe upper case utility for backwards compatibility
//noinspection DataFlowIssue
return new IsInCaseInsensitive(values).map(StringUtilities::safelyUpperCase);
// 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;

import org.jspecify.annotations.Nullable;
import org.mybatis.dynamic.sql.AbstractListValueCondition;
import org.mybatis.dynamic.sql.util.StringUtilities;
import org.mybatis.dynamic.sql.util.Utilities;

public class IsInCaseInsensitiveWhenPresent extends AbstractListValueCondition<String>
implements CaseInsensitiveRenderableCondition {
Expand All @@ -35,8 +34,8 @@ public static IsInCaseInsensitiveWhenPresent empty() {
return EMPTY;
}

protected IsInCaseInsensitiveWhenPresent(Collection<@Nullable String> values) {
super(Utilities.removeNullElements(values));
protected IsInCaseInsensitiveWhenPresent(Collection<String> values) {
super(values);
}

@Override
Expand Down Expand Up @@ -66,8 +65,7 @@ public static IsInCaseInsensitiveWhenPresent of(@Nullable String... values) {
}

public static IsInCaseInsensitiveWhenPresent of(Collection<@Nullable String> values) {
// Keep the null safe upper case utility for backwards compatibility
//noinspection DataFlowIssue
return new IsInCaseInsensitiveWhenPresent(values).map(StringUtilities::safelyUpperCase);
return new IsInCaseInsensitiveWhenPresent(
values.stream().filter(Objects::nonNull).map(String::toUpperCase).toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public IsLikeCaseInsensitive map(UnaryOperator<String> mapper) {
}

public static IsLikeCaseInsensitive of(String value) {
// Keep the null safe upper case utility for backwards compatibility
//noinspection DataFlowIssue
return new IsLikeCaseInsensitive(value).map(StringUtilities::safelyUpperCase);
// Keep the null safe upper case utility for backwards compatibility in case someone passes in a null
return new IsLikeCaseInsensitive(StringUtilities.safelyUpperCase(value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public static IsNotInCaseInsensitive of(String... values) {
}

public static IsNotInCaseInsensitive of(Collection<String> values) {
// Keep the null safe upper case utility for backwards compatibility
//noinspection DataFlowIssue
return new IsNotInCaseInsensitive(values).map(StringUtilities::safelyUpperCase);
// 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;

import org.jspecify.annotations.Nullable;
import org.mybatis.dynamic.sql.AbstractListValueCondition;
import org.mybatis.dynamic.sql.util.StringUtilities;
import org.mybatis.dynamic.sql.util.Utilities;

public class IsNotInCaseInsensitiveWhenPresent extends AbstractListValueCondition<String>
implements CaseInsensitiveRenderableCondition {
Expand All @@ -35,8 +34,8 @@ public static IsNotInCaseInsensitiveWhenPresent empty() {
return EMPTY;
}

protected IsNotInCaseInsensitiveWhenPresent(Collection<@Nullable String> values) {
super(Utilities.removeNullElements(values));
protected IsNotInCaseInsensitiveWhenPresent(Collection<String> values) {
super(values);
}

@Override
Expand Down Expand Up @@ -66,8 +65,7 @@ public static IsNotInCaseInsensitiveWhenPresent of(@Nullable String... values) {
}

public static IsNotInCaseInsensitiveWhenPresent of(Collection<@Nullable String> values) {
// Keep the null safe upper case utility for backwards compatibility
//noinspection DataFlowIssue
return new IsNotInCaseInsensitiveWhenPresent(values).map(StringUtilities::safelyUpperCase);
return new IsNotInCaseInsensitiveWhenPresent(
values.stream().filter(Objects::nonNull).map(String::toUpperCase).toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ public IsNotLikeCaseInsensitive map(UnaryOperator<String> mapper) {
}

public static IsNotLikeCaseInsensitive of(String value) {
// Keep the null safe upper case utility for backwards compatibility
//noinspection DataFlowIssue
return new IsNotLikeCaseInsensitive(value).map(StringUtilities::safelyUpperCase);
// Keep the null safe upper case utility for backwards compatibility in case someone passes in a null
return new IsNotLikeCaseInsensitive(StringUtilities.safelyUpperCase(value));
}
}