c095b36003aa2c166801442023a2844e273aca7f
[mono.git] / mcs / class / referencesource / System / services / monitoring / system / diagnosticts / EventLogEntryCollection.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="EventLogEntryCollection.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 //------------------------------------------------------------------------------
6
7 namespace System.Diagnostics {
8     using System.Text;
9     using System;
10     using System.Collections;
11    
12     //Consider, V2, Microsoft: Is there a way to implement Contains
13     //and IndexOf, can we live withouth this part of the ReadOnly
14     //collection pattern?
15     /// <devdoc>
16     ///    <para>[To be supplied.]</para>
17     /// </devdoc>
18     public class EventLogEntryCollection : ICollection {
19         private EventLogInternal log;
20
21         internal EventLogEntryCollection(EventLogInternal log) {
22             this.log = log;
23         }
24         
25         /// <devdoc>
26         ///    <para>
27         ///       Gets the number of entries in the event log
28         ///    </para>
29         /// </devdoc>
30         public int Count {
31             get {
32                 return log.EntryCount;
33             }
34         }
35
36         /// <devdoc>
37         ///    <para>
38         ///       Gets an entry in
39         ///       the event log, based on an index starting at 0.
40         ///    </para>
41         /// </devdoc>
42         public virtual EventLogEntry this[int index] {
43             get {
44                 return log.GetEntryAt(index);
45             }
46         }
47         
48         /// <devdoc>
49         ///    <para>[To be supplied.]</para>
50         /// </devdoc>
51         public void CopyTo(EventLogEntry[] entries, int index) {
52             ((ICollection)this).CopyTo((Array)entries, index);
53         }       
54                        
55         /// <devdoc>
56         /// </devdoc>
57         public IEnumerator GetEnumerator() {                
58             return new EntriesEnumerator(this);
59         } 
60
61         internal EventLogEntry GetEntryAtNoThrow(int index) {
62             return log.GetEntryAtNoThrow(index);
63         }
64         
65         /// <internalonly/>
66         bool ICollection.IsSynchronized {
67             get {
68                 return false;
69             }
70         }
71
72         /// <devdoc>        
73         ///    ICollection private interface implementation.        
74         /// </devdoc>
75         /// <internalonly/>
76         object ICollection.SyncRoot {
77             get {
78                 return this;
79             }
80         }
81     
82         /// <devdoc>        
83         ///    ICollection private interface implementation.        
84         /// </devdoc>
85         /// <internalonly/>
86         void ICollection.CopyTo(Array array, int index) {
87             EventLogEntry[] entries = log.GetAllEntries();
88             Array.Copy(entries, 0, array, index, entries.Length);                                                       
89         }                     
90     
91         /// <devdoc>
92         ///    <para>
93         ///       Holds an System.Diagnostics.EventLog.EventLogEntryCollection that
94         ///       consists of the entries in an event
95         ///       log.
96         ///    </para>
97         /// </devdoc>
98         private class EntriesEnumerator : IEnumerator {
99             private EventLogEntryCollection entries;
100             private int num = -1;
101             private EventLogEntry cachedEntry = null;
102
103             internal EntriesEnumerator(EventLogEntryCollection entries) {
104                 this.entries = entries;
105             }
106
107             /// <devdoc>
108             ///    <para>
109             ///       Gets the entry at the current position.
110             ///    </para>
111             /// </devdoc>
112             public object Current {
113                 get {
114                     if (cachedEntry == null)
115                         throw new InvalidOperationException(SR.GetString(SR.NoCurrentEntry));
116                         
117                     return cachedEntry;
118                 }
119             }
120
121             /// <devdoc>
122             ///    <para>
123             ///       Advances the enumerator to the next entry in the event log.
124             ///    </para>
125             /// </devdoc>
126             public bool MoveNext() {
127                 num++;
128                 cachedEntry = entries.GetEntryAtNoThrow(num);
129                 
130                 return cachedEntry != null;
131             }            
132             
133             /// <devdoc>
134             ///    <para>
135             ///       Resets the state of the enumeration.
136             ///    </para>
137             /// </devdoc>
138             public void Reset() {
139                 num = -1;
140             }
141         }
142     }
143 }