View Javadoc
1   package net.secodo.jcircuitbreaker.breakstrategy;
2   
3   import net.secodo.jcircuitbreaker.breakstrategy.impl.dsl.DslAndStrategy;
4   import net.secodo.jcircuitbreaker.breakstrategy.impl.dsl.DslOrStrategy;
5   
6   
7   /**
8    * Contains methods (which may be accessed statically) for chaining multiple breaker strategies into single
9    * break strategy using "and","or" expressions.
10   */
11  public class SimpleStrategyDsl {
12    protected SimpleStrategyDsl() {
13    }
14  
15    /**
16     * Returns {@link BreakStrategy} which returns true if at least one of the given strategies returns true.
17     *
18     * @param breakStrategies strategies to be chained with "or" statement
19     * @param <R> the return type of target-method for which the strategy will be executed
20     *
21     * @return new strategy built out of given strategies
22     */
23    @SuppressWarnings({ "unchecked", "rawtypes" })
24    public static <R> BreakStrategy<R> anyOf(BreakStrategy... breakStrategies) {
25      BreakStrategy<R> resultingStrategy = breakStrategies[0];
26  
27      for (int i = 1; i < breakStrategies.length; i++) {
28        resultingStrategy = new DslOrStrategy<R>(resultingStrategy, breakStrategies[i]);
29      }
30  
31      return resultingStrategy;
32    }
33  
34    /**
35     * Returns {@link BreakStrategy} which returns true if all of the given strategies returns true.
36     *
37     * @param breakStrategies strategies to be chained with "and" statement
38     * @param <R> the return type of target-method for which the strategy will be executed
39     * @return new strategy built out of given strategies
40     */
41    @SuppressWarnings({ "unchecked", "rawtypes" })
42    public static <R> BreakStrategy<R> allOf(BreakStrategy... breakStrategies) {
43      BreakStrategy<R> resultingStrategy = breakStrategies[0];
44  
45      for (int i = 1; i < breakStrategies.length; i++) {
46        resultingStrategy = new DslAndStrategy<R>(resultingStrategy, breakStrategies[i]);
47      }
48  
49      return resultingStrategy;
50    }
51  }