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 77d137da06 CAUSEWAY-2297: remove rarely used idioms from _With util
77d137da06 is described below
commit 77d137da065be15baafc0e37fbe9fd45cf75ed8d
Author: andi-huber <[email protected]>
AuthorDate: Mon Feb 20 06:55:43 2023 +0100
CAUSEWAY-2297: remove rarely used idioms from _With util
---
.../causeway/commons/internal/base/_With.java | 212 ---------------------
.../commons/internal/context/_Context.java | 16 +-
.../facets/collections/CollectionFacet.java | 42 ++--
.../templresources/TemplateResourceServlet.java | 8 +-
4 files changed, 38 insertions(+), 240 deletions(-)
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
index c0e7ebf36c..6f5d787282 100644
--- 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
@@ -18,22 +18,13 @@
*/
package org.apache.causeway.commons.internal.base;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.HashSet;
import java.util.Objects;
-import java.util.Optional;
-import java.util.TreeMap;
-import java.util.TreeSet;
-import java.util.concurrent.Callable;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import org.springframework.lang.Nullable;
-import lombok.val;
-
/**
* <h1>- internal use only -</h1>
* <p>
@@ -48,47 +39,6 @@ import lombok.val;
*/
public final class _With<T> {
- private _With() { }
-
- // -- OPTION IDIOMS
-
- /**
- * Equivalent to {@code Optional.ofNullable(obj).orElse(orElse);}
- * @param obj (nullable)
- * @param orElse (nullable)
- * @return {@code obj!=null ? obj : orElse}
- */
- public static <X> X ifPresentElse(final @Nullable X obj, final @Nullable X
orElse) {
- return obj!=null ? obj : orElse;
- }
-
- /**
- * Equivalent to {@code Optional.ofNullable(obj).orElseGet(elseGet);}
- * @param obj (nullable)
- * @param elseGet
- * @return {@code obj!=null ? obj : elseGet.get()}
- */
- public static <X> X ifPresentElseGet(final @Nullable X obj, final
Supplier<X> elseGet) {
- return obj!=null ? obj : Objects.requireNonNull(elseGet,
"elseGet").get();
- }
-
- /**
- * Equivalent to {@code Optional.ofNullable(obj).orElseThrow(elseThrow);}
- * @param obj (nullable)
- * @param elseThrow
- * @return {@code obj!=null ? obj : throw( elseThrow.get() )}
- * @throws E
- */
- public static <X, E extends Exception> X ifPresentElseThrow(
- final @Nullable X obj,
- final Supplier<E> elseThrow)
- throws E {
- if(obj!=null) {
- return obj;
- }
- throw Objects.requireNonNull(elseThrow, "elseThrow").get();
- }
-
// -- CONSUMER IDIOMS
/**
@@ -102,57 +52,6 @@ public final class _With<T> {
return obj;
}
- /**
- * Unary identity operator that passes {@code obj} to {@code ifPresent} if
{@code obj} is present.
- * @param obj (nullable)
- * @param ifPresent
- * @return {@code obj}
- */
- public static <X> X acceptIfPresent(final @Nullable X obj, final
Consumer<X> ifPresent) {
- if(obj!=null) {
- Objects.requireNonNull(ifPresent, "ifPresent").accept(obj);
- }
- return obj;
- }
-
- /**
- * Unary identity operator that passes {@code obj} to {@code ifPresent} if
{@code obj} is present,
- * runs the specified {@code elseRun} otherwise.
- * @param obj (nullable)
- * @param ifPresent
- * @param elseRun
- * @return {@code obj}
- */
- public static <X> X acceptIfPresentElseRun(final @Nullable X obj, final
Consumer<X> ifPresent, final Runnable elseRun) {
- if(obj!=null) {
- Objects.requireNonNull(ifPresent, "ifPresent").accept(obj);
- } else {
- Objects.requireNonNull(elseRun, "elseRun").run();
- }
- return obj;
- }
-
- /**
- * Unary identity operator that passes {@code obj} to {@code ifPresent} if
{@code obj} is present,
- * throws the specified Exception provided by {@code elseThrow} otherwise.
- * @param obj (nullable)
- * @param ifPresent
- * @param elseThrow
- * @return {@code obj!=null ? obj : throw( elseThrow.get() ) }
- * @throws E
- */
- public static <X, E extends Exception> X acceptIfPresentElseThrow(
- final @Nullable X obj, final Consumer<X> ifPresent, final
Supplier<E> elseThrow)
- throws E {
-
- if(obj!=null) {
- Objects.requireNonNull(ifPresent, "ifPresent").accept(obj);
- } else {
- throw Objects.requireNonNull(elseThrow, "elseThrow").get();
- }
- return obj;
- }
-
// -- SUPPLIER IDIOMS
/**
@@ -177,36 +76,6 @@ public final class _With<T> {
return obj!=null ? Objects.requireNonNull(mapper, "mapper").apply(obj)
: orElse;
}
- /**
- * Equivalent to {@code
Optional.ofNullable(obj).map(mapper).orElseGet(elseGet);}
- * @param obj (nullable)
- * @param mapper
- * @param elseGet
- * @return {@code obj!=null ? mapper.apply(obj) : elseGet.get()}
- */
- public static <X, R> R mapIfPresentElseGet(final @Nullable X obj, final
Function<X, R> mapper, final Supplier<R> elseGet) {
- return obj!=null ? Objects.requireNonNull(mapper, "mapper").apply(obj)
: Objects.requireNonNull(elseGet, "elseGet").get();
- }
-
- /**
- * Equivalent to {@code
Optional.ofNullable(obj).map(mapper).orElseThrow(elseThrow);}
- * @param obj (nullable)
- * @param mapper
- * @param elseThrow
- * @return {@code obj!=null ? mapper.apply(obj) : throw( elseThrow.get() )}
- * @throws E
- */
- public static <X, R, E extends Exception> R mapIfPresentElseThrow(
- final @Nullable X obj,
- final Function<X, R> mapper,
- final Supplier<E> elseThrow)
- throws E {
- if(obj!=null) {
- return Objects.requireNonNull(mapper, "mapper").apply(obj);
- }
- throw Objects.requireNonNull(elseThrow, "elseThrow").get();
- }
-
// -- PARAMETER NON-EMPTY CHECK(S)
/**
@@ -239,85 +108,4 @@ public final class _With<T> {
return accept(factory.get(), initializer);
}
- /**
- * Allows for single line instantiation and initialization of an ArrayList.
- * @param initializer
- * @return a new ArrayList after calling the {@code initializer} on it
- */
- public static <X> ArrayList<X> arrayList(final Consumer<ArrayList<X>>
initializer) {
- return create(ArrayList::new, initializer);
- }
-
- /**
- * Allows for single line instantiation and initialization of a HashSet.
- * @param initializer
- * @return a new HashSet after calling the {@code initializer} on it
- */
- public static <X> HashSet<X> hashSet(final Consumer<HashSet<X>>
initializer) {
- return create(HashSet::new, initializer);
- }
-
- /**
- * Allows for single line instantiation and initialization of a TreeSet.
- * @param initializer
- * @return a new TreeSet after calling the {@code initializer} on it
- */
- public static <X> TreeSet<X> treeSet(final Consumer<TreeSet<X>>
initializer) {
- return create(TreeSet::new, initializer);
- }
-
- /**
- * Allows for single line instantiation and initialization of a HashMap.
- * @param initializer
- * @return a new HashMap after calling the {@code initializer} on it
- */
- public static <K, V> HashMap<K, V> hashMap(final Consumer<HashMap<K, V>>
initializer) {
- return accept(new HashMap<K, V>(), initializer);
- }
-
- /**
- * Allows for single line instantiation and initialization of a TreeMap.
- * @param initializer
- * @return a new TreeMap after calling the {@code initializer} on it
- */
- public static <K, V> TreeMap<K, V> treeMap(final Consumer<TreeMap<K, V>>
initializer) {
- return accept(new TreeMap<K, V>(), initializer);
- }
-
- /**
- * Allows for single line instantiation and initialization of a
StringBuilder.
- * @param initializer
- * @return a new StringBuilder after calling the {@code initializer} on it
- */
- public static StringBuilder stringBuilder(final Consumer<StringBuilder>
initializer) {
- return create(StringBuilder::new, initializer);
- }
-
- // -- EXCEPTION SWALLOW IDIOMS
-
- /**
- * Returns Optional of the callable's result after invocation. Any
exception during
- * invocation will result in an empty Optional.
- */
- public static <T> Optional<T> tryCall(final Callable<T> callable) {
- try {
- val result = callable.call();
- return Optional.ofNullable(result);
- } catch (Exception e) {
- return Optional.empty();
- }
- }
-
- /**
- * Returns the callable's result after invocation. Any exception during
- * invocation will result in the defaultValue being returned instead.
- */
- public static <T> T tryOrDefault(final Callable<T> callable, final T
defaultValue) {
- try {
- return callable.call();
- } catch (Exception e) {
- return defaultValue;
- }
- }
-
}
diff --git
a/commons/src/main/java/org/apache/causeway/commons/internal/context/_Context.java
b/commons/src/main/java/org/apache/causeway/commons/internal/context/_Context.java
index 3f857d2cb2..fb816e1001 100644
---
a/commons/src/main/java/org/apache/causeway/commons/internal/context/_Context.java
+++
b/commons/src/main/java/org/apache/causeway/commons/internal/context/_Context.java
@@ -21,13 +21,13 @@ package org.apache.causeway.commons.internal.context;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
+import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Supplier;
import org.apache.causeway.commons.internal.base._Casts;
import org.apache.causeway.commons.internal.base._NullSafe;
-import org.apache.causeway.commons.internal.base._With;
import org.apache.causeway.commons.internal.collections._Lists;
import lombok.NonNull;
@@ -170,7 +170,9 @@ public final class _Context {
* @param fallback non-null
*/
public static <T> T getOrElse(final Class<? super T> type, final @NonNull
Supplier<T> fallback) {
- return _With.ifPresentElseGet(getIfAny(type), fallback);
+ return Optional
+ .ofNullable(_Casts.<T>uncheckedCast(getIfAny(type)))
+ .orElseGet(fallback);
}
/**
@@ -183,7 +185,9 @@ public final class _Context {
public static <T, E extends Exception> T getElseThrow (
final @NonNull Class<? super T> type,
final @NonNull Supplier<E> onNotFound) throws E {
- return _With.ifPresentElseThrow(getIfAny(type), onNotFound);
+ return Optional
+ .ofNullable(_Casts.<T>uncheckedCast(getIfAny(type)))
+ .orElseThrow(onNotFound);
}
/**
@@ -192,8 +196,10 @@ public final class _Context {
* @param type non-null
*/
public static <T> T getElseFail(final Class<? super T> type) {
- return _With.ifPresentElseThrow(getIfAny(type), ()->
- new NoSuchElementException(String.format("Could not resolve an
instance of type '%s'", type.getName())));
+ return Optional
+ .ofNullable(_Casts.<T>uncheckedCast(getIfAny(type)))
+ .orElseThrow(()->new NoSuchElementException(
+ String.format("Could not resolve an instance of type
'%s'", type.getName())));
}
diff --git
a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/facets/collections/CollectionFacet.java
b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/facets/collections/CollectionFacet.java
index e756cd0a83..cdb07ca6e7 100644
---
a/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/facets/collections/CollectionFacet.java
+++
b/core/metamodel/src/main/java/org/apache/causeway/core/metamodel/facets/collections/CollectionFacet.java
@@ -40,7 +40,7 @@ import java.util.stream.Stream;
import org.springframework.lang.Nullable;
-import org.apache.causeway.commons.internal.base._With;
+import org.apache.causeway.commons.collections.Can;
import org.apache.causeway.commons.internal.collections._Arrays;
import org.apache.causeway.commons.internal.collections._Lists;
import org.apache.causeway.commons.internal.collections._Sets;
@@ -154,41 +154,45 @@ public interface CollectionFacet extends Facet {
return rawStream.collect(rawCollector);
}
- // array
+ // Array
if (requiredType.isArray()) {
Class<?> elementType = requiredType.getComponentType();
return rawStream.collect(_Arrays.toArray(elementType));
}
+ // Can
+ if (Can.class.equals(requiredType)) {
+ return rawStream.collect(Can.toCan());
+ }
+
// not recognized
return null;
}
-
// -- HELPER
- private static final Map<Class<?>, Supplier<Collection<?>>>
factoriesByType = _With.hashMap(
- map-> {
+ private static final Map<Class<?>, Supplier<Collection<?>>>
factoriesByType = Map.ofEntries(
+
// specific list implementations
- map.put(CopyOnWriteArrayList.class,
_Lists::newConcurrentList);
- map.put(LinkedList.class, _Lists::newLinkedList);
- map.put(ArrayList.class, _Lists::newArrayList);
- map.put(AbstractList.class, _Lists::newArrayList);
+ Map.entry(CopyOnWriteArrayList.class,
_Lists::newConcurrentList),
+ Map.entry(LinkedList.class, _Lists::newLinkedList),
+ Map.entry(ArrayList.class, _Lists::newArrayList),
+ Map.entry(AbstractList.class, _Lists::newArrayList),
// specific set implementations
- map.put(CopyOnWriteArraySet.class,
_Sets::newCopyOnWriteArraySet);
- map.put(LinkedHashSet.class, _Sets::newLinkedHashSet);
- map.put(HashSet.class, _Sets::newHashSet);
- map.put(TreeSet.class, _Sets::newTreeSet);
- map.put(AbstractSet.class, _Sets::newLinkedHashSet);
+ Map.entry(CopyOnWriteArraySet.class,
_Sets::newCopyOnWriteArraySet),
+ Map.entry(LinkedHashSet.class, _Sets::newLinkedHashSet),
+ Map.entry(HashSet.class, _Sets::newHashSet),
+ Map.entry(TreeSet.class, _Sets::newTreeSet),
+ Map.entry(AbstractSet.class, _Sets::newLinkedHashSet),
// interfaces
- map.put(List.class, _Lists::newArrayList);
- map.put(SortedSet.class, _Sets::newTreeSet);
- map.put(Set.class, _Sets::newLinkedHashSet);
- map.put(Collection.class, _Lists::newArrayList);
- });
+ Map.entry(List.class, _Lists::newArrayList),
+ Map.entry(SortedSet.class, _Sets::newTreeSet),
+ Map.entry(Set.class, _Sets::newLinkedHashSet),
+ Map.entry(Collection.class, _Lists::newArrayList)
+ );
}
diff --git
a/core/webapp/src/main/java/org/apache/causeway/core/webapp/modules/templresources/TemplateResourceServlet.java
b/core/webapp/src/main/java/org/apache/causeway/core/webapp/modules/templresources/TemplateResourceServlet.java
index 4be5d8908c..ab465f01c4 100644
---
a/core/webapp/src/main/java/org/apache/causeway/core/webapp/modules/templresources/TemplateResourceServlet.java
+++
b/core/webapp/src/main/java/org/apache/causeway/core/webapp/modules/templresources/TemplateResourceServlet.java
@@ -21,6 +21,7 @@ package
org.apache.causeway.core.webapp.modules.templresources;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
+import java.util.Optional;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
@@ -40,7 +41,6 @@ import
org.apache.causeway.core.metamodel.commons.ResourceUtil;
import org.apache.causeway.core.metamodel.commons.StringExtensions;
import static org.apache.causeway.commons.internal.base._Strings.pair;
-import static org.apache.causeway.commons.internal.base._With.ifPresentElseGet;
import lombok.val;
import lombok.extern.log4j.Log4j2;
@@ -82,9 +82,9 @@ public class TemplateResourceServlet extends HttpServlet {
final String servletPath =
StringExtensions.stripLeadingSlash(request.getServletPath());
log.debug("request: {}", servletPath);
- val resourceInputStream = ifPresentElseGet(
- loadFromFileSystem(request), // try to load from file-system
first
- ()->loadFromClassPath(servletPath)); // otherwise, try to load
from class-path
+ val resourceInputStream = Optional
+ .ofNullable(loadFromFileSystem(request)) // try to load from
file-system first
+ .orElseGet(()->loadFromClassPath(servletPath)); // otherwise,
try to load from class-path
if (resourceInputStream != null) {
try {