aboutsummaryrefslogtreecommitdiff
path: root/Emby.Common.Implementations/IO/SharpCifs/Util/Sharpen/EnumeratorWrapper.cs
blob: e6fbdd411ed16a8b7f507eba3c0c1aece49c22e5 (plain)
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
using System;
using System.Collections.Generic;

namespace SharpCifs.Util.Sharpen
{
    internal class EnumeratorWrapper<T> : Iterator<T>
    {
        object _collection;
        IEnumerator<T> _e;
        T _lastVal;
        bool _more;
        bool _copied;

        public EnumeratorWrapper(object collection, IEnumerator<T> e)
        {
            this._e = e;
            this._collection = collection;
            _more = e.MoveNext();
        }

        public override bool HasNext()
        {
            return _more;
        }

        public override T Next()
        {
            if (!_more)
                throw new NoSuchElementException();
            _lastVal = _e.Current;
            _more = _e.MoveNext();
            return _lastVal;
        }

        public override void Remove()
        {
            ICollection<T> col = _collection as ICollection<T>;
            if (col == null)
            {
                throw new NotSupportedException();
            }
            if (_more && !_copied)
            {
                // Read the remaining elements, since the current enumerator
                // will be invalid after removing the element
                List<T> remaining = new List<T>();
                do
                {
                    remaining.Add(_e.Current);
                } while (_e.MoveNext());
                _e = remaining.GetEnumerator();
                _e.MoveNext();
                _copied = true;
            }
            col.Remove(_lastVal);
        }
    }
}