All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.cloudyrock.dimmer.DimmerFeatureConfigurable Maven / Gradle / Ivy

Go to download

Lightweight library to manage feature toggling. Unlike others Dimmer doesn't work in switch mode, black or white, It provides a flexible way to respond to disabled features by adding customizable behaviours and pre-configured responses.

There is a newer version: 2.2.2
Show newest version
package com.github.cloudyrock.dimmer;

import com.github.cloudyrock.dimmer.exceptions.DimmerInvocationException;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;

abstract class DimmerFeatureConfigurable {

    protected static final Class DEFAULT_EXCEPTION_TYPE =
            DimmerInvocationException.class;

    protected final Collection environments;

    protected final Map> configMetadata;

    protected final Class defaultExceptionType;

    protected DimmerFeatureConfigurable(
            Collection environments,
            Map> configMetadata,
            Class defaultExceptionType) {
        this.environments = environments;
        this.configMetadata = configMetadata;
        this.defaultExceptionType = defaultExceptionType;
    }

    public RUNNER environments(String... environments) {
        Util.checkArgumentNullEmpty(environments, "environments");
        return newInstance(
                Arrays.asList(environments),
                this.configMetadata,
                this.defaultExceptionType);
    }

    public RUNNER featureWithBehaviour(
            String feature,
            Function behaviour) {
        final FeatureMetadataBehaviour metadata = new FeatureMetadataBehaviour(
                feature,
                behaviour
        );
        addFeatureMetadata(metadata);
        return newInstance(this.environments, this.configMetadata,
                this.defaultExceptionType);

    }

    public RUNNER featureWithDefaultException(String feature) {
        final FeatureMetadata metadata = new FeatureMetadataDefaultException(
                feature
        );
        addFeatureMetadata(metadata);
        return newInstance(this.environments, this.configMetadata,
                this.defaultExceptionType);
    }

    public RUNNER featureWithException(
            String feature,
            Class exceptionType) {
        ExceptionUtil.checkExceptionConstructorType(exceptionType);
        final FeatureMetadata metadata = new FeatureMetadataException(
                feature,
                exceptionType
        );
        addFeatureMetadata(metadata);
        return newInstance(this.environments, this.configMetadata,
                this.defaultExceptionType);
    }

    public RUNNER featureWithValue(String feature,
                                   Object valueToReturn) {
        final FeatureMetadata metadata = new FeatureMetadataValue(
                feature,
                valueToReturn
        );
        addFeatureMetadata(metadata);
        return newInstance(this.environments, this.configMetadata,
                this.defaultExceptionType);

    }

    private void addFeatureMetadata(FeatureMetadata metadata) {
        this.environments.forEach(env -> {
            if (!configMetadata.containsKey(env)) {
                configMetadata.put(env, new HashSet<>());
            }
            configMetadata.get(env).add(metadata);
        });
    }

    /**
     * Set the default exception type to be thrown as behaviour.
     * 

* Notice the exception type must have either an empty constructor or a contractor with only * one parameter, (@{@link FeatureInvocation}) * * @param newDefaultExceptionType new default exception type * @return Singleton DimmerProcessor builder */ public RUNNER setDefaultExceptionType( Class newDefaultExceptionType) { Util.checkArgumentNullEmpty(newDefaultExceptionType, "defaultExceptionType"); ExceptionUtil.checkExceptionConstructorType(newDefaultExceptionType); return newInstance(this.environments, this.configMetadata, newDefaultExceptionType); } FeatureProcessorBase newFeatureProcessor(Set featureMetadataSet) { FeatureProcessorBase processor = newFeatureProcessorInstance(); if (featureMetadataSet == null) { featureMetadataSet.stream() .filter(fm -> fm instanceof FeatureMetadataBehaviour) .map(fm -> (FeatureMetadataBehaviour) fm) .forEach(fmb -> processor.featureWithBehaviour( fmb.getFeature(), fmb.getBehaviour())); featureMetadataSet.stream() .filter(fm -> fm instanceof FeatureMetadataException) .map(fm -> (FeatureMetadataException) fm) .forEach(fme -> processor.featureWithException( fme.getFeature(), fme.getException() )); final Class exceptionType = getDefaultExceptionType(); featureMetadataSet.stream() .filter(fm -> fm instanceof FeatureMetadataDefaultException) .map(fm -> (FeatureMetadataDefaultException) fm) .forEach(fmde -> processor .featureWithException(fmde.getFeature(), exceptionType)); featureMetadataSet.stream() .filter(fm -> fm instanceof FeatureMetadataValue) .map(fm -> (FeatureMetadataValue) fm) .forEach(fmv -> processor.featureWithValue( fmv.getFeature(), fmv.getValueToReturn()) ); } return processor; } private Class getDefaultExceptionType() { return this.defaultExceptionType != null ? this.defaultExceptionType : DEFAULT_EXCEPTION_TYPE; } protected abstract RUNNER newInstance( Collection environments, Map> configMetadata, Class newDefaultExceptionType); protected abstract FeatureProcessorBase newFeatureProcessorInstance(); }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy