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/12/18 14:31:24 UTC

[isis] 01/04: ISIS-2226: getOrThrow shortcut for Can

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/isis.git

commit 51674e952abc39cc2bbcc3c617ac763eb638bb4e
Author: Andi Huber <ah...@apache.org>
AuthorDate: Wed Dec 18 12:14:35 2019 +0100

    ISIS-2226: getOrThrow shortcut for Can<T>
---
 .../java/org/apache/isis/commons/collections/Can.java | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/core/commons/src/main/java/org/apache/isis/commons/collections/Can.java b/core/commons/src/main/java/org/apache/isis/commons/collections/Can.java
index 5d916e6..ee7c308 100644
--- a/core/commons/src/main/java/org/apache/isis/commons/collections/Can.java
+++ b/core/commons/src/main/java/org/apache/isis/commons/collections/Can.java
@@ -21,6 +21,7 @@ package org.apache.isis.commons.collections;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
+import java.util.NoSuchElementException;
 import java.util.Optional;
 import java.util.function.Consumer;
 import java.util.function.Function;
@@ -72,6 +73,19 @@ public interface Can<T> extends Iterable<T> {
      * @return optionally this Can's element with index {@code elementIndex} 
      */
     Optional<T> get(int elementIndex);
+    
+    /**
+     * Shortcut to {@code get(elementIndex).orElseThrow(...)}
+     * @param elementIndex
+     * @return this Can's element with index {@code elementIndex}
+     * @throws NoSuchElementException
+     * @see {@link #get(int)} 
+     */
+    default T getOrThrow(final int elementIndex) {
+        return get(elementIndex)
+                .orElseThrow(()->new NoSuchElementException(
+                        "no element with elementIndex = " + elementIndex));
+    }
 
     /**
      * @return Stream of elements this Can contains
@@ -443,9 +457,4 @@ public interface Can<T> extends Iterable<T> {
                 Can::ofCollection);
     }
 
-
-
-   
-
-
 }