< Summary

Class:Azure.Core.Optional`1
Assembly:Azure.AI.TextAnalytics
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\AutoRest\Optional.cs
Covered lines:7
Uncovered lines:1
Coverable lines:8
Total lines:105
Line coverage:87.5% (7 of 8)
Covered branches:0
Total branches:0

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
get_Value()-100%100%
get_HasValue()-100%100%
op_Implicit(...)-100%100%
op_Implicit(...)-0%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\AutoRest\Optional.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4#nullable disable
 5
 6using System.Collections.Generic;
 7using System.Linq;
 8
 9namespace 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    {
 4093        public Optional(T value) : this()
 94        {
 4095            Value = value;
 4096            HasValue = true;
 4097        }
 98
 12099        public T Value { get; }
 100100        public bool HasValue { get; }
 101
 40102        public static implicit operator Optional<T>(T value) => new Optional<T>(value);
 0103        public static implicit operator T(Optional<T> optional) => optional.Value;
 104    }
 105}