1 package com.github.davidmoten.aws.lw.client.internal;
2
3 import java.util.Optional;
4 import java.util.function.Function;
5 import java.util.function.Predicate;
6
7 import com.github.davidmoten.aws.lw.client.ExceptionFactory;
8 import com.github.davidmoten.aws.lw.client.Response;
9
10 public class ExceptionFactoryExtended implements ExceptionFactory {
11
12 private final ExceptionFactory factory;
13 private final Predicate<? super Response> predicate;
14 private final Function<? super Response, ? extends RuntimeException> function;
15
16 public ExceptionFactoryExtended(ExceptionFactory factory, Predicate<? super Response> predicate, Function<? super Response, ? extends RuntimeException> function) {
17 this.factory = factory;
18 this.predicate = predicate;
19 this.function = function;
20 }
21
22 @Override
23 public Optional<? extends RuntimeException> create(Response response) {
24 if (predicate.test(response)) {
25 return Optional.of(function.apply(response));
26 } else {
27 return factory.create(response);
28 }
29 }
30 }