| | | 1 | | namespace Intentum.Versioning; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Tracks policy versions for rollback and comparison. |
| | | 5 | | /// </summary> |
| | | 6 | | public sealed class PolicyVersionTracker |
| | | 7 | | { |
| | 9 | 8 | | private readonly List<IVersionedPolicy> _versions = []; |
| | 9 | 9 | | private int _currentIndex = -1; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Registers a versioned policy. Latest is used as current. |
| | | 13 | | /// </summary> |
| | | 14 | | public PolicyVersionTracker Add(IVersionedPolicy versionedPolicy) |
| | 12 | 15 | | { |
| | 12 | 16 | | _versions.Add(versionedPolicy ?? throw new ArgumentNullException(nameof(versionedPolicy))); |
| | 11 | 17 | | _currentIndex = _versions.Count - 1; |
| | 11 | 18 | | return this; |
| | 11 | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Gets the current (active) versioned policy. |
| | | 23 | | /// </summary> |
| | 8 | 24 | | public IVersionedPolicy? Current => _currentIndex >= 0 && _currentIndex < _versions.Count ? _versions[_currentIndex] |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets all registered versions (oldest to newest). |
| | | 28 | | /// </summary> |
| | 1 | 29 | | public IReadOnlyList<IVersionedPolicy> Versions => _versions; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Rolls back to the previous version. Returns true if rollback was performed. |
| | | 33 | | /// </summary> |
| | | 34 | | public bool Rollback() |
| | 3 | 35 | | { |
| | 4 | 36 | | if (_currentIndex <= 0) return false; |
| | 2 | 37 | | _currentIndex--; |
| | 2 | 38 | | return true; |
| | 3 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Rolls forward to the next version (after a rollback). Returns true if rollforward was performed. |
| | | 43 | | /// </summary> |
| | | 44 | | public bool Rollforward() |
| | 2 | 45 | | { |
| | 3 | 46 | | if (_currentIndex < 0 || _currentIndex >= _versions.Count - 1) return false; |
| | 1 | 47 | | _currentIndex++; |
| | 1 | 48 | | return true; |
| | 2 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Sets the current version by index (0 = oldest). |
| | | 53 | | /// </summary> |
| | | 54 | | public void SetCurrent(int index) |
| | 3 | 55 | | { |
| | 3 | 56 | | if (index < 0 || index >= _versions.Count) |
| | 2 | 57 | | throw new ArgumentOutOfRangeException(nameof(index)); |
| | 1 | 58 | | _currentIndex = index; |
| | 1 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Compares two version identifiers (e.g. semantic or string). |
| | | 63 | | /// </summary> |
| | | 64 | | public static int CompareVersions(string a, string b) |
| | 3 | 65 | | { |
| | 3 | 66 | | return string.Compare(a, b, StringComparison.OrdinalIgnoreCase); |
| | 3 | 67 | | } |
| | | 68 | | } |