<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dolstagis.Web.Http;

namespace Dolstagis.Web
{
    /// <summary>
    ///  Provides a mechanism to fetch the appropriate feature set for a request.
    /// </summary>

    public class FeatureSwitchboard
    {
        private List<IFeatureSwitch> switches = new List<IFeatureSwitch>();

        private Dictionary<ulong, FeatureSet> featureSets = new Dictionary<ulong, FeatureSet>();


        public Application Application { get; private set; }

        public FeatureSwitchboard(Application application)
        {
            this.Application = application;
        }

        public FeatureSwitchboard Add(params IFeatureSwitch[] switches)
        {
            this.switches.AddRange(switches);
            this.featureSets.Clear();
            return this;
        }


        public FeatureSwitchboard Add(params Feature[] features)
        {
            this.switches.AddRange(features.Select(f => new AlwaysEnabledFeatureSwitch(f)));
            return this;
        }

        private async Task<ulong> GetKey(IRequest request)
        {
            ulong key = 0;
            foreach (var sw in switches)
            {
                if (sw.DependentOnRequest)
                {
                    checked { 
                        key = key << 1;
                    }
                    if (await sw.IsEnabledForRequest(request))
                    {
                        key++;
                    }
                }
            }
            return key;
        }

        private async Task<FeatureSet> CreateFeatureSet(IRequest request)
        {
            var features = new List<Feature>();
            foreach (var sw in switches) {
                if (await sw.IsEnabledForRequest(request)) {
                    features.Add(sw.Feature);
                }
            }

            return new FeatureSet(Application, features);
        }

        public async Task<FeatureSet> GetFeatureSet(IRequest request)
        {
            ulong key = await GetKey(request);
            FeatureSet result;
            if (!featureSets.TryGetValue(key, out result)) {
                result = await CreateFeatureSet(request);
                featureSets.Add(key, result);
            }
            return result;
        }

        private class AlwaysEnabledFeatureSwitch : IFeatureSwitch
        {
            public AlwaysEnabledFeatureSwitch(Feature feature)
            {
                this.Feature = feature;
            }

            public async Task<bool> IsEnabledForRequest(IRequest request)
            {
                return true;
            }

            public Feature Feature { get; private set; }

            public bool DependentOnRequest { get { return false; } }
        }
    }
}<commit_msg>Kill compiler warning about synchronous result<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Dolstagis.Web.Http;

namespace Dolstagis.Web
{
    /// <summary>
    ///  Provides a mechanism to fetch the appropriate feature set for a request.
    /// </summary>

    public class FeatureSwitchboard
    {
        private List<IFeatureSwitch> switches = new List<IFeatureSwitch>();

        private Dictionary<ulong, FeatureSet> featureSets = new Dictionary<ulong, FeatureSet>();


        public Application Application { get; private set; }

        public FeatureSwitchboard(Application application)
        {
            this.Application = application;
        }

        public FeatureSwitchboard Add(params IFeatureSwitch[] switches)
        {
            this.switches.AddRange(switches);
            this.featureSets.Clear();
            return this;
        }


        public FeatureSwitchboard Add(params Feature[] features)
        {
            this.switches.AddRange(features.Select(f => new AlwaysEnabledFeatureSwitch(f)));
            return this;
        }

        private async Task<ulong> GetKey(IRequest request)
        {
            ulong key = 0;
            foreach (var sw in switches)
            {
                if (sw.DependentOnRequest)
                {
                    checked { 
                        key = key << 1;
                    }
                    if (await sw.IsEnabledForRequest(request))
                    {
                        key++;
                    }
                }
            }
            return key;
        }

        private async Task<FeatureSet> CreateFeatureSet(IRequest request)
        {
            var features = new List<Feature>();
            foreach (var sw in switches) {
                if (await sw.IsEnabledForRequest(request)) {
                    features.Add(sw.Feature);
                }
            }

            return new FeatureSet(Application, features);
        }

        public async Task<FeatureSet> GetFeatureSet(IRequest request)
        {
            ulong key = await GetKey(request);
            FeatureSet result;
            if (!featureSets.TryGetValue(key, out result)) {
                result = await CreateFeatureSet(request);
                featureSets.Add(key, result);
            }
            return result;
        }

        private class AlwaysEnabledFeatureSwitch : IFeatureSwitch
        {
            public AlwaysEnabledFeatureSwitch(Feature feature)
            {
                this.Feature = feature;
            }

            public async Task<bool> IsEnabledForRequest(IRequest request)
            {
                return await Task.FromResult(true);
            }

            public Feature Feature { get; private set; }

            public bool DependentOnRequest { get { return false; } }
        }
    }
}
{'fraction_non_alphanumeric': 0.07786180818150568, 'fraction_numerical': 0.0006875214850464077, 'mean_word_length': 1.5278019113814074, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1675403', 'n_tokens_mistral': 1414, 'n_tokens_neox': 1338, 'n_words': 346}