You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@causeway.apache.org by ah...@apache.org on 2023/02/20 06:33:45 UTC
[causeway] branch master updated: CAUSEWAY-2297: purge _With util
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/causeway.git
The following commit(s) were added to refs/heads/master by this push:
new 78a77b9427 CAUSEWAY-2297: purge _With util
78a77b9427 is described below
commit 78a77b942737750f15cc9abfbf35a2769fcd3e96
Author: andi-huber <ah...@apache.org>
AuthorDate: Mon Feb 20 07:33:39 2023 +0100
CAUSEWAY-2297: purge _With util
---
.../causeway/commons/internal/base/_Strings.java | 56 ++++++++---
.../causeway/commons/internal/base/_With.java | 111 ---------------------
.../causeway/commons/internal/debug/_Probe.java | 3 +-
.../causeway/core/metamodel/commons/ClassUtil.java | 4 +-
.../causeway/core/metamodel/consent/Veto.java | 5 +-
5 files changed, 48 insertions(+), 131 deletions(-)
diff --git a/commons/src/main/java/org/apache/causeway/commons/internal/base/_Strings.java b/commons/src/main/java/org/apache/causeway/commons/internal/base/_Strings.java
index 12b7980f97..6486277acb 100644
--- a/commons/src/main/java/org/apache/causeway/commons/internal/base/_Strings.java
+++ b/commons/src/main/java/org/apache/causeway/commons/internal/base/_Strings.java
@@ -47,15 +47,15 @@ import org.springframework.lang.Nullable;
import org.apache.causeway.commons.internal._Constants;
import org.apache.causeway.commons.internal.base._Bytes.BytesOperator;
import org.apache.causeway.commons.internal.functions._Predicates;
+
import static org.apache.causeway.commons.internal.base._NullSafe.size;
import static org.apache.causeway.commons.internal.base._Strings_SplitIterator.splitIterator;
-import static org.apache.causeway.commons.internal.base._With.mapIfPresentElse;
-import static org.apache.causeway.commons.internal.base._With.requiresNotEmpty;
import static org.apache.causeway.commons.internal.functions._Predicates.not;
import lombok.NonNull;
import lombok.SneakyThrows;
import lombok.val;
+import lombok.experimental.UtilityClass;
/**
* <h1>- internal use only -</h1>
@@ -72,10 +72,9 @@ import lombok.val;
*
* @since 2.0
*/
+@UtilityClass
public final class _Strings {
- private _Strings() {}
-
// -- CONSTANTS
/**
@@ -251,7 +250,7 @@ public final class _Strings {
* @return null if the {@code input} is null
*/
public static String trim(final @Nullable String input) {
- return mapIfPresentElse(input, String::trim, null);
+ return mapIfPresentElseNull(input, String::trim);
}
/**
@@ -260,7 +259,7 @@ public final class _Strings {
* @return null if {@code input} is null
*/
public static String lower(final @Nullable String input) {
- return mapIfPresentElse(input, String::toLowerCase, null);
+ return mapIfPresentElseNull(input, String::toLowerCase);
}
/**
@@ -269,7 +268,7 @@ public final class _Strings {
* @return null if {@code input} is null
*/
public static String upper(final @Nullable String input) {
- return mapIfPresentElse(input, String::toUpperCase, null);
+ return mapIfPresentElseNull(input, String::toUpperCase);
}
/**
@@ -310,6 +309,23 @@ public final class _Strings {
// -- SPECIAL UNARY OPERATORS
+ /**
+ * String not-empty (nor null) guard.
+ * @param str target for the non-empty-check
+ * @param identifier to use for the exception message, when the non-empty-check fails
+ * @throws NullPointerException if {@code str} is {@code null}
+ * @throws IllegalArgumentException if {@code str} is 'empty'
+ */
+ public static String requireNonEmpty(final @Nullable String str, final String identifier) {
+ if (str == null) {
+ throw new NullPointerException(String.format("'%s' is required to be present (not null).", identifier));
+ }
+ if (str.length()==0) {
+ throw new IllegalArgumentException(String.format("'%s' is required to be present and not empty.", identifier));
+ }
+ return str;
+ }
+
public static @Nullable String htmlEscape(final @Nullable String source) {
return _Strings_HtmlEscaper.htmlEscape(source);
}
@@ -383,7 +399,7 @@ public final class _Strings {
public static String combineWithDelimiter(
final @Nullable String left, final @Nullable String right, final String delimiter) {
- requiresNotEmpty(delimiter, "pathDelimiter");
+ requireNonEmpty(delimiter, "pathDelimiter");
if (isNullOrEmpty(left) && isNullOrEmpty(right)) {
return "";
@@ -595,7 +611,7 @@ public final class _Strings {
* @return null if {@code input} is null
*/
public static String condenseWhitespaces(final @Nullable String input, final @NonNull String replacement) {
- return mapIfPresentElse(input, __->input.replaceAll("\\s+", replacement), null);
+ return mapIfPresentElseNull(input, __->input.replaceAll("\\s+", replacement));
}
/**
@@ -705,7 +721,9 @@ public final class _Strings {
* @return null if {@code str} is null
*/
public static final byte[] toBytes(final @Nullable String str, final @NonNull Charset charset) {
- return mapIfPresentElse(str, __->str.getBytes(charset), null);
+ return Optional.ofNullable(str)
+ .map(s->s.getBytes(charset))
+ .orElse(null);
}
/**
@@ -715,7 +733,9 @@ public final class _Strings {
* @return null if {@code bytes} is null
*/
public static final String ofBytes(final @Nullable byte[] bytes, final @NonNull Charset charset) {
- return mapIfPresentElse(bytes, __->new String(bytes, charset), null);
+ return Optional.ofNullable(bytes)
+ .map(b->new String(b, charset))
+ .orElse(null);
}
/**
@@ -728,7 +748,7 @@ public final class _Strings {
* @return null if {@code input} is null
*/
public static final String convert(final @Nullable String input, final @NonNull BytesOperator converter, final @NonNull Charset charset) {
- return mapIfPresentElse(input, __->ofBytes(converter.apply(toBytes(input, charset)), charset), null);
+ return mapIfPresentElseNull(input, __->ofBytes(converter.apply(toBytes(input, charset)), charset));
}
// -- UNARY OPERATOR COMPOSITION
@@ -880,4 +900,16 @@ public final class _Strings {
: str;
}
+ // -- HELPER
+
+ /**
+ * Equivalent to {@code Optional.ofNullable(obj).map(mapper).orElse(null);}
+ */
+ private static String mapIfPresentElseNull(
+ final @Nullable String obj, final @NonNull UnaryOperator<String> mapper) {
+ return obj!=null
+ ? mapper.apply(obj)
+ : null;
+ }
+
}
diff --git a/commons/src/main/java/org/apache/causeway/commons/internal/base/_With.java b/commons/src/main/java/org/apache/causeway/commons/internal/base/_With.java
deleted file mode 100644
index 6f5d787282..0000000000
--- a/commons/src/main/java/org/apache/causeway/commons/internal/base/_With.java
+++ /dev/null
@@ -1,111 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you 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.
- */
-package org.apache.causeway.commons.internal.base;
-
-import java.util.Objects;
-import java.util.function.Consumer;
-import java.util.function.Function;
-import java.util.function.Supplier;
-
-import org.springframework.lang.Nullable;
-
-/**
- * <h1>- internal use only -</h1>
- * <p>
- * Provides shortcuts for common 'Optional' idioms.
- * </p>
- * <p>
- * <b>WARNING</b>: Do <b>NOT</b> use any of the classes provided by this package! <br/>
- * These may be changed or removed without notice!
- * </p>
- *
- * @since 2.0
- */
-public final class _With<T> {
-
- // -- CONSUMER IDIOMS
-
- /**
- * Unary identity operator that passes {@code obj} to {@code consumer}.
- * @param obj (nullable)
- * @param consumer
- * @return {@code obj}
- */
- public static <X> X accept(final @Nullable X obj, final Consumer<X> consumer) {
- Objects.requireNonNull(consumer, "consumer").accept(obj);
- return obj;
- }
-
- // -- SUPPLIER IDIOMS
-
- /**
- * @param obj (nullable)
- * @param supplier
- * @return {@code obj!=null ? obj : supplier.get()}
- */
- public static <X> X computeIfAbsent(final @Nullable X obj, final Supplier<X> supplier) {
- return obj!=null ? obj : Objects.requireNonNull(supplier, "supplier").get();
- }
-
- // -- MAPPING IDIOMS
-
- /**
- * Equivalent to {@code Optional.ofNullable(obj).map(mapper).orElse(orElse);}
- * @param obj (nullable)
- * @param mapper
- * @param orElse (nullable)
- * @return {@code obj!=null ? mapper.apply(obj) : orElse}
- */
- public static <X, R> R mapIfPresentElse(final @Nullable X obj, final Function<X, R> mapper, final @Nullable R orElse) {
- return obj!=null ? Objects.requireNonNull(mapper, "mapper").apply(obj) : orElse;
- }
-
- // -- PARAMETER NON-EMPTY CHECK(S)
-
- /**
- * Allows for convenient named parameter non-empty-check.
- * @param obj target for the non-empty-check
- * @param paramName to use for the exception message, when the non-empty-check fails
- * @return {@code obj}
- * @throws NullPointerException if {@code obj} is {@code null}
- * @throws IllegalArgumentException if {@code obj} is 'empty'
- */
- public static String requiresNotEmpty(final @Nullable String obj, final String paramName) {
- if (obj == null) {
- throw new NullPointerException(String.format("Parameter/Field '%s' is required to be present (not null).", paramName));
- }
- if (obj.length()==0) {
- throw new IllegalArgumentException(String.format("Parameter/Field '%s' is required to be present and not empty.", paramName));
- }
- return obj;
- }
-
- // -- CONVENIENT CONSTRUCTORS
-
- /**
- * Allows for single line instantiation and initialization of an Object.
- * @param factory
- * @param initializer
- * @return a new Object as provided by {@code factory} after calling the {@code initializer} on it
- */
- public static <T> T create(final Supplier<T> factory, final Consumer<T> initializer) {
- return accept(factory.get(), initializer);
- }
-
-}
diff --git a/commons/src/main/java/org/apache/causeway/commons/internal/debug/_Probe.java b/commons/src/main/java/org/apache/causeway/commons/internal/debug/_Probe.java
index 5936362aa3..c01552d0f8 100644
--- a/commons/src/main/java/org/apache/causeway/commons/internal/debug/_Probe.java
+++ b/commons/src/main/java/org/apache/causeway/commons/internal/debug/_Probe.java
@@ -24,7 +24,6 @@ import java.util.concurrent.atomic.LongAdder;
import java.util.stream.Collectors;
import org.apache.causeway.commons.internal.base._Strings;
-import org.apache.causeway.commons.internal.base._With;
import org.apache.causeway.commons.internal.exceptions._Exceptions;
import lombok.val;
@@ -232,7 +231,7 @@ public class _Probe {
}
});
return _Strings.splitThenStream(name[0], ".")
- .map(part->_With.mapIfPresentElse(abbreviations.get(part), value->value, part))
+ .map(part->_Strings.nonEmpty(abbreviations.get(part)).orElse(part))
.collect(Collectors.joining("."));
}
diff --git a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/commons/ClassUtil.java b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/commons/ClassUtil.java
index a16f460a84..42d4204427 100644
--- a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/commons/ClassUtil.java
+++ b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/commons/ClassUtil.java
@@ -24,8 +24,6 @@ import org.apache.causeway.commons.internal.base._Strings;
import org.apache.causeway.commons.internal.context._Context;
import org.apache.causeway.commons.internal.exceptions._Exceptions;
-import static org.apache.causeway.commons.internal.base._With.requiresNotEmpty;
-
import lombok.NonNull;
import lombok.val;
import lombok.experimental.UtilityClass;
@@ -131,7 +129,7 @@ public final class ClassUtil {
}
public Class<?> forNameElseFail(final String fullName) {
- requiresNotEmpty(fullName, "fullName");
+ _Strings.requireNonEmpty(fullName, "Full Name");
final Class<?> builtIn = ClassUtil.getBuiltIn(fullName);
if (builtIn != null) {
return builtIn;
diff --git a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/consent/Veto.java b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/consent/Veto.java
index 4c4dfd89f5..6bbdc81dbb 100644
--- a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/consent/Veto.java
+++ b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/consent/Veto.java
@@ -18,10 +18,9 @@
*/
package org.apache.causeway.core.metamodel.consent;
+import org.apache.causeway.commons.internal.base._Strings;
import org.apache.causeway.core.metamodel.facetapi.Facet;
-import static org.apache.causeway.commons.internal.base._With.requiresNotEmpty;
-
public class Veto extends ConsentAbstract {
private static final long serialVersionUID = 1L;
@@ -37,7 +36,7 @@ public class Veto extends ConsentAbstract {
* - must not be empty or <tt>null</tt>
*/
public Veto(final String reasonVetoed) {
- super(null, requiresNotEmpty(reasonVetoed, "reasonVetoed"));
+ super(null, _Strings.requireNonEmpty(reasonVetoed, "Reason Vetoed"));
}
public Veto(final InteractionResult interactionResult) {