@@ -0,0 +1,2230 @@
+/*%0A * Copyright 2014-2015 the original author or authors.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, %0A * either express or implied. See the License for the specific language%0A * governing permissions and limitations under the License.%0A */%0Apackage org.lastaflute.web.api;%0A%0Aimport java.util.Collections;%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0Aimport java.util.function.Consumer;%0A%0Aimport org.dbflute.optional.OptionalThing;%0A%0A/**%0A * @author jflute%0A * @param %3CVALUE%3E The type of value corresponding to failure type.%0A * @since 0.7.6 (2016/01/06 Wednesday)%0A */%0Apublic class BusinessFailureMapping%3CVALUE%3E %7B%0A%0A    protected final Map%3CClass%3C?%3E, VALUE%3E failureMap;%0A%0A    /**%0A     * @param oneArgLambda The callback to initialize failure map keyed by type. (NotNull)%0A     */%0A    public BusinessFailureMapping(Consumer%3CMap%3CClass%3C?%3E, VALUE%3E%3E oneArgLambda) %7B%0A        final Map%3CClass%3C?%3E, VALUE%3E failureMap = new HashMap%3CClass%3C?%3E, VALUE%3E();%0A        oneArgLambda.accept(failureMap);%0A        this.failureMap = Collections.unmodifiableMap(failureMap);%0A    %7D%0A%0A    /**%0A     * @param cause The cause exception to find value. (NotNull)%0A     * @return The optional value corresponding to failure type. (NotNull, EmptyAllowed)%0A     */%0A    public OptionalThing%3CVALUE%3E findAssignable(RuntimeException cause) %7B%0A        final Class%3C?%3E key = cause.getClass();%0A        final VALUE found = failureMap.get(key);%0A        if (found != null) %7B%0A            return OptionalThing.of(found);%0A        %7D else %7B%0A            return OptionalThing.migratedFrom(failureMap.entrySet().stream().filter(entry -%3E %7B%0A                return entry.getKey().isAssignableFrom(key);%0A            %7D).map(entry -%3E entry.getValue()).findFirst(), () -%3E %7B%0A                throw new IllegalStateException(%22Not found the exception type in the map: %22 + failureMap.keySet());%0A            %7D);%0A        %7D%0A    %7D%0A%7D%0A

make business failure mapping