forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckedEnumerable.cs
More file actions
157 lines (138 loc) · 4.24 KB
/
CheckedEnumerable.cs
File metadata and controls
157 lines (138 loc) · 4.24 KB
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
using System;
using System.Collections.Generic;
namespace Simple.Data
{
using System.Collections;
using System.Linq;
using System.Threading;
public static class CheckedEnumerable
{
public static CheckedEnumerable<T> Create<T>(IEnumerable<T> source)
{
return new CheckedEnumerable<T>(source);
}
}
public class CheckedEnumerable<T> : IEnumerable<T>
{
private readonly object _sync = new object();
private readonly IEnumerable<T> _source;
private IEnumerator<T> _sourceEnumerator;
private Enumerator _enumerator;
private T _first;
public T Single
{
get
{
Check();
if (_count != 1) throw new InvalidOperationException("Enumerable does not contain exactly one element.");
return _first;
}
}
private T _second;
private int _count = -1;
private int _used;
public CheckedEnumerable(IEnumerable<T> source)
{
_source = source;
}
public IEnumerator<T> GetEnumerator()
{
if (Interlocked.CompareExchange(ref _used, 1, 0) == 1)
{
throw new InvalidOperationException("CheckedEnumerable may only be enumerated once.");
}
Check();
return _count == 0
? Enumerable.Empty<T>().GetEnumerator()
: new Enumerator(_count, _first, _second, _sourceEnumerator);
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public bool IsEmpty
{
get
{
Check();
return _count == 0;
}
}
public bool HasMoreThanOneValue
{
get
{
Check();
return _count > 1;
}
}
private void Check()
{
if (_sourceEnumerator == null)
{
lock (_sync)
{
if (_sourceEnumerator != null) return;
_sourceEnumerator = _source.GetEnumerator();
if (!_sourceEnumerator.MoveNext())
{
_count = 0;
return;
}
_first = _sourceEnumerator.Current;
if (_sourceEnumerator.MoveNext())
{
_count = 2;
_second = _sourceEnumerator.Current;
return;
}
_count = 1;
}
}
}
private class Enumerator : IEnumerator<T>
{
private readonly IEnumerator<T> _wrapped;
private int _check;
private readonly T _first;
private readonly T _second;
public Enumerator(int check, T first, T second, IEnumerator<T> wrapped)
{
_check = check;
_first = first;
_second = second;
_wrapped = wrapped;
}
public void Dispose()
{
_wrapped.Dispose();
}
public bool MoveNext()
{
switch (_check--)
{
case 2:
Current = _first;
return true;
case 1:
Current = _second;
return true;
default:
bool moved = _wrapped.MoveNext();
if (moved) Current = _wrapped.Current;
return moved;
}
}
public void Reset()
{
_check = 0;
_wrapped.Reset();
}
public T Current { get; private set; }
object IEnumerator.Current
{
get { return Current; }
}
}
}
}