| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TimeHumanizationBuilder.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using MyNet.Humanizer.Formatting.Collections; |
| | | 9 | | using MyNet.Primitives; |
| | | 10 | | using MyNet.Temporal.Decomposition; |
| | | 11 | | |
| | | 12 | | namespace MyNet.Humanizer.Temporal; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Fluent builder used to configure <see cref="TimeHumanizationOptions"/>. |
| | | 16 | | /// </summary> |
| | | 17 | | public sealed class TimeHumanizationBuilder |
| | | 18 | | { |
| | 15 | 19 | | private TimeHumanizationMode _mode = TimeHumanizationMode.Duration; |
| | 15 | 20 | | private TimeUnit _minUnit = TimeUnit.Second; |
| | 15 | 21 | | private TimeUnit _maxUnit = TimeUnit.Year; |
| | 15 | 22 | | private int? _maxComponents = 2; |
| | | 23 | | private bool _includeZeroUnits; |
| | | 24 | | private TimeSpanDecompositionMode _decompositionMode = TimeSpanDecompositionMode.Hierarchical; |
| | | 25 | | private ITimeSpanQuantizer? _quantizer; |
| | | 26 | | private Tense? _tense; |
| | 15 | 27 | | private ListFormattingOptions _listFormatting = ListFormattingOptionsPresets.CommaSeparated; |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Configures the builder from an existing preset or options instance. |
| | | 31 | | /// </summary> |
| | | 32 | | /// <param name="options">The source options.</param> |
| | | 33 | | /// <returns>The current builder.</returns> |
| | | 34 | | public TimeHumanizationBuilder From(TimeHumanizationOptions options) |
| | | 35 | | { |
| | 3 | 36 | | ArgumentNullException.ThrowIfNull(options); |
| | | 37 | | |
| | 3 | 38 | | _mode = options.Mode; |
| | 3 | 39 | | _minUnit = options.MinUnit; |
| | 3 | 40 | | _maxUnit = options.MaxUnit; |
| | 3 | 41 | | _maxComponents = options.MaxComponents; |
| | 3 | 42 | | _includeZeroUnits = options.IncludeZeroUnits; |
| | 3 | 43 | | _decompositionMode = options.DecompositionMode; |
| | 3 | 44 | | _quantizer = options.Quantizer; |
| | 3 | 45 | | _tense = options.Tense; |
| | 3 | 46 | | _listFormatting = options.ListFormatting; |
| | | 47 | | |
| | 3 | 48 | | return this; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Uses duration humanization. |
| | | 53 | | /// </summary> |
| | | 54 | | /// <returns>The current builder.</returns> |
| | | 55 | | public TimeHumanizationBuilder AsDuration() |
| | | 56 | | { |
| | 0 | 57 | | _mode = TimeHumanizationMode.Duration; |
| | 0 | 58 | | return this; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Uses relative-time humanization. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <returns>The current builder.</returns> |
| | | 65 | | public TimeHumanizationBuilder AsRelative() |
| | | 66 | | { |
| | 6 | 67 | | _mode = TimeHumanizationMode.Relative; |
| | 6 | 68 | | return this; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Uses the specified tense for relative humanization. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="tense">The tense to use.</param> |
| | | 75 | | /// <returns>The current builder.</returns> |
| | | 76 | | public TimeHumanizationBuilder WithTense(Tense tense) |
| | | 77 | | { |
| | 3 | 78 | | _tense = tense; |
| | 3 | 79 | | return this; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Uses the specified minimum unit. |
| | | 84 | | /// </summary> |
| | | 85 | | /// <param name="unit">The minimum unit.</param> |
| | | 86 | | /// <returns>The current builder.</returns> |
| | | 87 | | public TimeHumanizationBuilder MinUnit(TimeUnit unit) |
| | | 88 | | { |
| | 0 | 89 | | _minUnit = unit; |
| | 0 | 90 | | return this; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Uses the specified maximum unit. |
| | | 95 | | /// </summary> |
| | | 96 | | /// <param name="unit">The maximum unit.</param> |
| | | 97 | | /// <returns>The current builder.</returns> |
| | | 98 | | public TimeHumanizationBuilder MaxUnit(TimeUnit unit) |
| | | 99 | | { |
| | 0 | 100 | | _maxUnit = unit; |
| | 0 | 101 | | return this; |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// Uses the specified unit range. |
| | | 106 | | /// </summary> |
| | | 107 | | /// <param name="minUnit">The minimum unit.</param> |
| | | 108 | | /// <param name="maxUnit">The maximum unit.</param> |
| | | 109 | | /// <returns>The current builder.</returns> |
| | | 110 | | public TimeHumanizationBuilder UseUnits(TimeUnit minUnit, TimeUnit maxUnit) |
| | | 111 | | { |
| | 3 | 112 | | _minUnit = minUnit; |
| | 3 | 113 | | _maxUnit = maxUnit; |
| | | 114 | | |
| | 3 | 115 | | return this; |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// Limits the number of displayed components. |
| | | 120 | | /// </summary> |
| | | 121 | | /// <param name="count">The maximum number of components.</param> |
| | | 122 | | /// <returns>The current builder.</returns> |
| | | 123 | | public TimeHumanizationBuilder MaxComponents(int? count) |
| | | 124 | | { |
| | 3 | 125 | | _maxComponents = count; |
| | 3 | 126 | | return this; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | /// <summary> |
| | | 130 | | /// Includes zero-value units. |
| | | 131 | | /// </summary> |
| | | 132 | | /// <returns>The current builder.</returns> |
| | | 133 | | public TimeHumanizationBuilder IncludeZeroUnits() |
| | | 134 | | { |
| | 0 | 135 | | _includeZeroUnits = true; |
| | 0 | 136 | | return this; |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Excludes zero-value units. |
| | | 141 | | /// </summary> |
| | | 142 | | /// <returns>The current builder.</returns> |
| | | 143 | | public TimeHumanizationBuilder ExcludeZeroUnits() |
| | | 144 | | { |
| | 0 | 145 | | _includeZeroUnits = false; |
| | 0 | 146 | | return this; |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Uses hierarchical decomposition. |
| | | 151 | | /// </summary> |
| | | 152 | | /// <returns>The current builder.</returns> |
| | | 153 | | public TimeHumanizationBuilder Hierarchical() |
| | | 154 | | { |
| | 0 | 155 | | _decompositionMode = TimeSpanDecompositionMode.Hierarchical; |
| | 0 | 156 | | return this; |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | /// <summary> |
| | | 160 | | /// Uses largest-unit-only decomposition. |
| | | 161 | | /// </summary> |
| | | 162 | | /// <returns>The current builder.</returns> |
| | | 163 | | public TimeHumanizationBuilder LargestUnitOnly() |
| | | 164 | | { |
| | 0 | 165 | | _decompositionMode = TimeSpanDecompositionMode.LargestUnitOnly; |
| | 0 | 166 | | return this; |
| | | 167 | | } |
| | | 168 | | |
| | | 169 | | /// <summary> |
| | | 170 | | /// Uses smallest-unit-only decomposition. |
| | | 171 | | /// </summary> |
| | | 172 | | /// <returns>The current builder.</returns> |
| | | 173 | | public TimeHumanizationBuilder SmallestUnitOnly() |
| | | 174 | | { |
| | 0 | 175 | | _decompositionMode = TimeSpanDecompositionMode.SmallestUnitOnly; |
| | 0 | 176 | | return this; |
| | | 177 | | } |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// Uses fuzzy humanization. |
| | | 181 | | /// </summary> |
| | | 182 | | /// <returns>The current builder.</returns> |
| | | 183 | | public TimeHumanizationBuilder Fuzzy() |
| | | 184 | | { |
| | 6 | 185 | | _quantizer = HumanFriendlyTimeSpanQuantizer.Default; |
| | 6 | 186 | | return this; |
| | | 187 | | } |
| | | 188 | | |
| | | 189 | | /// <summary> |
| | | 190 | | /// Uses precise humanization. |
| | | 191 | | /// </summary> |
| | | 192 | | /// <returns>The current builder.</returns> |
| | | 193 | | public TimeHumanizationBuilder Precise() |
| | | 194 | | { |
| | 3 | 195 | | _quantizer = null; |
| | 3 | 196 | | return this; |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// Uses the specified quantizer. |
| | | 201 | | /// </summary> |
| | | 202 | | /// <param name="quantizer">The quantizer.</param> |
| | | 203 | | /// <returns>The current builder.</returns> |
| | | 204 | | public TimeHumanizationBuilder WithQuantizer(ITimeSpanQuantizer? quantizer) |
| | | 205 | | { |
| | 0 | 206 | | _quantizer = quantizer; |
| | 0 | 207 | | return this; |
| | | 208 | | } |
| | | 209 | | |
| | | 210 | | /// <summary> |
| | | 211 | | /// Uses the specified list formatting options. |
| | | 212 | | /// </summary> |
| | | 213 | | /// <param name="options">The list formatting options.</param> |
| | | 214 | | /// <returns>The current builder.</returns> |
| | | 215 | | public TimeHumanizationBuilder WithListFormatting(ListFormattingOptions options) |
| | | 216 | | { |
| | 0 | 217 | | _listFormatting = options; |
| | 0 | 218 | | return this; |
| | | 219 | | } |
| | | 220 | | |
| | | 221 | | /// <summary> |
| | | 222 | | /// Builds the configured <see cref="TimeHumanizationOptions"/>. |
| | | 223 | | /// </summary> |
| | | 224 | | /// <returns>The built options.</returns> |
| | 15 | 225 | | public TimeHumanizationOptions Build() => new() |
| | 15 | 226 | | { |
| | 15 | 227 | | Mode = _mode, |
| | 15 | 228 | | MinUnit = _minUnit, |
| | 15 | 229 | | MaxUnit = _maxUnit, |
| | 15 | 230 | | MaxComponents = _maxComponents, |
| | 15 | 231 | | IncludeZeroUnits = _includeZeroUnits, |
| | 15 | 232 | | DecompositionMode = _decompositionMode, |
| | 15 | 233 | | Quantizer = _quantizer, |
| | 15 | 234 | | Tense = _tense, |
| | 15 | 235 | | ListFormatting = _listFormatting |
| | 15 | 236 | | }; |
| | | 237 | | } |
| | | 238 | | |