|
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 SymbianCodeLib.SourceManagement.Source |
|
28 { |
|
29 public class CodeSourceCollection : DisposableObject, IEnumerable<CodeSource> |
|
30 { |
|
31 #region Constructors |
|
32 public CodeSourceCollection() |
|
33 { |
|
34 } |
|
35 |
|
36 public CodeSourceCollection( CodeSource aSource ) |
|
37 { |
|
38 Add( aSource ); |
|
39 } |
|
40 #endregion |
|
41 |
|
42 #region API |
|
43 public void Clear() |
|
44 { |
|
45 lock ( iSources ) |
|
46 { |
|
47 IList<CodeSource> list = iSources.Values; |
|
48 int count = list.Count; |
|
49 for ( int i = count - 1; i >= 0; i-- ) |
|
50 { |
|
51 CodeSource source = list[ i ]; |
|
52 Remove( source ); |
|
53 } |
|
54 } |
|
55 } |
|
56 |
|
57 public bool Add( CodeSource aSource ) |
|
58 { |
|
59 bool added = false; |
|
60 // |
|
61 lock ( iSources ) |
|
62 { |
|
63 if ( Contains( aSource ) == false ) |
|
64 { |
|
65 string uri = aSource.URI; |
|
66 iSources.Add( uri, aSource ); |
|
67 added = true; |
|
68 } |
|
69 else |
|
70 { |
|
71 System.Diagnostics.Debug.WriteLine( "**** WARNING **** discarding duplicate source: " + aSource ); |
|
72 } |
|
73 } |
|
74 // |
|
75 if ( added ) |
|
76 { |
|
77 OnAdded( aSource ); |
|
78 } |
|
79 // |
|
80 return added; |
|
81 } |
|
82 |
|
83 public void AddRange( IEnumerable<CodeSource> aSources ) |
|
84 { |
|
85 foreach ( CodeSource source in aSources ) |
|
86 { |
|
87 Add( source ); |
|
88 } |
|
89 } |
|
90 |
|
91 public bool Remove( CodeSource aSource ) |
|
92 { |
|
93 bool ret = false; |
|
94 CodeSource source = null; |
|
95 // |
|
96 lock( iSources ) |
|
97 { |
|
98 string uri = aSource.URI; |
|
99 // |
|
100 if ( iSources.TryGetValue( uri, out source ) ) |
|
101 { |
|
102 ret = iSources.Remove( uri ); |
|
103 } |
|
104 } |
|
105 |
|
106 // Notify outside of the lock |
|
107 if ( source != null ) |
|
108 { |
|
109 OnRemoved( source ); |
|
110 } |
|
111 // |
|
112 return ret; |
|
113 } |
|
114 |
|
115 public bool Contains( string aURI ) |
|
116 { |
|
117 lock ( iSources ) |
|
118 { |
|
119 return iSources.ContainsKey( aURI ); |
|
120 } |
|
121 } |
|
122 |
|
123 public bool Contains( CodeSource aSource ) |
|
124 { |
|
125 return Contains( aSource.URI ); |
|
126 } |
|
127 #endregion |
|
128 |
|
129 #region Properties |
|
130 public int Count |
|
131 { |
|
132 get |
|
133 { |
|
134 lock ( iSources ) |
|
135 { |
|
136 return iSources.Count; |
|
137 } |
|
138 } |
|
139 } |
|
140 |
|
141 public bool IsEmpty |
|
142 { |
|
143 get { return Count == 0; } |
|
144 } |
|
145 |
|
146 public CodeSource this[ int aIndex ] |
|
147 { |
|
148 get |
|
149 { |
|
150 lock ( iSources ) |
|
151 { |
|
152 string key = iSources.Keys[ aIndex ]; |
|
153 return iSources[ key ]; |
|
154 } |
|
155 } |
|
156 } |
|
157 #endregion |
|
158 |
|
159 #region From IEnumerable<CodeSource> |
|
160 public IEnumerator<CodeSource> GetEnumerator() |
|
161 { |
|
162 foreach ( KeyValuePair<string, CodeSource> kvp in iSources ) |
|
163 { |
|
164 yield return kvp.Value; |
|
165 } |
|
166 } |
|
167 |
|
168 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
|
169 { |
|
170 foreach ( KeyValuePair<string, CodeSource> kvp in iSources ) |
|
171 { |
|
172 yield return kvp.Value; |
|
173 } |
|
174 } |
|
175 #endregion |
|
176 |
|
177 #region From DisposableObject |
|
178 protected override void CleanupManagedResources() |
|
179 { |
|
180 try |
|
181 { |
|
182 base.CleanupManagedResources(); |
|
183 } |
|
184 finally |
|
185 { |
|
186 foreach ( KeyValuePair<string, CodeSource> kvp in iSources ) |
|
187 { |
|
188 CodeSource source = kvp.Value; |
|
189 source.Dispose(); |
|
190 } |
|
191 |
|
192 Clear(); |
|
193 } |
|
194 } |
|
195 #endregion |
|
196 |
|
197 #region Internal methods |
|
198 protected virtual void OnAdded( CodeSource aSource ) |
|
199 { |
|
200 aSource.AddedToCollection( this ); |
|
201 } |
|
202 |
|
203 protected virtual void OnRemoved( CodeSource aSource ) |
|
204 { |
|
205 aSource.RemovedFromCollection( this ); |
|
206 } |
|
207 #endregion |
|
208 |
|
209 #region Data members |
|
210 private SortedList<string, CodeSource> iSources = new SortedList<string, CodeSource>(); |
|
211 #endregion |
|
212 } |
|
213 } |