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

de.otto.edison.togglz.configuration.InMemoryFeatureStateRepositoryConfiguration Maven / Gradle / Ivy

There is a newer version: 3.4.0
Show newest version
package de.otto.edison.togglz.configuration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.togglz.core.Feature;
import org.togglz.core.repository.FeatureState;
import org.togglz.core.repository.StateRepository;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class InMemoryFeatureStateRepositoryConfiguration {

    @Bean
    @ConditionalOnMissingBean(StateRepository.class)
    public StateRepository stateRepository() {
        return createInMemoryStateRepository();
    }


    private StateRepository createInMemoryStateRepository() {
        return new StateRepository() {

            Logger LOG = LoggerFactory.getLogger(TogglzConfiguration.class);

            private Map featureStore = new HashMap<>();

            @Override
            public FeatureState getFeatureState(final Feature feature) {
                if (featureStore.containsKey(feature.name())) {
                    return featureStore.get(feature.name());
                }
                return new FeatureState(feature, false);
            }

            @Override
            public void setFeatureState(final FeatureState featureState) {
                featureStore.put(featureState.getFeature().name(), featureState);
                LOG.info("Switched feature state to " + featureState.toString());
            }
        };
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy