<commit_before>﻿using AncoraMVVM.Base.Interfaces;
using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;

namespace AncoraMVVM.Phone.Implementations
{
    internal static class TypeExtensions
    {
        public static bool HasParameterlessConstructor(this Type type)
        {
            return type.GetConstructor(Type.EmptyTypes) != null;
        }
    }

    public class ConfigurationManager : IConfigurationManager
    {
        private static Dictionary<string, object> cachedObjects = new Dictionary<string, object>();
        private static object dicLock = new object();
        private static object storageLock = new object();

        private IsolatedStorageSettings config = IsolatedStorageSettings.ApplicationSettings;

        private T CreateDefault<T>()
        {
            var type = typeof(T);

            if (type.HasParameterlessConstructor())
                return Activator.CreateInstance<T>();
            else
                return default(T);
        }

        public T Get<T>(ConfigItem<T> key)
        {
            object cached;

            T item;

            try
            {
                lock (dicLock)
                    if (cachedObjects.TryGetValue(key.Key, out cached))
                        return (T)cached;

                lock (storageLock)
                {
                    if (!config.TryGetValue<T>(key.Key, out item))
                    {
                        if (key.DefaultValue != null)
                            item = key.DefaultValue;
                        else
                            item = CreateDefault<T>();

                        config.Add(key.Key, item);
                        config.Save();
                    }
                }
            }
            catch (InvalidCastException)
            {
                item = CreateDefault<T>();
                lock (storageLock)
                {
                    config.Remove(key.Key);
                    config.Save();
                }
            }

            if (item == null)
                item = CreateDefault<T>();

            cachedObjects[key.Key] = item;

            return item;
        }

        public void Set<T>(ConfigItem<T> key, T item)
        {
            lock (dicLock)
                cachedObjects[key.Key] = item;

            lock (storageLock)
            {
                config[key.Key] = item;
                config.Save();
            }
        }
    }
}<commit_msg>Minor tweaks on configuration manager.<commit_after>﻿using AncoraMVVM.Base.Interfaces;
using System;
using System.Collections.Generic;
using System.IO.IsolatedStorage;

namespace AncoraMVVM.Phone.Implementations
{
    internal static class TypeExtensions
    {
        public static bool HasParameterlessConstructor(this Type type)
        {
            return type.GetConstructor(Type.EmptyTypes) != null;
        }
    }

    public class ConfigurationManager : IConfigurationManager
    {
        private static Dictionary<string, object> cachedObjects = new Dictionary<string, object>();
        private static object dicLock = new object();
        private static object storageLock = new object();

        private IsolatedStorageSettings config = IsolatedStorageSettings.ApplicationSettings;

        public T Get<T>(ConfigItem<T> key)
        {
            object cached;

            T item;

            try
            {
                lock (dicLock)
                    if (cachedObjects.TryGetValue(key.Key, out cached))
                        return (T)cached;

                lock (storageLock)
                {
                    if (!config.TryGetValue<T>(key.Key, out item))
                    {
                        item = key.DefaultValue;

                        config.Add(key.Key, item);
                        config.Save();
                    }
                }
            }
            catch (InvalidCastException)
            {
                item = default(T);
                lock (storageLock)
                {
                    config.Remove(key.Key);
                    config.Save();
                }
            }
            
            cachedObjects[key.Key] = item;

            return item;
        }

        public void Set<T>(ConfigItem<T> key, T item)
        {
            lock (dicLock)
                cachedObjects[key.Key] = item;

            lock (storageLock)
            {
                config[key.Key] = item;
                config.Save();
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08296557811120918, 'fraction_numerical': 0.0, 'mean_word_length': 1.254102436598707, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, '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': '5625308', 'n_tokens_mistral': 1114, 'n_tokens_neox': 1038, 'n_words': 240}