<commit_before>﻿using System;
using System.Collections.Generic;
using FireworksNet.Model;

namespace FireworksNet.Generation
{
    /// <summary>
    /// Base class for spark generators.
    /// </summary>
    /// <typeparam name="T">Type of the spark that can be generated.</typeparam>
    public abstract class SparkGeneratorBase<T> : ISparkGenerator where T : ExplosionBase
    {
        /// <summary>
        /// Gets the type of the generated spark.
        /// </summary>
        public abstract FireworkType GeneratedSparkType { get; }

        /// <summary>
        /// Creates the sparks from the explosion.
        /// </summary>
        /// <param name="explosion">The explosion that is the source of sparks.</param>
        /// <returns>
        /// A collection of sparks for the specified explosion.
        /// </returns>
        /// <exception cref="System.ArgumentNullException"> if <paramref name="explosion"/>
        /// is <c>null</c>.</exception>
        /// <exception cref="System.InvalidOperationException"> if <paramref name="explosion"/>
        /// does not match <typeparamref name="T"/>.</exception>
        public virtual IEnumerable<Firework> CreateSparks(ExplosionBase explosion)
        {
            if (explosion == null)
            {
                throw new ArgumentNullException(nameof(explosion));
            }

            int desiredNumberOfSparks;
            if (!explosion.SparkCounts.TryGetValue(this.GeneratedSparkType, out desiredNumberOfSparks))
            {
                return new List<Firework>();
            }

            T typedExplosion = explosion as T;
            if (typedExplosion == null)
            {
                throw new InvalidOperationException();
            }

            List<Firework> sparks = new List<Firework>(desiredNumberOfSparks);
            for (int i = 0; i < desiredNumberOfSparks; i++)
            {
                sparks.Add(this.CreateSparkTyped(typedExplosion));
            }

            return sparks;
        }

        /// <summary>
        /// Creates the spark from the explosion.
        /// </summary>
        /// <param name="explosion">The explosion that is the source of sparks.</param>
        /// <returns>A spark for the specified explosion.</returns>
        /// <exception cref="System.ArgumentNullException"> if <paramref name="explosion"/>
        /// is <c>null</c>.</exception>
        /// <exception cref="System.InvalidOperationException"> if <paramref name="explosion"/>
        /// does not match <typeparamref name="T"/>.</exception>
        public virtual Firework CreateSpark(ExplosionBase explosion)
        {
            if (explosion == null)
            {
                throw new ArgumentNullException(nameof(explosion));
            }

            T typedExplosion = explosion as T;
            if (typedExplosion == null)
            {
                throw new InvalidOperationException();
            }

            return this.CreateSparkTyped(typedExplosion);
        }

        /// <summary>
        /// Creates the typed spark.
        /// </summary>
        /// <param name="explosion">The explosion that gives birth to the spark.</param>
        /// <returns>The new typed spark.</returns>
        protected abstract Firework CreateSparkTyped(T explosion);
    }
}<commit_msg>Minor refactoring of the SparkGeneratorBase<TExplosion>.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using FireworksNet.Model;

namespace FireworksNet.Generation
{
    /// <summary>
    /// Base class for spark generators.
    /// </summary>
    /// <typeparam name="TExplosion">Type of the explosion.</typeparam>
    public abstract class SparkGeneratorBase<TExplosion> : ISparkGenerator where TExplosion : ExplosionBase
    {
        /// <summary>
        /// Gets the type of the generated spark.
        /// </summary>
        public abstract FireworkType GeneratedSparkType { get; }

        /// <summary>
        /// Creates the sparks from the explosion.
        /// </summary>
        /// <param name="explosion">The explosion that is the source of sparks.</param>
        /// <returns>
        /// A collection of sparks for the specified explosion.
        /// </returns>
        /// <exception cref="System.ArgumentNullException"> if <paramref name="explosion"/>
        /// is <c>null</c>.</exception>
        /// <exception cref="System.InvalidOperationException"> if <paramref name="explosion"/>
        /// does not match <typeparamref name="TExplosion"/>.</exception>
        public virtual IEnumerable<Firework> CreateSparks(ExplosionBase explosion)
        {
            if (explosion == null)
            {
                throw new ArgumentNullException(nameof(explosion));
            }

            int desiredNumberOfSparks;
            if (!explosion.SparkCounts.TryGetValue(this.GeneratedSparkType, out desiredNumberOfSparks))
            {
                return Enumerable.Empty<Firework>();
            }

            TExplosion typedExplosion = SparkGeneratorBase<TExplosion>.GetTypedExplosion(explosion);
            IList<Firework> sparks = new List<Firework>(desiredNumberOfSparks);
            for (int i = 0; i < desiredNumberOfSparks; i++)
            {
                sparks.Add(this.CreateSparkTyped(typedExplosion));
            }

            return sparks;
        }

        /// <summary>
        /// Creates the spark from the explosion.
        /// </summary>
        /// <param name="explosion">The explosion that is the source of sparks.</param>
        /// <returns>A spark for the specified explosion.</returns>
        /// <exception cref="System.ArgumentNullException"> if <paramref name="explosion"/>
        /// is <c>null</c>.</exception>
        /// <exception cref="System.InvalidOperationException"> if <paramref name="explosion"/>
        /// does not match <typeparamref name="TExplosion"/>.</exception>
        public virtual Firework CreateSpark(ExplosionBase explosion)
        {
            if (explosion == null)
            {
                throw new ArgumentNullException(nameof(explosion));
            }

            TExplosion typedExplosion = SparkGeneratorBase<TExplosion>.GetTypedExplosion(explosion);
            return this.CreateSparkTyped(typedExplosion);
        }

        /// <summary>
        /// Creates the typed spark.
        /// </summary>
        /// <param name="explosion">The explosion that gives birth to the spark.</param>
        /// <returns>The new typed spark.</returns>
        protected abstract Firework CreateSparkTyped(TExplosion explosion);

        /// <summary>
        /// Casts <paramref name="explosion"/> to the <typeparamref name="TExplosion"/>.
        /// </summary>
        /// <param name="explosion">The firework explosion.</param>
        /// <returns>Typed firework explosion.</returns>
        /// <exception cref="System.InvalidOperationException"> if <paramref name="explosion"/>
        /// does not match <typeparamref name="TExplosion"/>.</exception>
        private static TExplosion GetTypedExplosion(ExplosionBase explosion)
        {
            Debug.Assert(explosion != null, "Explosion is null");

            TExplosion typedExplosion = explosion as TExplosion;
            if (typedExplosion == null)
            {
                throw new InvalidOperationException();
            }

            return typedExplosion;
        }
    }
}
{'fraction_non_alphanumeric': 0.11880789367700363, 'fraction_numerical': 0.0002684924150892737, 'mean_word_length': 2.0470347648261757, 'pattern_counts': {'":': 0, '<': 114, '<?xml version=': 0, '>': 112, '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': '3307680', 'n_tokens_mistral': 1988, 'n_tokens_neox': 1858, 'n_words': 482}