|
4 | 4 | // Created by: Alexey Kochetov
|
5 | 5 | // Created: 2007.09.24
|
6 | 6 |
|
7 |
| -using System; |
8 |
| -using System.Collections; |
9 |
| -using System.Collections.Generic; |
10 |
| -using System.Linq; |
11 | 7 | using Xtensive.Core;
|
12 | 8 |
|
| 9 | +namespace Xtensive.Orm.Rse; |
13 | 10 |
|
14 |
| -namespace Xtensive.Orm.Rse |
| 11 | +/// <summary> |
| 12 | +/// Collection of <see cref="Column"/> items. |
| 13 | +/// </summary> |
| 14 | +[Serializable] |
| 15 | +public readonly struct ColumnCollection |
15 | 16 | {
|
16 |
| - /// <summary> |
17 |
| - /// Collection of <see cref="Column"/> items. |
18 |
| - /// </summary> |
19 |
| - [Serializable] |
20 |
| - public sealed class ColumnCollection : IReadOnlyList<Column> |
21 |
| - { |
22 |
| - public struct Enumerator : IEnumerator<Column>, IEnumerator |
23 |
| - { |
24 |
| - private readonly IReadOnlyList<Column> list; |
25 |
| - private int index; |
26 |
| - public Column Current { get; private set; } |
27 |
| - |
28 |
| - object IEnumerator.Current => |
29 |
| - index == 0 || index == list.Count + 1 |
30 |
| - ? throw new InvalidOperationException("Enumeration cannot happen") |
31 |
| - : Current; |
32 |
| - |
33 |
| - public void Dispose() |
34 |
| - { |
35 |
| - } |
36 |
| - |
37 |
| - public bool MoveNext() |
38 |
| - { |
39 |
| - if (index < list.Count) { |
40 |
| - Current = list[index++]; |
41 |
| - return true; |
42 |
| - } |
43 |
| - index = list.Count + 1; |
44 |
| - Current = default; |
45 |
| - return false; |
46 |
| - } |
47 |
| - |
48 |
| - void IEnumerator.Reset() |
49 |
| - { |
50 |
| - index = 0; |
51 |
| - Current = default; |
52 |
| - } |
53 |
| - |
54 |
| - internal Enumerator(IReadOnlyList<Column> list) |
55 |
| - { |
56 |
| - this.list = list; |
57 |
| - index = 0; |
58 |
| - Current = default; |
59 |
| - } |
60 |
| - } |
| 17 | + private readonly Dictionary<string, int> nameIndex; |
61 | 18 |
|
62 |
| - private readonly Dictionary<string, int> nameIndex; |
63 |
| - private readonly IReadOnlyList<Column> columns; |
| 19 | + public IReadOnlyList<Column> Columns { get; } |
64 | 20 |
|
65 |
| - /// <summary> |
66 |
| - /// Gets the number of <see href="Column"/>s in the collection. |
67 |
| - /// </summary> |
68 |
| - public ColNum Count => (ColNum)columns.Count; |
69 |
| - |
70 |
| - int IReadOnlyCollection<Column>.Count => columns.Count; |
71 |
| - |
72 |
| - /// <summary> |
73 |
| - /// Gets a <see href="Column"/> instance by its index. |
74 |
| - /// </summary> |
75 |
| - public Column this[int index] => columns[index]; |
76 |
| - |
77 |
| - /// <summary> |
78 |
| - /// Gets <see cref="Column"/> by provided <paramref name="fullName"/>. |
79 |
| - /// </summary> |
80 |
| - /// <remarks> |
81 |
| - /// Returns <see cref="Column"/> if it was found; otherwise <see langword="null"/>. |
82 |
| - /// </remarks> |
83 |
| - /// <param name="fullName">Full name of the <see cref="Column"/> to find.</param> |
84 |
| - public Column this[string fullName] => |
85 |
| - nameIndex.TryGetValue(fullName, out var index) ? columns[index] : null; |
| 21 | + /// <summary> |
| 22 | + /// Gets the number of <see href="Column"/>s in the collection. |
| 23 | + /// </summary> |
| 24 | + public ColNum Count => (ColNum)Columns.Count; |
86 | 25 |
|
87 |
| - /// <summary> |
88 |
| - /// Determines whether the collecton contains specified column |
89 |
| - /// </summary> |
90 |
| - /// <param name="column"></param> |
91 |
| - /// <returns></returns> |
92 |
| - public bool Contains(Column column) |
93 |
| - { |
94 |
| - if (columns is ICollection<Column> colColumns) |
95 |
| - return colColumns.Contains(column); |
96 |
| - return columns.Contains(column); |
97 |
| - } |
98 | 26 |
|
99 |
| - /// <summary> |
100 |
| - /// Joins this collection with specified the column collection. |
101 |
| - /// </summary> |
102 |
| - /// <param name="joined">The joined.</param> |
103 |
| - /// <returns>The joined collection.</returns> |
104 |
| - public ColumnCollection Join(IEnumerable<Column> joined) |
105 |
| - { |
106 |
| - return new ColumnCollection(this.Concat(joined).ToList()); |
107 |
| - } |
| 27 | + /// <summary> |
| 28 | + /// Gets a <see href="Column"/> instance by its index. |
| 29 | + /// </summary> |
| 30 | + public Column this[int index] => Columns[index]; |
108 | 31 |
|
109 |
| - /// <summary> |
110 |
| - /// Aliases the specified <see cref="Column"/> collection. |
111 |
| - /// </summary> |
112 |
| - /// <param name="alias">The alias to add.</param> |
113 |
| - /// <returns>Aliased collection of columns.</returns> |
114 |
| - public ColumnCollection Alias(string alias) |
115 |
| - { |
116 |
| - ArgumentException.ThrowIfNullOrEmpty(alias); |
117 |
| - return new ColumnCollection(this.Select(column => column.Clone(alias + "." + column.Name)).ToArray(Count)); |
118 |
| - } |
| 32 | + /// <summary> |
| 33 | + /// Gets <see cref="Column"/> by provided <paramref name="fullName"/>. |
| 34 | + /// </summary> |
| 35 | + /// <remarks> |
| 36 | + /// Returns <see cref="Column"/> if it was found; otherwise <see langword="null"/>. |
| 37 | + /// </remarks> |
| 38 | + /// <param name="fullName">Full name of the <see cref="Column"/> to find.</param> |
| 39 | + public Column this[string fullName] => |
| 40 | + nameIndex.TryGetValue(fullName, out var index) ? Columns[index] : null; |
119 | 41 |
|
120 |
| - /// <summary> |
121 |
| - /// Returns an enumerator that iterates through the <see href="ColumnCollection"/>. |
122 |
| - /// </summary> |
123 |
| - public Enumerator GetEnumerator() => new Enumerator(this); |
| 42 | + /// <summary> |
| 43 | + /// Determines whether the collecton contains specified column |
| 44 | + /// </summary> |
| 45 | + /// <param name="column"></param> |
| 46 | + /// <returns></returns> |
| 47 | + public bool Contains(Column column) => |
| 48 | + Columns is ICollection<Column> colColumns ? colColumns.Contains(column) : Columns.Contains(column); |
124 | 49 |
|
125 |
| - /// <summary> |
126 |
| - /// Returns an enumerator that iterates through the <see href="ColumnCollection"/>. |
127 |
| - /// </summary> |
128 |
| - IEnumerator<Column> IEnumerable<Column>.GetEnumerator() => GetEnumerator(); |
| 50 | + /// <summary> |
| 51 | + /// Joins this collection with specified the column collection. |
| 52 | + /// </summary> |
| 53 | + /// <param name="joined">The joined.</param> |
| 54 | + /// <returns>The joined collection.</returns> |
| 55 | + public ColumnCollection Join(IEnumerable<Column> joined) |
| 56 | + { |
| 57 | + return new ColumnCollection(Columns.Concat(joined).ToList()); |
| 58 | + } |
129 | 59 |
|
130 |
| - /// <inheritdoc/> |
131 |
| - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| 60 | + /// <summary> |
| 61 | + /// Aliases the specified <see cref="Column"/> collection. |
| 62 | + /// </summary> |
| 63 | + /// <param name="alias">The alias to add.</param> |
| 64 | + /// <returns>Aliased collection of columns.</returns> |
| 65 | + public ColumnCollection Alias(string alias) |
| 66 | + { |
| 67 | + ArgumentException.ThrowIfNullOrEmpty(alias); |
| 68 | + return new ColumnCollection(Columns.Select(column => column.Clone(alias + "." + column.Name)).ToArray(Count)); |
| 69 | + } |
132 | 70 |
|
133 |
| - // Constructors |
| 71 | + // Constructors |
134 | 72 |
|
135 |
| - /// <summary> |
136 |
| - /// Initializes a new instance of this class. |
137 |
| - /// </summary> |
138 |
| - /// <param name="columns">Collection of items to add.</param> |
139 |
| - /// <remarks> |
140 |
| - /// <paramref name="columns"/> is used to initialize inner field directly |
141 |
| - /// to save time on avoiding collection copy. If you pass an <see cref="IReadOnlyList{Column}"/> |
142 |
| - /// implementor that, in fact, can be changed, make sure the passed collection doesn't change afterwards. |
143 |
| - /// Ideally, use arrays instead of <see cref="List{T}"/> or similar collections. |
144 |
| - /// Changing the passed collection afterwards will lead to unpredictable results. |
145 |
| - /// </remarks> |
146 |
| - public ColumnCollection(IReadOnlyList<Column> columns) |
147 |
| - { |
148 |
| - //!!! Direct initialization by parameter is unsafe performance optimization: See remarks in ctor summary! |
149 |
| - this.columns = columns; |
150 |
| - var count = this.columns.Count; |
151 |
| - nameIndex = new Dictionary<string, int>(count); |
152 |
| - for (var index = count; index-- > 0;) { |
153 |
| - nameIndex.Add(this.columns[index].Name, index); |
154 |
| - } |
| 73 | + /// <summary> |
| 74 | + /// Initializes a new instance of this class. |
| 75 | + /// </summary> |
| 76 | + /// <param name="columns">Collection of items to add.</param> |
| 77 | + /// <remarks> |
| 78 | + /// <paramref name="columns"/> is used to initialize inner field directly |
| 79 | + /// to save time on avoiding collection copy. If you pass an <see cref="IReadOnlyList{Column}"/> |
| 80 | + /// implementor that, in fact, can be changed, make sure the passed collection doesn't change afterwards. |
| 81 | + /// Ideally, use arrays instead of <see cref="List{T}"/> or similar collections. |
| 82 | + /// Changing the passed collection afterwards will lead to unpredictable results. |
| 83 | + /// </remarks> |
| 84 | + public ColumnCollection(IReadOnlyList<Column> columns) |
| 85 | + { |
| 86 | + //!!! Direct initialization by parameter is unsafe performance optimization: See remarks in ctor summary! |
| 87 | + Columns = columns; |
| 88 | + var count = Columns.Count; |
| 89 | + nameIndex = new Dictionary<string, int>(count); |
| 90 | + for (var index = count; index-- > 0;) { |
| 91 | + nameIndex.Add(Columns[index].Name, index); |
155 | 92 | }
|
156 | 93 | }
|
157 | 94 | }
|
0 commit comments