| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TimeSpanDecompositionBuilder.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using MyNet.Primitives; |
| | | 10 | | |
| | | 11 | | namespace MyNet.Temporal.Decomposition; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Builder for configuring and performing decomposition of a TimeSpan into its components based on specified options. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="decomposer">The decomposer to use for the decomposition.</param> |
| | | 17 | | /// <param name="timeSpan">The TimeSpan to decompose.</param> |
| | | 18 | | public sealed class TimeSpanDecompositionBuilder(ITimeSpanDecomposer decomposer, TimeSpan timeSpan) : ITimeSpanDecomposi |
| | | 19 | | { |
| | | 20 | | private TimeSpanDecompositionMode _mode = TimeSpanDecompositionMode.Hierarchical; |
| | | 21 | | private int? _maxComponents; |
| | | 22 | | private bool _removeZeros; |
| | | 23 | | private ITimeSpanQuantizer? _quantizer; |
| | | 24 | | private ITimeUnitRuleEngine? _ruleEngine; |
| | | 25 | | private TimeUnit _minUnit = TimeUnit.Millisecond; |
| | 6 | 26 | | private TimeUnit _maxUnit = TimeUnit.Year; |
| | | 27 | | |
| | | 28 | | /// <inheritdoc/> |
| | | 29 | | public ITimeSpanDecompositionBuilder AsHierarchical() |
| | | 30 | | { |
| | 3 | 31 | | _mode = TimeSpanDecompositionMode.Hierarchical; |
| | 3 | 32 | | return this; |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <inheritdoc/> |
| | | 36 | | public ITimeSpanDecompositionBuilder AsFlat() |
| | | 37 | | { |
| | 0 | 38 | | _mode = TimeSpanDecompositionMode.Flat; |
| | 0 | 39 | | return this; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <inheritdoc/> |
| | | 43 | | public ITimeSpanDecompositionBuilder UseLargestUnitOnly() |
| | | 44 | | { |
| | 3 | 45 | | _mode = TimeSpanDecompositionMode.LargestUnitOnly; |
| | 3 | 46 | | return this; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <inheritdoc/> |
| | | 50 | | public ITimeSpanDecompositionBuilder UseSmallestUnitOnly() |
| | | 51 | | { |
| | 0 | 52 | | _mode = TimeSpanDecompositionMode.SmallestUnitOnly; |
| | 0 | 53 | | return this; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc/> |
| | | 57 | | public ITimeSpanDecompositionBuilder MaxComponents(int count) |
| | | 58 | | { |
| | 6 | 59 | | _maxComponents = count; |
| | 6 | 60 | | return this; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <inheritdoc/> |
| | | 64 | | public ITimeSpanDecompositionBuilder WithMinUnit(TimeUnit minUnit) |
| | | 65 | | { |
| | 6 | 66 | | _minUnit = minUnit; |
| | 6 | 67 | | return this; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <inheritdoc/> |
| | | 71 | | public ITimeSpanDecompositionBuilder WithMaxUnit(TimeUnit maxUnit) |
| | | 72 | | { |
| | 6 | 73 | | _maxUnit = maxUnit; |
| | 6 | 74 | | return this; |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <inheritdoc/> |
| | | 78 | | public ITimeSpanDecompositionBuilder WithoutZeroUnits() |
| | | 79 | | { |
| | 0 | 80 | | _removeZeros = true; |
| | 0 | 81 | | return this; |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | /// <inheritdoc/> |
| | | 85 | | public ITimeSpanDecompositionBuilder WithQuantizer(ITimeSpanQuantizer quantizer) |
| | | 86 | | { |
| | 0 | 87 | | _quantizer = quantizer; |
| | 0 | 88 | | return this; |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <inheritdoc/> |
| | | 92 | | public ITimeSpanDecompositionBuilder WithUnitRules(ITimeUnitRuleEngine ruleEngine) |
| | | 93 | | { |
| | 0 | 94 | | _ruleEngine = ruleEngine; |
| | 0 | 95 | | return this; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <inheritdoc/> |
| | | 99 | | public IReadOnlyList<TimeUnitValue> Decompose() |
| | | 100 | | { |
| | 6 | 101 | | var options = new TimeSpanDecompositionOptions |
| | 6 | 102 | | { |
| | 6 | 103 | | RuleEngine = _ruleEngine ?? TimeUnitRuleSelector.Default, |
| | 6 | 104 | | MinUnit = _minUnit, |
| | 6 | 105 | | MaxUnit = _maxUnit, |
| | 6 | 106 | | Quantizer = _quantizer, |
| | 6 | 107 | | IncludeZeroUnits = !_removeZeros, |
| | 6 | 108 | | MaxComponents = _maxComponents, |
| | 6 | 109 | | Mode = _mode |
| | 6 | 110 | | }; |
| | | 111 | | |
| | 6 | 112 | | return decomposer.Decompose(timeSpan, options); |
| | | 113 | | } |
| | | 114 | | } |
| | | 115 | | |