<commit_before>/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * Copyright 2013-2016 the original author or authors.
 */

package org.ff4j.spring.boot.services;


import org.apache.commons.lang3.StringUtils;
import org.ff4j.FF4j;
import org.ff4j.cache.FF4jCacheProxy;
import org.ff4j.core.Feature;
import org.ff4j.spring.boot.domain.CacheApiBean;
import org.ff4j.spring.boot.domain.FeatureApiBean;
import org.ff4j.spring.boot.domain.FeatureStoreApiBean;
import org.ff4j.spring.boot.exceptions.FeatureStoreNotCached;
import org.ff4j.spring.boot.domain.GroupDescApiBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by Paul
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
@Service
public class FeatureStoreServices {
    @Autowired
    private FF4j ff4j;

    public FeatureStoreApiBean getFeatureStore() {
        return new FeatureStoreApiBean(ff4j.getFeatureStore());
    }

    public Collection<FeatureApiBean> getAllFeatures() {
        List<FeatureApiBean> features;
        Map<String, Feature> featureMap = ff4j.getFeatureStore().readAll();
        if (CollectionUtils.isEmpty(featureMap)) {
            features = new ArrayList<>(0);
        } else {
            features = new ArrayList<>(featureMap.size());
            features.addAll(featureMap.values().stream().map(FeatureApiBean::new).collect(Collectors.toList()));
        }
        return features;
    }

    public Collection<GroupDescApiBean> getAllGroups() {
        Map<String, GroupDescApiBean> groups = new HashMap<>();
        Map<String, Feature> featureMap = ff4j.getFeatureStore().readAll();
        if (!CollectionUtils.isEmpty(featureMap)) {
            for (Feature feature : featureMap.values()) {
                initGroupMap(groups, feature.getUid(), feature.getGroup());
            }
        }
        return groups.values();
    }

    private void initGroupMap(Map<String, GroupDescApiBean> groups, String featureUID, String groupName) {
        if (StringUtils.isNotBlank(groupName)) {
            if (groups.containsKey(groupName)) {
                groups.get(groupName).getFeatures().add(featureUID);
            } else {
                groups.put(groupName, new GroupDescApiBean(groupName, new ArrayList<>()));
                groups.get(groupName).getFeatures().add(featureUID);
            }
        }
    }

    public void deleteAllFeatures() {
        ff4j.getFeatureStore().clear();
    }<commit_msg>Refactored<commit_after>/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * Copyright 2013-2016 the original author or authors.
 */

package org.ff4j.spring.boot.services;


import org.apache.commons.lang3.StringUtils;
import org.ff4j.FF4j;
import org.ff4j.cache.FF4jCacheProxy;
import org.ff4j.core.Feature;
import org.ff4j.spring.boot.domain.CacheApiBean;
import org.ff4j.spring.boot.domain.FeatureApiBean;
import org.ff4j.spring.boot.domain.FeatureStoreApiBean;
import org.ff4j.spring.boot.domain.GroupDescApiBean;
import org.ff4j.spring.boot.exceptions.FeatureStoreNotCached;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by Paul
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
@Service
public class FeatureStoreServices {
    @Autowired
    private FF4j ff4j;

    public FeatureStoreApiBean getFeatureStore() {
        return new FeatureStoreApiBean(ff4j.getFeatureStore());
    }

    public Collection<FeatureApiBean> getAllFeatures() {
        List<FeatureApiBean> features;
        Map<String, Feature> featureMap = ff4j.getFeatureStore().readAll();
        if (CollectionUtils.isEmpty(featureMap)) {
            features = new ArrayList<>(0);
        } else {
            features = new ArrayList<>(featureMap.size());
            features.addAll(featureMap.values().stream().map(FeatureApiBean::new).collect(Collectors.toList()));
        }
        return features;
    }

    public Collection<GroupDescApiBean> getAllGroups() {
        Map<String, GroupDescApiBean> groups = new HashMap<>();
        Map<String, Feature> featureMap = ff4j.getFeatureStore().readAll();
        if (!CollectionUtils.isEmpty(featureMap)) {
            for (Feature feature : featureMap.values()) {
                initGroupMap(groups, feature.getUid(), feature.getGroup());
            }
        }
        return groups.values();
    }

    private void initGroupMap(Map<String, GroupDescApiBean> groups, String featureUID, String groupName) {
        if (StringUtils.isNotBlank(groupName)) {
            if (!groups.containsKey(groupName)) {
                groups.put(groupName, new GroupDescApiBean(groupName, new ArrayList<>()));
            }
            groups.get(groupName).getFeatures().add(featureUID);
        }
    }

    public void deleteAllFeatures() {
        ff4j.getFeatureStore().clear();
    }
{'fraction_non_alphanumeric': 0.11102000655952772, 'fraction_numerical': 0.010167267956707117, 'mean_word_length': 3.406791907514451, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4828629', 'n_tokens_mistral': 1721, 'n_tokens_neox': 1687, 'n_words': 408}