-
Notifications
You must be signed in to change notification settings - Fork 51
/
ResourceNameList.cs
104 lines (87 loc) · 3.84 KB
/
ResourceNameList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright 2016 Google Inc. All Rights Reserved.
* Use of this source code is governed by a BSD-style
* license that can be found in the LICENSE file or at
* https://developers.google.com/open-source/licenses/bsd
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Google.Api.Gax
{
/// <summary>
/// A list of resource names of a specific type, that delegates all operations to an
/// underlying list of string-based resource names.
/// </summary>
/// <typeparam name="TName">The type of the resource name contained in this list.</typeparam>
public sealed class ResourceNameList<TName> : IList<TName> where TName : class, IResourceName
{
private readonly IList<String> _underlyingList;
private readonly Func<TName, string> _nameToString;
private readonly Func<string, TName> _stringToName;
/// <summary>
/// Constructs a <see cref="ResourceNameList{TName}"/> from an underlying string-based list
/// and a resource name parser.
/// </summary>
/// <param name="underlyingList"></param>
/// <param name="stringToName"></param>
public ResourceNameList(IList<string> underlyingList, Func<string, TName> stringToName)
{
_underlyingList = underlyingList;
_nameToString = name => GaxPreconditions.CheckNotNull(name, nameof(name)).ToString();
_stringToName = str => stringToName(GaxPreconditions.CheckNotNull(str, nameof(str)));
}
/// <inheritdoc />
public int Count => _underlyingList.Count;
/// <inheritdoc />
public bool IsReadOnly => _underlyingList.IsReadOnly;
/// <inheritdoc />
public TName this[int index]
{
get { return _stringToName(_underlyingList[index]); }
set { _underlyingList[index] = _nameToString(value); }
}
/// <inheritdoc />
public void Add(TName item) => _underlyingList.Add(_nameToString(item));
/// <summary>
/// Adds all items to this list.
/// </summary>
/// <param name="items">The items to add to this list.</param>
public void Add(IEnumerable<TName> items)
{
GaxPreconditions.CheckNotNull(items, nameof(items));
foreach (TName item in items)
{
Add(item);
}
}
/// <inheritdoc />
public void Clear() => _underlyingList.Clear();
/// <inheritdoc />
public bool Contains(TName item) => _underlyingList.Contains(_nameToString(item));
/// <inheritdoc />
public void CopyTo(TName[] array, int arrayIndex)
{
if (array == null) throw new ArgumentNullException(nameof(array));
if (arrayIndex < 0) throw new ArgumentOutOfRangeException(nameof(arrayIndex), "Must be >= 0");
if (Count > array.Length - arrayIndex) throw new ArgumentException("Copy operation must not exceed the array space available.");
for (int i = 0; i < Count; i++)
{
array[arrayIndex + i] = _stringToName(_underlyingList[i]);
}
}
/// <inheritdoc />
public IEnumerator<TName> GetEnumerator() => _underlyingList.Select(_stringToName).GetEnumerator();
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <inheritdoc />
public int IndexOf(TName item) => _underlyingList.IndexOf(_nameToString(item));
/// <inheritdoc />
public void Insert(int index, TName item) => _underlyingList.Insert(index, _nameToString(item));
/// <inheritdoc />
public bool Remove(TName item) => _underlyingList.Remove(_nameToString(item));
/// <inheritdoc />
public void RemoveAt(int index) => _underlyingList.RemoveAt(index);
}
}