| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | |
| | | 7 | | namespace System.Text.Json.Serialization.Metadata |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// Defines builder type for constructing updated <see cref="PropertyRef"/> caches. |
| | | 11 | | /// </summary> |
| | 0 | 12 | | internal sealed class PropertyRefCacheBuilder(PropertyRef[] originalCache) |
| | | 13 | | { |
| | | 14 | | public const int MaxCapacity = 64; |
| | 0 | 15 | | private readonly List<PropertyRef> _propertyRefs = []; |
| | 0 | 16 | | private readonly HashSet<PropertyRef> _added = []; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Stores a reference to the original cache off which the current list is being built. |
| | | 20 | | /// </summary> |
| | 0 | 21 | | public readonly PropertyRef[] OriginalCache = originalCache; |
| | 0 | 22 | | public int Count => _propertyRefs.Count; |
| | 0 | 23 | | public int TotalCount => OriginalCache.Length + _propertyRefs.Count; |
| | 0 | 24 | | public PropertyRef[] ToArray() => [.. OriginalCache, .. _propertyRefs]; |
| | | 25 | | |
| | | 26 | | public void TryAdd(PropertyRef propertyRef) |
| | 0 | 27 | | { |
| | 0 | 28 | | Debug.Assert(TotalCount < MaxCapacity, "Should have been checked by the caller."); |
| | | 29 | | |
| | 0 | 30 | | if (_added.Add(propertyRef)) |
| | 0 | 31 | | { |
| | 0 | 32 | | _propertyRefs.Add(propertyRef); |
| | 0 | 33 | | } |
| | 0 | 34 | | } |
| | | 35 | | } |
| | | 36 | | } |