| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | #nullable disable |
| | 5 | |
|
| | 6 | | using System.Collections.Generic; |
| | 7 | | using System.Linq; |
| | 8 | |
|
| | 9 | | namespace Azure.Core |
| | 10 | | { |
| | 11 | | internal static class Optional |
| | 12 | | { |
| | 13 | | public static bool IsCollectionDefined<T>(IEnumerable<T> collection) |
| | 14 | | { |
| | 15 | | return !(collection is ChangeTrackingList<T> changeTrackingList && changeTrackingList.IsUndefined); |
| | 16 | | } |
| | 17 | |
|
| | 18 | | public static bool IsCollectionDefined<TKey, TValue>(IReadOnlyDictionary<TKey, TValue> collection) |
| | 19 | | { |
| | 20 | | return !(collection is ChangeTrackingDictionary<TKey, TValue> changeTrackingList && changeTrackingList.IsUnd |
| | 21 | | } |
| | 22 | |
|
| | 23 | | public static bool IsCollectionDefined<TKey, TValue>(IDictionary<TKey, TValue> collection) |
| | 24 | | { |
| | 25 | | return !(collection is ChangeTrackingDictionary<TKey, TValue> changeTrackingList && changeTrackingList.IsUnd |
| | 26 | | } |
| | 27 | |
|
| | 28 | | public static bool IsDefined<T>(T? value) where T: struct |
| | 29 | | { |
| | 30 | | return value.HasValue; |
| | 31 | | } |
| | 32 | | public static bool IsDefined(object value) |
| | 33 | | { |
| | 34 | | return value != null; |
| | 35 | | } |
| | 36 | | public static bool IsDefined(string value) |
| | 37 | | { |
| | 38 | | return value != null; |
| | 39 | | } |
| | 40 | |
|
| | 41 | | public static IReadOnlyDictionary<TKey, TValue> ToDictionary<TKey, TValue>(Optional<IReadOnlyDictionary<TKey, TV |
| | 42 | | { |
| | 43 | | if (optional.HasValue) |
| | 44 | | { |
| | 45 | | return optional.Value; |
| | 46 | | } |
| | 47 | | return new ChangeTrackingDictionary<TKey, TValue>(optional); |
| | 48 | | } |
| | 49 | |
|
| | 50 | | public static IDictionary<TKey, TValue> ToDictionary<TKey, TValue>(Optional<IDictionary<TKey, TValue>> optional) |
| | 51 | | { |
| | 52 | | if (optional.HasValue) |
| | 53 | | { |
| | 54 | | return optional.Value; |
| | 55 | | } |
| | 56 | | return new ChangeTrackingDictionary<TKey, TValue>(optional); |
| | 57 | | } |
| | 58 | | public static IReadOnlyList<T> ToList<T>(Optional<IReadOnlyList<T>> optional) |
| | 59 | | { |
| | 60 | | if (optional.HasValue) |
| | 61 | | { |
| | 62 | | return optional.Value; |
| | 63 | | } |
| | 64 | | return new ChangeTrackingList<T>(optional); |
| | 65 | | } |
| | 66 | |
|
| | 67 | | public static IList<T> ToList<T>(Optional<IList<T>> optional) |
| | 68 | | { |
| | 69 | | if (optional.HasValue) |
| | 70 | | { |
| | 71 | | return optional.Value; |
| | 72 | | } |
| | 73 | | return new ChangeTrackingList<T>(optional); |
| | 74 | | } |
| | 75 | |
|
| | 76 | | public static T? ToNullable<T>(Optional<T> optional) where T: struct |
| | 77 | | { |
| | 78 | | if (optional.HasValue) |
| | 79 | | { |
| | 80 | | return optional.Value; |
| | 81 | | } |
| | 82 | | return default; |
| | 83 | | } |
| | 84 | |
|
| | 85 | | public static T? ToNullable<T>(Optional<T?> optional) where T: struct |
| | 86 | | { |
| | 87 | | return optional.Value; |
| | 88 | | } |
| | 89 | | } |
| | 90 | |
|
| | 91 | | internal readonly partial struct Optional<T> |
| | 92 | | { |
| 3414 | 93 | | public Optional(T value) : this() |
| | 94 | | { |
| 3414 | 95 | | Value = value; |
| 3414 | 96 | | HasValue = true; |
| 3414 | 97 | | } |
| | 98 | |
|
| 3454 | 99 | | public T Value { get; } |
| 1286 | 100 | | public bool HasValue { get; } |
| | 101 | |
|
| 3414 | 102 | | public static implicit operator Optional<T>(T value) => new Optional<T>(value); |
| 0 | 103 | | public static implicit operator T(Optional<T> optional) => optional.Value; |
| | 104 | | } |
| | 105 | | } |