forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJoinedEnumerable.cs
289 lines (237 loc) · 6.4 KB
/
JoinedEnumerable.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
namespace NHibernate.Util
{
/// <summary>
/// Concatenates multiple objects implementing <see cref="IEnumerable"/> into one.
/// </summary>
public class JoinedEnumerable : IEnumerable
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(JoinedEnumerable));
private readonly IEnumerable[] _enumerables;
/// <summary>
/// Creates an IEnumerable object from multiple IEnumerables.
/// </summary>
/// <param name="enumerables">The IEnumerables to join together.</param>
public JoinedEnumerable(IEnumerable[] enumerables)
{
_enumerables = new IEnumerable[enumerables.Length];
Array.Copy(enumerables, _enumerables, enumerables.Length);
}
public JoinedEnumerable(IEnumerable<IEnumerable> enumerables)
: this(enumerables.ToArray())
{
}
public JoinedEnumerable(IEnumerable first, IEnumerable second)
: this(new[] { first, second })
{
}
#region System.Collections.IEnumerable Members
/// <summary></summary>
public IEnumerator GetEnumerator()
{
var enumerators = _enumerables.Select(enumerable => enumerable.GetEnumerator());
return new JoinedEnumerator(enumerators);
}
#endregion
#region Nested class JoinedEnumerator
private class JoinedEnumerator : IEnumerator, IDisposable
{
private readonly IEnumerator[] _enumerators;
private int _current;
public JoinedEnumerator(IEnumerable<IEnumerator> enumerators)
{
_enumerators = enumerators.ToArray();
_current = 0;
}
#region System.Collections.IEnumerator Members
public bool MoveNext()
{
for (; _current < _enumerators.Length; _current++)
{
if (_enumerators[_current].MoveNext())
{
return true;
}
else
{
// there are no items left to iterate over in the current
// enumerator so go ahead and dispose of it.
IDisposable disposable = _enumerators[_current] as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}
}
}
return false;
}
public void Reset()
{
foreach (var enumerator in _enumerators)
enumerator.Reset();
_current = 0;
}
public object Current
{
get { return _enumerators[_current].Current; }
}
#endregion
#region IDisposable Members
/// <summary>
/// A flag to indicate if <c>Dispose()</c> has been called.
/// </summary>
private bool _isAlreadyDisposed;
/// <summary>
/// Finalizer that ensures the object is correctly disposed of.
/// </summary>
~JoinedEnumerator()
{
Dispose(false);
}
/// <summary>
/// Takes care of freeing the managed and unmanaged resources that
/// this class is responsible for.
/// </summary>
public void Dispose()
{
log.Debug("running JoinedEnumerator.Dispose()");
Dispose(true);
}
/// <summary>
/// Takes care of freeing the managed and unmanaged resources that
/// this class is responsible for.
/// </summary>
/// <param name="isDisposing">Indicates if this JoinedEnumerable is being Disposed of or Finalized.</param>
/// <remarks>
/// The command is closed and the reader is disposed. This allows other ADO.NET
/// related actions to occur without needing to move all the way through the
/// EnumerableImpl.
/// </remarks>
private void Dispose(bool isDisposing)
{
if (_isAlreadyDisposed)
{
// don't dispose of multiple times.
return;
}
// free managed resources that are being managed by the JoinedEnumerable if we
// know this call came through Dispose()
if (isDisposing)
{
// dispose each IEnumerable that still needs to be disposed of
for (; _current < _enumerators.Length; _current++)
{
IDisposable currentDisposable = _enumerators[_current] as IDisposable;
if (currentDisposable != null)
{
currentDisposable.Dispose();
}
}
// nothing for Finalizer to do - so tell the GC to ignore it
GC.SuppressFinalize(this);
}
// free unmanaged resources here
_isAlreadyDisposed = true;
}
#endregion
}
#endregion
}
// Since 5.3
[Obsolete("This class has no more usages and will be removed in a future version")]
public class JoinedEnumerable<T> : IEnumerable<T>
{
private readonly IEnumerable<T>[] enumerables;
public JoinedEnumerable(IEnumerable<T>[] enumerables)
{
this.enumerables = enumerables;
}
public JoinedEnumerable(List<IEnumerable<T>> enumerables)
: this(enumerables.ToArray())
{
}
public JoinedEnumerable(IEnumerable<T> first, IEnumerable<T> second)
: this(new IEnumerable<T>[] { first, second })
{
}
#region IEnumerable<T> Members
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return new JoinedEnumerator(enumerables);
}
#endregion
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return ((IEnumerable<T>)this).GetEnumerator();
}
#endregion
private class JoinedEnumerator : IEnumerator<T>
{
private readonly IEnumerator<T>[] enumerators;
private int currentEnumIdx = 0;
private bool disposed;
public JoinedEnumerator(IEnumerable<T>[] enumerables)
{
enumerators = new IEnumerator<T>[enumerables.Length];
for (int i = 0; i < enumerables.Length; i++)
enumerators[i] = enumerables[i].GetEnumerator();
}
#region IEnumerator<T> Members
T IEnumerator<T>.Current
{
get { return enumerators[currentEnumIdx].Current; }
}
#endregion
#region IDisposable Members
public void Dispose()
{
Dispose(true);
}
private void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
for (; currentEnumIdx < enumerators.Length; currentEnumIdx++)
enumerators[currentEnumIdx].Dispose();
GC.SuppressFinalize(this);
}
disposed = true;
}
}
~JoinedEnumerator()
{
Dispose(false);
}
#endregion
#region IEnumerator Members
public bool MoveNext()
{
for (; currentEnumIdx < enumerators.Length; currentEnumIdx++)
{
if (enumerators[currentEnumIdx].MoveNext())
return true;
else
enumerators[currentEnumIdx].Dispose();
}
return false;
}
public void Reset()
{
foreach (IEnumerator<T> enumerator in enumerators)
enumerator.Reset();
currentEnumIdx = 0;
}
public object Current
{
get { return ((IEnumerator<T>)this).Current; }
}
#endregion
}
}
}