/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed 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 net.jodah.failsafe;

import net.jodah.failsafe.internal.util.Assert;

import java.util.Arrays;
import java.util.List;

/**
 * Simple, sophisticated failure handling.
 *
 * @author Jonathan Halterman
 */
public class Failsafe {
  /**
   * Creates and returns a new {@link FailsafeExecutor} instance that will handle failures according to the given {@code
   * policies}. The {@code policies} are composed around an execution and will handle execution results in reverse, with
   * the last policy being applied first. For example, consider:
   * <p>
   * <pre>
   *   Failsafe.with(fallback, retryPolicy, circuitBreaker).get(supplier);
   * </pre>
   * </p>
   * This results in the following internal composition when executing the {@code supplier} and handling its result:
   * <p>
   * <pre>
   *   Fallback(RetryPolicy(CircuitBreaker(Supplier)))
   * </pre>
   * </p>
   * This means the {@code CircuitBreaker} is first to evaluate the {@code Supplier}'s result, then the {@code
   * RetryPolicy}, then the {@code Fallback}. Each policy makes its own determination as to whether the result
   * represents a failure. This allows different policies to be used for handling different types of failures.
   *
   * @param <R> result type
   * @param <P> policy type
   * @throws NullPointerException if {@code policies} is null
   * @throws IllegalArgumentException if {@code policies} is empty
   */
  @SafeVarargs
  public static <R, P extends Policy<R>> FailsafeExecutor<R> with(P... policies) {
    Assert.notNull(policies, "policies");
    return new FailsafeExecutor<>(Arrays.asList(policies));
  }

  /**
   * Creates and returns a new {@link FailsafeExecutor} instance that will handle failures according to the given {@code
   * policies}. The {@code policies} are composed around an execution and will handle execution results in reverse, with
   * the last policy being applied first. For example, consider:
   * <p>
   * <pre>
   *   Failsafe.with(fallback, retryPolicy, circuitBreaker).get(supplier);
   * </pre>
   * </p>
   * This results in the following internal composition when executing the {@code supplier} and handling its result:
   * <p>
   * <pre>
   *   Fallback(RetryPolicy(CircuitBreaker(Supplier)))
   * </pre>
   * </p>
   * This means the {@code CircuitBreaker} is first to evaluate the {@code Supplier}'s result, then the {@code
   * RetryPolicy}, then the {@code Fallback}. Each policy makes its own determination as to whether the result
   * represents a failure. This allows different policies to be used for handling different types of failures.
   *
   * @param <R> result type
   * @throws NullPointerException if {@code policies} is null
   * @throws IllegalArgumentException if {@code policies} is empty
   */
  @SuppressWarnings("unchecked")
  public static <R> FailsafeExecutor<R> with(List<? extends Policy<R>> policies) {
    return new FailsafeExecutor<>((List<Policy<R>>) Assert.notNull(policies, "policies"));
  }
}

@@ -703,16 +703,49 @@
 il.Array
+List;%0Aimport java.util.Collection
 s;%0Aimpor
@@ -1957,92 +1957,32 @@
 ode 
-policies%7D is null%0A   * @throws IllegalArgumentException if %7B@code p
+outermostP
 olic
-ies
+y
 %7D is 
-empty
+null
 %0A   
@@ -2066,16 +2066,35 @@
 %3E with(P
+ outermostPolicy, P
 ... poli
@@ -2120,35 +2120,204 @@
 notNull(
-policies
+outermostPolicy, %22outermostPolicy%22);%0A    List%3CPolicy%3CR%3E%3E policyList = new ArrayList%3C%3E(policies.length + 1);%0A    policyList.add(outermostPolicy);%0A    Collections.addAll(policyList
 , 
-%22
 policies
 %22);%0A    
@@ -2308,17 +2308,16 @@
 policies
-%22
 );%0A    r
@@ -2349,31 +2349,18 @@
 r%3C%3E(
-Arrays.asList(policies)
+policyList
 );%0A 

Update Failsafe.with to require at least one policy (for now).