<commit_before>﻿namespace LostTech.App
{
    using System;
    using System.Threading.Tasks;
    using JetBrains.Annotations;
    using PCLStorage;
    using FileAccess = PCLStorage.FileAccess;

    public sealed class Settings
    {
        readonly IFolder folder;
        readonly IFreezerFactory freezerFactory;
        readonly ISerializerFactory serializerFactory;
        readonly IDeserializerFactory deserializerFactory;

        public Settings([NotNull] IFolder folder,
            [NotNull] IFreezerFactory freezerFactory,
            [NotNull] ISerializerFactory serializerFactory,
            [NotNull] IDeserializerFactory deserializerFactory)
        {
            this.folder = folder ?? throw new ArgumentNullException(nameof(folder));
            this.freezerFactory = freezerFactory ?? throw new ArgumentNullException(nameof(freezerFactory));
            this.serializerFactory = serializerFactory ?? throw new ArgumentNullException(nameof(serializerFactory));
            this.deserializerFactory = deserializerFactory ?? throw new ArgumentNullException(nameof(deserializerFactory));
        }

        public async Task<SettingsSet<T, TFreezed>> Load<T, TFreezed>([NotNull] string fileName) where T: class
        {
            if (string.IsNullOrEmpty(fileName))
                throw new ArgumentNullException(nameof(fileName));

            var file = await this.folder.GetFile(fileName).ConfigureAwait(false);
            if (file == null)
                return null;

            T value;
            using (var stream = await file.OpenAsync(FileAccess.Read).ConfigureAwait(false))
                value = await deserializerFactory.MakeDeserializer<T>()(stream).ConfigureAwait(false);

            return new SettingsSet<T, TFreezed>(file, value,
                this.freezerFactory.MakeFreezer<T, TFreezed>(),
                this.serializerFactory.MakeSerializer<TFreezed>());
        }
    }
}
<commit_msg>added Settings.LoadOrCreate<commit_after>﻿namespace LostTech.App
{
    using System;
    using System.Threading.Tasks;
    using JetBrains.Annotations;
    using PCLStorage;
    using FileAccess = PCLStorage.FileAccess;

    public sealed class Settings
    {
        readonly IFolder folder;
        readonly IFreezerFactory freezerFactory;
        readonly ISerializerFactory serializerFactory;
        readonly IDeserializerFactory deserializerFactory;

        public Settings([NotNull] IFolder folder,
            [NotNull] IFreezerFactory freezerFactory,
            [NotNull] ISerializerFactory serializerFactory,
            [NotNull] IDeserializerFactory deserializerFactory)
        {
            this.folder = folder ?? throw new ArgumentNullException(nameof(folder));
            this.freezerFactory = freezerFactory ?? throw new ArgumentNullException(nameof(freezerFactory));
            this.serializerFactory = serializerFactory ?? throw new ArgumentNullException(nameof(serializerFactory));
            this.deserializerFactory = deserializerFactory ?? throw new ArgumentNullException(nameof(deserializerFactory));
        }

        public async Task<SettingsSet<T, TFreezed>> Load<T, TFreezed>([NotNull] string fileName) where T: class
        {
            if (string.IsNullOrEmpty(fileName))
                throw new ArgumentNullException(nameof(fileName));

            var file = await this.folder.GetFile(fileName).ConfigureAwait(false);
            if (file == null)
                return null;

            T value;
            using (var stream = await file.OpenAsync(FileAccess.Read).ConfigureAwait(false))
                value = await deserializerFactory.MakeDeserializer<T>()(stream).ConfigureAwait(false);

            return new SettingsSet<T, TFreezed>(file, value,
                this.freezerFactory.MakeFreezer<T, TFreezed>(),
                this.serializerFactory.MakeSerializer<TFreezed>());
        }

        public Task<SettingsSet<T, TFreezed>> LoadOrCreate<T, TFreezed>([NotNull] string fileName)
            where T : class, new()
            => this.LoadOrCreate<T, TFreezed>(fileName, () => new T());

        public async Task<SettingsSet<T, TFreezed>> LoadOrCreate<T, TFreezed>([NotNull] string fileName, Func<T> defaultSettings)
            where T : class
        {
            if (defaultSettings == null)
                throw new ArgumentNullException(nameof(defaultSettings));

            var settings = await this.Load<T, TFreezed>(fileName).ConfigureAwait(false);
            if (settings != null)
                return settings;

            var file = await this.folder.CreateFileAsync(fileName, CreationCollisionOption.FailIfExists).ConfigureAwait(false);
            return new SettingsSet<T, TFreezed>(file, defaultSettings(),
                this.freezerFactory.MakeFreezer<T, TFreezed>(),
                this.serializerFactory.MakeSerializer<TFreezed>());
        }
    }
}

{'fraction_non_alphanumeric': 0.0875, 'fraction_numerical': 0.0, 'mean_word_length': 2.8073322932917315, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, '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': '5852085', 'n_tokens_mistral': 1253, 'n_tokens_neox': 1234, 'n_words': 268}