/*
 * The MIT License
 * 
 * Copyright (c) 2014-2014 Efthymios Sarmpanis http://esarbanis.github.io/roolr
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction,
 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or
 * substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.github.esarbanis.roolr;

import java.util.Collection;


/**
 * @author <a href="mailto:e.sarbanis@gmail.com">Efthymios Sarmpanis</a>
 */
public class Roolr<T> {

  private final Collection<Rule<T>> rules;
  private final T defaultOutcome;

  public Roolr(Collection<Rule<T>> rules, T defaultOutcome) {
    this.rules = rules;
    this.defaultOutcome = defaultOutcome;
  }

  /**
   * Evaluates the {@link Rule} object list and resolves a predefined
   * type.
   * <p>
   * If the evaluation fails an {@link EvaluationException} will be
   * thrown.
   * </p>
   *
   * @param context the {@link EvaluationContext context} of the
   *        evaluation.
   * @return the resolved type of the rule list evaluation.
   * @throws EvaluationException Thrown when the evaluation has failed, wrapping the original exception.
   */
  public T decide(EvaluationContext context) throws EvaluationException {
    T outcome = null;
    for (Rule<T> rule : rules) {
      try {
        if ((outcome = rule.apply(context)) != null) {
          break;
        }
      } catch (Exception e) {
        throw new EvaluationException("Cannot make a decision. There was an error.", e);
      }
    }
    if (outcome == null) {
      outcome = defaultOutcome;
    }
    return outcome;
  }

}

@@ -2092,114 +2092,155 @@
     
-T outcome = null;%0A    for (Rule%3CT%3E rule :
+try %7B%0A        return
  rules
-) %7B%0A
+%0A  
       
-try %7B%0A        if ((outcome = rule.apply(context))
+        .stream()%0A                .map((Rule%3CT%3E rule) -%3E rule.apply(context))%0A                .filter((T t) -%3E t
  != 
@@ -2240,27 +2240,31 @@
 %3E t != null)
- %7B%0A
+%0A      
           br
@@ -2265,27 +2265,62 @@
     
-break;%0A        %7D%0A  
+.findFirst()%0A                .orElse(defaultOutcome);%0A
     
@@ -2440,106 +2440,12 @@
     
-  
 %7D%0A  
- 
- %7D%0A    if (outcome == null) %7B%0A      outcome = defaultOutcome;%0A    %7D%0A    return outcome;%0A  %7D%0A
+%7D
 %0A%7D%0A

Migrate decision implementation to java8.