|
| 1 | +using System.Collections.Generic; |
| 2 | + |
| 3 | +namespace Tynamix.ObjectFiller |
| 4 | +{ |
| 5 | + /// <summary> |
| 6 | + /// The collectionizer can be used to create <see cref="List{T}"/> based on a <see cref="IRandomizerPlugin{T}"/>. |
| 7 | + /// </summary> |
| 8 | + /// <typeparam name="T">Typeparameter of of the target List</typeparam> |
| 9 | + /// <typeparam name="TRandomizer">Plugin which will be used to create the List</typeparam> |
| 10 | + public class Collectionizer<T, TRandomizer> : IRandomizerPlugin<List<T>> |
| 11 | + where TRandomizer : IRandomizerPlugin<T>, new() |
| 12 | + { |
| 13 | + private readonly IRandomizerPlugin<T> randomizerToUse; |
| 14 | + private readonly int minCount; |
| 15 | + private readonly int maxCount; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Initializes a new instance of the <see cref="Collectionizer{T,TRandomizer}"/> class. |
| 19 | + /// </summary> |
| 20 | + public Collectionizer() |
| 21 | + : this(new TRandomizer()) |
| 22 | + { |
| 23 | + } |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Initializes a new instance of the <see cref="Collectionizer{T,TRandomizer}"/> class. |
| 27 | + /// </summary> |
| 28 | + /// <param name="minCount">Min count of list items</param> |
| 29 | + public Collectionizer(uint minCount) |
| 30 | + : this(new TRandomizer(), minCount) |
| 31 | + { |
| 32 | + } |
| 33 | + |
| 34 | + /// <summary> |
| 35 | + /// Initializes a new instance of the <see cref="Collectionizer{T,TRandomizer}"/> class. |
| 36 | + /// </summary> |
| 37 | + /// <param name="minCount">Min count of list items</param> |
| 38 | + /// <param name="maxCount">Max count of list items</param> |
| 39 | + public Collectionizer(uint minCount, uint maxCount) |
| 40 | + : this(new TRandomizer(), minCount, maxCount) |
| 41 | + { |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Initializes a new instance of the <see cref="Collectionizer{T,TRandomizer}"/> class. |
| 46 | + /// </summary> |
| 47 | + /// <param name="randomizerToUse">The randomizer which will be used. Use this if you want |
| 48 | + /// to specifiy how the randomizer to use should work</param> |
| 49 | + public Collectionizer(TRandomizer randomizerToUse) |
| 50 | + : this(randomizerToUse, 10) |
| 51 | + { |
| 52 | + |
| 53 | + } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Initializes a new instance of the <see cref="Collectionizer{T,TRandomizer}"/> class. |
| 57 | + /// </summary> |
| 58 | + /// <param name="randomizerToUse">The randomizer which will be used. Use this if you want |
| 59 | + /// to specifiy how the randomizer to use should work</param> |
| 60 | + /// <param name="minCount">Min count of list items</param> |
| 61 | + public Collectionizer(TRandomizer randomizerToUse, uint minCount) |
| 62 | + : this(randomizerToUse, minCount, 50) |
| 63 | + { |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Initializes a new instance of the <see cref="Collectionizer{T,TRandomizer}"/> class. |
| 68 | + /// </summary> |
| 69 | + /// <param name="randomizerToUse">The randomizer which will be used. Use this if you want |
| 70 | + /// to specifiy how the randomizer to use should work</param> |
| 71 | + /// <param name="minCount">Min count of list items</param> |
| 72 | + /// <param name="maxCount">Max count of list items</param> |
| 73 | + public Collectionizer(TRandomizer randomizerToUse, uint minCount, uint maxCount) |
| 74 | + { |
| 75 | + if (randomizerToUse == null) |
| 76 | + { |
| 77 | + randomizerToUse = new TRandomizer(); |
| 78 | + } |
| 79 | + |
| 80 | + if (minCount > maxCount) |
| 81 | + { |
| 82 | + maxCount = minCount; |
| 83 | + minCount = maxCount; |
| 84 | + } |
| 85 | + |
| 86 | + this.randomizerToUse = randomizerToUse; |
| 87 | + this.minCount = (int)minCount; |
| 88 | + this.maxCount = (int)maxCount; |
| 89 | + } |
| 90 | + |
| 91 | + |
| 92 | + /// <summary> |
| 93 | + /// Gets random data for type <see cref="T"/> |
| 94 | + /// </summary> |
| 95 | + /// <returns>Random data for type <see cref="T"/></returns> |
| 96 | + public List<T> GetValue() |
| 97 | + { |
| 98 | + var count = Randomizer<int>.Create(new IntRange(this.minCount, this.maxCount)); |
| 99 | + List<T> result = new List<T>(); |
| 100 | + |
| 101 | + for (int i = 0; i < count; i++) |
| 102 | + { |
| 103 | + result.Add(this.randomizerToUse.GetValue()); |
| 104 | + } |
| 105 | + |
| 106 | + return result; |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments