You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2019/09/23 11:37:40 UTC
[isis] branch v2 updated: ISIS-2158: internal API extensions and
fixes
This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch v2
in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/v2 by this push:
new 102efad ISIS-2158: internal API extensions and fixes
102efad is described below
commit 102efadd24a4bfb230cca6979d1d53e132c25ff3
Author: Andi Huber <ah...@apache.org>
AuthorDate: Mon Sep 23 13:37:16 2019 +0200
ISIS-2158: internal API extensions and fixes
---
.../isis/commons/internal/collections/_Sets.java | 55 ++++++++++-
.../commons/internal/collections/_Streams.java | 105 +++++++++++++++++++++
.../commons/internal/functions/_Predicates.java | 12 ++-
3 files changed, 166 insertions(+), 6 deletions(-)
diff --git a/core/commons/src/main/java/org/apache/isis/commons/internal/collections/_Sets.java b/core/commons/src/main/java/org/apache/isis/commons/internal/collections/_Sets.java
index 23b0c67..488d349 100644
--- a/core/commons/src/main/java/org/apache/isis/commons/internal/collections/_Sets.java
+++ b/core/commons/src/main/java/org/apache/isis/commons/internal/collections/_Sets.java
@@ -24,6 +24,7 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.LinkedHashSet;
+import java.util.Objects;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
@@ -38,6 +39,7 @@ import javax.annotation.Nullable;
import org.apache.isis.commons.internal.base._NullSafe;
import static org.apache.isis.commons.internal.base._With.requires;
+import static org.apache.isis.commons.internal.functions._Predicates.not;
/**
* <h1>- internal use only -</h1>
@@ -209,6 +211,7 @@ public final class _Sets {
/**
* Returns the intersection (set theory) of two given sets, not retaining any order.
+ * Any {@code null} elements are ignored and will not be contained in the resulting set.
* @param a
* @param b
* @return non null, unmodifiable
@@ -222,12 +225,15 @@ public final class _Sets {
}
return Collections.unmodifiableSet(
a.stream()
+ .filter(Objects::nonNull)
.filter(b::contains)
.collect(Collectors.toSet()) );
}
/**
- * Returns the intersection (set theory) of two given sets, retaining order.
+ * Returns the intersection (set theory) of two given (sorted) sets,
+ * retaining order only when natural order.
+ * Any {@code null} elements are ignored and will not be contained in the resulting set.
* @param a
* @param b
* @return non null, unmodifiable
@@ -241,9 +247,56 @@ public final class _Sets {
}
return Collections.unmodifiableSortedSet(
a.stream()
+ .filter(Objects::nonNull)
.filter(b::contains)
.collect(Collectors.toCollection(TreeSet::new)));
}
+ /**
+ * Returns a new set containing all the elements of {@code a} that are not in {@code b},
+ * not retaining any order.
+ * Any {@code null} elements are ignored and will not be contained in the resulting set.
+ * @param <T>
+ * @param a
+ * @param b
+ * @return {@code a - b}, non null, unmodifiable
+ */
+ public static <T> Set<T> minus(@Nullable Set<T> a, @Nullable Set<T> b) {
+ if(a==null || a.isEmpty()) {
+ return Collections.emptySet();
+ }
+ if(b==null || b.isEmpty()) {
+ return Collections.unmodifiableSet(new HashSet<>(a));
+ }
+ return Collections.unmodifiableSet(
+ a.stream()
+ .filter(Objects::nonNull)
+ .filter(not(b::contains))
+ .collect(Collectors.toSet()) );
+ }
+
+ /**
+ * Returns a new (sorted) set containing all the elements of {@code a} that are not in {@code b},
+ * retaining order only when natural order.
+ * Any {@code null} elements are ignored and will not be contained in the resulting set.
+ * @param <T>
+ * @param a
+ * @param b
+ * @return {@code a - b}, non null, unmodifiable
+ */
+ public static <T> SortedSet<T> minusSorted(@Nullable SortedSet<T> a, @Nullable SortedSet<T> b) {
+ if(a==null || a.isEmpty()) {
+ return Collections.emptySortedSet();
+ }
+ if(b==null || b.isEmpty()) {
+ return Collections.unmodifiableSortedSet(new TreeSet<>(a));
+ }
+ return Collections.unmodifiableSortedSet(
+ a.stream()
+ .filter(Objects::nonNull)
+ .filter(not(b::contains))
+ .collect(Collectors.toCollection(TreeSet::new)));
+ }
+
}
diff --git a/core/commons/src/main/java/org/apache/isis/commons/internal/collections/_Streams.java b/core/commons/src/main/java/org/apache/isis/commons/internal/collections/_Streams.java
new file mode 100644
index 0000000..1bd8317
--- /dev/null
+++ b/core/commons/src/main/java/org/apache/isis/commons/internal/collections/_Streams.java
@@ -0,0 +1,105 @@
+/*
+ * 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.isis.commons.internal.collections;
+
+import java.util.stream.Stream;
+
+/**
+ * <h1>- internal use only -</h1>
+ * <p>
+ * Stream extensions.
+ * </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 _Streams {
+
+ private _Streams(){}
+
+ // -- CONCATENATION
+
+ /**
+ * 3 param variant of {@link Stream#concat(Stream, Stream)}
+ */
+ public static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b, Stream<? extends T> c) {
+ return Stream.concat(Stream.concat(a, b), c);
+ }
+
+ /**
+ * 4 param variant of {@link Stream#concat(Stream, Stream)}
+ */
+ public static <T> Stream<T> concat(
+ Stream<? extends T> a, Stream<? extends T> b, Stream<? extends T> c, Stream<? extends T> d) {
+
+ return Stream.concat(Stream.concat(a, b), Stream.concat(c, d));
+ }
+
+ /**
+ * 5 param variant of {@link Stream#concat(Stream, Stream)}
+ */
+ public static <T> Stream<T> concat(
+ Stream<? extends T> a, Stream<? extends T> b, Stream<? extends T> c, Stream<? extends T> d,
+ Stream<? extends T> e) {
+
+ return Stream.concat(
+ Stream.concat(Stream.concat(a, b), Stream.concat(c, d)),
+ e);
+ }
+
+ /**
+ * 6 param variant of {@link Stream#concat(Stream, Stream)}
+ */
+ public static <T> Stream<T> concat(
+ Stream<? extends T> a, Stream<? extends T> b, Stream<? extends T> c, Stream<? extends T> d,
+ Stream<? extends T> e, Stream<? extends T> f) {
+
+ return Stream.concat(
+ Stream.concat(Stream.concat(a, b), Stream.concat(c, d)),
+ Stream.concat(e, f));
+ }
+
+ /**
+ * 7 param variant of {@link Stream#concat(Stream, Stream)}
+ */
+ public static <T> Stream<T> concat(
+ Stream<? extends T> a, Stream<? extends T> b, Stream<? extends T> c, Stream<? extends T> d,
+ Stream<? extends T> e, Stream<? extends T> f, Stream<? extends T> g) {
+
+ return Stream.concat(
+ Stream.concat(Stream.concat(a, b), Stream.concat(c, d)),
+ Stream.concat(Stream.concat(e, f), g));
+ }
+
+ /**
+ * 8 param variant of {@link Stream#concat(Stream, Stream)}
+ */
+ public static <T> Stream<T> concat(
+ Stream<? extends T> a, Stream<? extends T> b, Stream<? extends T> c, Stream<? extends T> d,
+ Stream<? extends T> e, Stream<? extends T> f, Stream<? extends T> g, Stream<? extends T> h) {
+
+ return Stream.concat(
+ Stream.concat(Stream.concat(a, b), Stream.concat(c, d)),
+ Stream.concat(Stream.concat(e, f), Stream.concat(g, h)));
+ }
+}
diff --git a/core/commons/src/main/java/org/apache/isis/commons/internal/functions/_Predicates.java b/core/commons/src/main/java/org/apache/isis/commons/internal/functions/_Predicates.java
index e1ede0a..9a5a155 100644
--- a/core/commons/src/main/java/org/apache/isis/commons/internal/functions/_Predicates.java
+++ b/core/commons/src/main/java/org/apache/isis/commons/internal/functions/_Predicates.java
@@ -73,15 +73,17 @@ public final class _Predicates {
* @param predicate
*/
public static <T> Predicate<T> not(Predicate<T> predicate) {
- return null;
+ return predicate.negate();
}
/**
- * @param superClass
- * @return a Predicate that tests for the operand to be an instance of {@code superClass}
+ * @param cls
+ * @return a Predicate that tests for the operand to be an instance of {@code cls}
+ * @deprecated use Class::isInstance instead
*/
- public static Predicate<Object> instanceOf(Class<?> superClass) {
- return obj->superClass.isAssignableFrom(obj.getClass());
+ @Deprecated
+ public static Predicate<Object> instanceOf(Class<?> cls) {
+ return cls::isInstance;
}
}