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/11/14 15:39:21 UTC

[isis] branch master updated: ISIS-2158: introduces ChainOfResponsibility with 'commons'

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


The following commit(s) were added to refs/heads/master by this push:
     new 97e9584  ISIS-2158: introduces ChainOfResponsibility with 'commons'
97e9584 is described below

commit 97e9584806a78d4580f8a352a193369ac80ba172
Author: Andi Huber <ah...@apache.org>
AuthorDate: Thu Nov 14 16:39:13 2019 +0100

    ISIS-2158: introduces ChainOfResponsibility with 'commons'
    
    allows for use of the Chain of Responsibility design pattern
---
 .../commons/handler/ChainOfResponsibility.java     | 84 ++++++++++++++++++++++
 .../handlers/ChainOfResponsibilityTest.java        | 70 ++++++++++++++++++
 2 files changed, 154 insertions(+)

diff --git a/core/commons/src/main/java/org/apache/isis/commons/handler/ChainOfResponsibility.java b/core/commons/src/main/java/org/apache/isis/commons/handler/ChainOfResponsibility.java
new file mode 100644
index 0000000..bc89103
--- /dev/null
+++ b/core/commons/src/main/java/org/apache/isis/commons/handler/ChainOfResponsibility.java
@@ -0,0 +1,84 @@
+/*
+ *  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.handler;
+
+import java.util.List;
+import java.util.Optional;
+
+import lombok.val;
+
+/**
+ * Building blocks for the <em>Chain of Responsibility</em> design pattern.
+ * <p>
+ * <em>Chain of Responsibility</em> allows passing requests along the chain of handlers, 
+ * until one of them handles the request.
+ *  
+ * @since 2.0
+ *
+ * @param <X> request type
+ * @param <R> response type
+ */
+public interface ChainOfResponsibility<X, R> {
+
+    /**
+     * The {@code request} is passed along the chain of handlers, until one of them handles the request.
+     * @param request
+     * @return response of the first handler that handled the request wrapped in an Optional, 
+     * or an empty Optional, if no handler handled the request  
+     */
+    Optional<R> handle(X request);
+
+    /**
+     * A chain of responsibility is made up of handlers, that are asked in sequence, 
+     * whether they handle a request. 
+     * 
+     * @since 2.0
+     *
+     * @param <X> request type
+     * @param <R> response type
+     */
+    static interface Handler<X, R> {
+        boolean isHandling(X request);
+        R handle(X request);
+    }
+
+    /**
+     * Creates a new ChainOfResponsibility of given {@code chainOfHandlers} 
+     * @param <X>
+     * @param <R>
+     * @param chainOfHandlers
+     * @return
+     */
+    static <X, R> ChainOfResponsibility<X, R> 
+    of(
+            final List<ChainOfResponsibility.Handler<? super X, R>> chainOfHandlers) {
+    
+        return request -> {
+
+            val response = chainOfHandlers.stream()
+                    .filter(h->h.isHandling(request))
+                    .findFirst()
+                    .map(h->h.handle(request));
+
+            return response;
+        };
+        
+    }
+    
+}
diff --git a/core/commons/src/test/java/org/apache/isis/commons/handlers/ChainOfResponsibilityTest.java b/core/commons/src/test/java/org/apache/isis/commons/handlers/ChainOfResponsibilityTest.java
new file mode 100644
index 0000000..e8a687e
--- /dev/null
+++ b/core/commons/src/test/java/org/apache/isis/commons/handlers/ChainOfResponsibilityTest.java
@@ -0,0 +1,70 @@
+package org.apache.isis.commons.handlers;
+
+import java.util.Arrays;
+
+import org.junit.jupiter.api.Test;
+
+import org.apache.isis.commons.handler.ChainOfResponsibility;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import lombok.val;
+
+class ChainOfResponsibilityTest {
+
+    @Test
+    void threeHandlers_shouldProperlyTakeResponsibilityInOrder() {
+        
+        val aToUpperCase = new ChainOfResponsibility.Handler<String, String>() {
+
+            @Override
+            public boolean isHandling(String request) {
+                return request.startsWith("a");
+            }
+
+            @Override
+            public String handle(String request) {
+                return request.toUpperCase();
+            }
+            
+        };
+
+        val bToUpperCase = new ChainOfResponsibility.Handler<String, String>() {
+
+            @Override
+            public boolean isHandling(String request) {
+                return request.startsWith("b");
+            }
+
+            @Override
+            public String handle(String request) {
+                return request.toUpperCase();
+            }
+            
+        };
+
+        val finallyNoop = new ChainOfResponsibility.Handler<String, String>() {
+
+            @Override
+            public boolean isHandling(String request) {
+                return true;
+            }
+
+            @Override
+            public String handle(String request) {
+                return request;
+            }
+            
+        };
+        
+        val chainOfResponsibility = ChainOfResponsibility.of(
+                Arrays.asList(aToUpperCase, bToUpperCase, finallyNoop)); 
+        
+        
+        assertEquals("ASTRING", chainOfResponsibility.handle("aString").orElse(null)); // handled by first handler
+        assertEquals("BSTRING", chainOfResponsibility.handle("bString").orElse(null)); // handled by second handler
+        assertEquals("cString", chainOfResponsibility.handle("cString").orElse(null)); // handled by third handler
+        
+    }
+
+}