package be.arndep.commons.patterns;

import java.util.Optional;

/**
 * Created by Arnaud on 12-08-14.
 * This is an helper class for the Chain of Responsibility pattern
 * http://www.oodesign.com/chain-of-responsibility-pattern.html
 *
 * @param <E>
 */
public abstract class AbstractHandlerChain<E> {
    private Optional<AbstractHandlerChain> successsor;

    /**
     * Default constructor without successor
     */
    public AbstractHandlerChain() {
        successsor = Optional.empty();
    }

    /**
     * Constructor to use with a successor
     * @param successsor
     */
    public AbstractHandlerChain(final AbstractHandlerChain successsor) {
        this.successsor = Optional.of(successsor);
    }

    /**
     * This public method handle the given element
     * @param element
     */
    public final void handle(final E element){
       final  boolean handleByThisNode = this.handleImpl(element);
        successsor.ifPresent(s -> {
            if (!handleByThisNode)
                s.handle(element);
        });
    }

    /**
     * This is the implementation of handle.
     * This method should be overriten for each implementation.
     * @param request
     * @return true to break the chain, false otherwise
     */
    protected abstract boolean handleImpl(final E request);
}

@@ -337,16 +337,19 @@
 lerChain
+%3CE%3E
 %3E succes
@@ -354,16 +354,16 @@
 esssor;%0A
-
 %0A    /**
@@ -502,48 +502,30 @@
  %7D%0A%0A
-    /**%0A     * Constructor to use with a
+%09/**%0A%09 * Configure the
  suc
@@ -531,20 +531,17 @@
 ccessor%0A
-    
+%09
  * @para
@@ -549,32 +549,37 @@
  success
-s
 or%0A
-     */%0A    
+%09 * @return%0A%09 */%0A%09
 public A
@@ -597,16 +597,32 @@
 lerChain
+%3CE%3E setSuccessor
 (final A
@@ -640,24 +640,27 @@
 lerChain
+%3CE%3E
  success
 sor) %7B%0A 
@@ -655,23 +655,16 @@
 cess
-s
 or) %7B%0A
-        
+%09%09
 this
@@ -700,18 +700,46 @@
 cess
-s
 or);%0A
-    
+%09%09return this.successsor.get();%0A%09
 %7D%0A%0A 

use setter instead of initialization constructor