package com.tngtech.jgiven.config;

import java.util.concurrent.ExecutionException;

import com.google.common.base.Throwables;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.tngtech.jgiven.annotation.JGivenConfiguration;
import com.tngtech.jgiven.impl.util.ReflectionUtil;

public class ConfigurationUtil {

    private static final LoadingCache<Class<?>, AbstractJGivenConfiguration> configurations = CacheBuilder.newBuilder().build(
        new CacheLoader<Class<?>, AbstractJGivenConfiguration>() {
            @Override
            public AbstractJGivenConfiguration load( Class<?> key ) throws Exception {
                AbstractJGivenConfiguration result = (AbstractJGivenConfiguration) ReflectionUtil.newInstance( key );
                result.configure();
                return result;
            }
        } );

    public static AbstractJGivenConfiguration getConfiguration( Class<? extends Object> testClass ) {
        JGivenConfiguration annotation = testClass.getAnnotation( JGivenConfiguration.class );
        Class<?> configuration;
        if( annotation == null ) {
            configuration = DefaultConfiguration.class;
        } else {
            configuration = annotation.value();
        }

        try {
            return configurations.get( configuration );
        } catch( ExecutionException e ) {
            throw Throwables.propagate( e.getCause() );
        }
    }
}

package com.tngtech.jgiven.config;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.tngtech.jgiven.annotation.JGivenConfiguration;
import com.tngtech.jgiven.impl.util.ReflectionUtil;
import java.util.Optional;
import java.util.concurrent.ExecutionException;


public class ConfigurationUtil {

    private static final LoadingCache<Class<?>, AbstractJGivenConfiguration> configurations =
        CacheBuilder.newBuilder().build(
            new CacheLoader<Class<?>, AbstractJGivenConfiguration>() {
                @Override
                public AbstractJGivenConfiguration load(Class<?> key) {
                    AbstractJGivenConfiguration result = (AbstractJGivenConfiguration) ReflectionUtil.newInstance(key);
                    result.configure();
                    return result;
                }
            });

    @SuppressWarnings({"unchecked"})
    public static <A extends AbstractJGivenConfiguration> AbstractJGivenConfiguration getConfiguration(
        Class<?> testClass) {
        Class<? extends AbstractJGivenConfiguration> configuration = Optional.ofNullable(testClass)
            .map(content -> content.getAnnotation(JGivenConfiguration.class))
            .map(content -> (Class<A>) content.value())
            .orElse((Class<A>) DefaultConfiguration.class);

        try {
            return configurations.get(configuration);
        } catch (ExecutionException e) {
            throw new RuntimeException(e.getCause());
        }
    }
}

Introduce optional into the configuration util to simplify the code
Introduce optional into the configuration util to simplify the code

Signed-off-by: l-1sqared <927badf1468b689b17391b1b1cb5895e9b797b96@users.noreply.github.com>
