|
1 /* |
|
2 * Copyright (c) 2004-2005 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 using System; |
|
19 using System.IO; |
|
20 using System.Text; |
|
21 using System.Threading; |
|
22 using System.Collections.Generic; |
|
23 using SymbianUtils; |
|
24 using SymbianUtils.Range; |
|
25 using SymbianUtils.Collections; |
|
26 |
|
27 namespace SymbianSymbolLib.SourceManagement.Source |
|
28 { |
|
29 public class SymSourceCollection : DisposableObject, IEnumerable<SymSource> |
|
30 { |
|
31 #region Constructors |
|
32 public SymSourceCollection() |
|
33 { |
|
34 } |
|
35 |
|
36 public SymSourceCollection( SymSource aSource ) |
|
37 { |
|
38 Add( aSource ); |
|
39 } |
|
40 #endregion |
|
41 |
|
42 #region API |
|
43 public void Clear() |
|
44 { |
|
45 lock ( iSources ) |
|
46 { |
|
47 IList<SymSource> list = iSources.Values; |
|
48 int count = list.Count; |
|
49 for ( int i = count - 1; i >= 0; i-- ) |
|
50 { |
|
51 SymSource source = list[ i ]; |
|
52 Remove( source ); |
|
53 } |
|
54 } |
|
55 } |
|
56 |
|
57 public void Add( SymSource aSource ) |
|
58 { |
|
59 bool added = false; |
|
60 // |
|
61 lock ( iSources ) |
|
62 { |
|
63 if ( !Contains( aSource ) ) |
|
64 { |
|
65 string uri = aSource.URI; |
|
66 iSources.Add( uri, aSource ); |
|
67 added = true; |
|
68 } |
|
69 } |
|
70 // |
|
71 if ( added ) |
|
72 { |
|
73 OnAdded( aSource ); |
|
74 } |
|
75 } |
|
76 |
|
77 public void AddRange( IEnumerable<SymSource> aSources ) |
|
78 { |
|
79 foreach ( SymSource source in aSources ) |
|
80 { |
|
81 Add( source ); |
|
82 } |
|
83 } |
|
84 |
|
85 public bool Remove( SymSource aSource ) |
|
86 { |
|
87 bool ret = false; |
|
88 SymSource source = null; |
|
89 // |
|
90 lock( iSources ) |
|
91 { |
|
92 string uri = aSource.URI; |
|
93 // |
|
94 if ( iSources.TryGetValue( uri, out source ) ) |
|
95 { |
|
96 ret = iSources.Remove( uri ); |
|
97 } |
|
98 } |
|
99 |
|
100 // Notify outside of the lock |
|
101 if ( source != null ) |
|
102 { |
|
103 OnRemoved( source ); |
|
104 } |
|
105 // |
|
106 return ret; |
|
107 } |
|
108 |
|
109 public bool Contains( string aURI ) |
|
110 { |
|
111 lock ( iSources ) |
|
112 { |
|
113 return iSources.ContainsKey( aURI ); |
|
114 } |
|
115 } |
|
116 |
|
117 public bool Contains( SymSource aSource ) |
|
118 { |
|
119 return Contains( aSource.URI ); |
|
120 } |
|
121 #endregion |
|
122 |
|
123 #region Properties |
|
124 public int Count |
|
125 { |
|
126 get |
|
127 { |
|
128 lock ( iSources ) |
|
129 { |
|
130 return iSources.Count; |
|
131 } |
|
132 } |
|
133 } |
|
134 |
|
135 public bool IsEmpty |
|
136 { |
|
137 get { return Count == 0; } |
|
138 } |
|
139 |
|
140 public SymSource this[ int aIndex ] |
|
141 { |
|
142 get |
|
143 { |
|
144 lock ( iSources ) |
|
145 { |
|
146 string key = iSources.Keys[ aIndex ]; |
|
147 return iSources[ key ]; |
|
148 } |
|
149 } |
|
150 } |
|
151 #endregion |
|
152 |
|
153 #region Internal methods |
|
154 protected virtual void OnAdded( SymSource aSource ) |
|
155 { |
|
156 aSource.AddedToCollection( this ); |
|
157 } |
|
158 |
|
159 protected virtual void OnRemoved( SymSource aSource ) |
|
160 { |
|
161 aSource.RemovedFromCollection( this ); |
|
162 } |
|
163 #endregion |
|
164 |
|
165 #region From IEnumerable<SymSource> |
|
166 public IEnumerator<SymSource> GetEnumerator() |
|
167 { |
|
168 foreach ( KeyValuePair<string, SymSource> kvp in iSources ) |
|
169 { |
|
170 yield return kvp.Value; |
|
171 } |
|
172 } |
|
173 |
|
174 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
|
175 { |
|
176 foreach ( KeyValuePair<string, SymSource> kvp in iSources ) |
|
177 { |
|
178 yield return kvp.Value; |
|
179 } |
|
180 } |
|
181 #endregion |
|
182 |
|
183 #region From DisposableObject |
|
184 protected override void CleanupManagedResources() |
|
185 { |
|
186 try |
|
187 { |
|
188 base.CleanupManagedResources(); |
|
189 } |
|
190 finally |
|
191 { |
|
192 foreach ( KeyValuePair<string, SymSource> kvp in iSources ) |
|
193 { |
|
194 SymSource source = kvp.Value; |
|
195 source.Dispose(); |
|
196 } |
|
197 iSources.Clear(); |
|
198 } |
|
199 } |
|
200 #endregion |
|
201 |
|
202 #region Data members |
|
203 private SortedList<string, SymSource> iSources = new SortedList<string, SymSource>(); |
|
204 #endregion |
|
205 } |
|
206 } |