Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
CollectionsHelper.cs
Go to the documentation of this file.
1 /*
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements. See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership. The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License. You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied. See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20 */
21 
22 using System;
23 using System.Collections;
24 
25 namespace Lucene.Net.Support
26 {
27  /// <summary>
28  /// Support class used to handle Hashtable addition, which does a check
29  /// first to make sure the added item is unique in the hash.
30  /// </summary>
31  public class CollectionsHelper
32  {
33  public static void Add(System.Collections.Hashtable hashtable, System.Object item)
34  {
35  hashtable.Add(item, item);
36  }
37 
38  public static void AddIfNotContains(System.Collections.Hashtable hashtable, System.Object item)
39  {
40  // Added lock around check. Even though the collection should already have
41  // a synchronized wrapper around it, it doesn't prevent this test from having
42  // race conditions. Two threads can (and have in TestIndexReaderReopen) call
43  // hashtable.Contains(item) == false at the same time, then both try to add to
44  // the hashtable, causing an ArgumentException. locking on the collection
45  // prevents this. -- cc
46  lock (hashtable)
47  {
48  if (hashtable.Contains(item) == false)
49  {
50  hashtable.Add(item, item);
51  }
52  }
53  }
54 
55  public static void AddIfNotContains(System.Collections.ArrayList hashtable, System.Object item)
56  {
57  // see AddIfNotContains(Hashtable, object) for information about the lock
58  lock (hashtable)
59  {
60  if (hashtable.Contains(item) == false)
61  {
62  hashtable.Add(item);
63  }
64  }
65  }
66 
67  public static void AddAll(System.Collections.Hashtable hashtable, System.Collections.ICollection items)
68  {
69  System.Collections.IEnumerator iter = items.GetEnumerator();
70  System.Object item;
71  while (iter.MoveNext())
72  {
73  item = iter.Current;
74  hashtable.Add(item, item);
75  }
76  }
77 
78  public static void AddAllIfNotContains(System.Collections.Hashtable hashtable, System.Collections.IList items)
79  {
80  System.Object item;
81  for (int i = 0; i < items.Count; i++)
82  {
83  item = items[i];
84  if (hashtable.Contains(item) == false)
85  {
86  hashtable.Add(item, item);
87  }
88  }
89  }
90 
91  public static void AddAllIfNotContains(System.Collections.Hashtable hashtable, System.Collections.ICollection items)
92  {
93  System.Collections.IEnumerator iter = items.GetEnumerator();
94  System.Object item;
95  while (iter.MoveNext())
96  {
97  item = iter.Current;
98  if (hashtable.Contains(item) == false)
99  {
100  hashtable.Add(item, item);
101  }
102  }
103  }
104 
105  public static void AddAllIfNotContains(System.Collections.Generic.IDictionary<string, string> hashtable, System.Collections.Generic.ICollection<string> items)
106  {
107  foreach (string s in items)
108  {
109  if (hashtable.ContainsKey(s) == false)
110  {
111  hashtable.Add(s, s);
112  }
113  }
114  }
115 
116  public static void AddAll(System.Collections.Generic.IDictionary<string, string> hashtable, System.Collections.Generic.ICollection<string> items)
117  {
118  foreach (string s in items)
119  {
120  hashtable.Add(s, s);
121  }
122  }
123 
124  public static bool Contains(System.Collections.Generic.ICollection<string> col, string item)
125  {
126  foreach (string s in col) if (s == item) return true;
127  return false;
128  }
129 
130  public static bool Contains(System.Collections.ICollection col, System.Object item)
131  {
132  System.Collections.IEnumerator iter = col.GetEnumerator();
133  while (iter.MoveNext())
134  {
135  if (iter.Current.Equals(item))
136  return true;
137  }
138  return false;
139  }
140 
141 
142  public static System.String CollectionToString(System.Collections.Generic.IDictionary<string, string> c)
143  {
144  Hashtable t = new Hashtable();
145  foreach (string key in c.Keys)
146  {
147  t.Add(key, c[key]);
148  }
149  return CollectionToString(t);
150  }
151 
152  /// <summary>
153  /// Converts the specified collection to its string representation.
154  /// </summary>
155  /// <param name="c">The collection to convert to string.</param>
156  /// <returns>A string representation of the specified collection.</returns>
157  public static System.String CollectionToString(System.Collections.ICollection c)
158  {
159  System.Text.StringBuilder s = new System.Text.StringBuilder();
160 
161  if (c != null)
162  {
163 
164  System.Collections.ArrayList l = new System.Collections.ArrayList(c);
165 
166  bool isDictionary = (c is System.Collections.BitArray || c is System.Collections.Hashtable || c is System.Collections.IDictionary || c is System.Collections.Specialized.NameValueCollection || (l.Count > 0 && l[0] is System.Collections.DictionaryEntry));
167  for (int index = 0; index < l.Count; index++)
168  {
169  if (l[index] == null)
170  s.Append("null");
171  else if (!isDictionary)
172  s.Append(l[index]);
173  else
174  {
175  isDictionary = true;
176  if (c is System.Collections.Specialized.NameValueCollection)
177  s.Append(((System.Collections.Specialized.NameValueCollection)c).GetKey(index));
178  else
179  s.Append(((System.Collections.DictionaryEntry)l[index]).Key);
180  s.Append("=");
181  if (c is System.Collections.Specialized.NameValueCollection)
182  s.Append(((System.Collections.Specialized.NameValueCollection)c).GetValues(index)[0]);
183  else
184  s.Append(((System.Collections.DictionaryEntry)l[index]).Value);
185 
186  }
187  if (index < l.Count - 1)
188  s.Append(", ");
189  }
190 
191  if (isDictionary)
192  {
193  if (c is System.Collections.ArrayList)
194  isDictionary = false;
195  }
196  if (isDictionary)
197  {
198  s.Insert(0, "{");
199  s.Append("}");
200  }
201  else
202  {
203  s.Insert(0, "[");
204  s.Append("]");
205  }
206  }
207  else
208  s.Insert(0, "null");
209  return s.ToString();
210  }
211 
212  /// <summary>
213  /// Compares two string arrays for equality.
214  /// </summary>
215  /// <param name="l1">First string array list to compare</param>
216  /// <param name="l2">Second string array list to compare</param>
217  /// <returns>true if the strings are equal in both arrays, false otherwise</returns>
218  public static bool CompareStringArrays(System.String[] l1, System.String[] l2)
219  {
220  if (l1.Length != l2.Length)
221  return false;
222  for (int i = 0; i < l1.Length; i++)
223  {
224  if (l1[i] != l2[i])
225  return false;
226  }
227  return true;
228  }
229 
230  /// <summary>
231  /// Sorts an IList collections
232  /// </summary>
233  /// <param name="list">The System.Collections.IList instance that will be sorted</param>
234  /// <param name="Comparator">The Comparator criteria, null to use natural comparator.</param>
235  public static void Sort(System.Collections.IList list, System.Collections.IComparer Comparator)
236  {
237  if (((System.Collections.ArrayList)list).IsReadOnly)
238  throw new System.NotSupportedException();
239 
240  if ((Comparator == null) || (Comparator is System.Collections.Comparer))
241  {
242  try
243  {
244  ((System.Collections.ArrayList)list).Sort();
245  }
246  catch (System.InvalidOperationException e)
247  {
248  throw new System.InvalidCastException(e.Message);
249  }
250  }
251  else
252  {
253  try
254  {
255  ((System.Collections.ArrayList)list).Sort(Comparator);
256  }
257  catch (System.InvalidOperationException e)
258  {
259  throw new System.InvalidCastException(e.Message);
260  }
261  }
262  }
263 
264  /// <summary>
265  /// Fills the array with an specific value from an specific index to an specific index.
266  /// </summary>
267  /// <param name="array">The array to be filled.</param>
268  /// <param name="fromindex">The first index to be filled.</param>
269  /// <param name="toindex">The last index to be filled.</param>
270  /// <param name="val">The value to fill the array with.</param>
271  public static void Fill(System.Array array, System.Int32 fromindex, System.Int32 toindex, System.Object val)
272  {
273  System.Object Temp_Object = val;
274  System.Type elementtype = array.GetType().GetElementType();
275  if (elementtype != val.GetType())
276  Temp_Object = Convert.ChangeType(val, elementtype);
277  if (array.Length == 0)
278  throw (new System.NullReferenceException());
279  if (fromindex > toindex)
280  throw (new System.ArgumentException());
281  if ((fromindex < 0) || ((System.Array)array).Length < toindex)
282  throw (new System.IndexOutOfRangeException());
283  for (int index = (fromindex > 0) ? fromindex-- : fromindex; index < toindex; index++)
284  array.SetValue(Temp_Object, index);
285  }
286 
287 
288  /// <summary>
289  /// Fills the array with an specific value.
290  /// </summary>
291  /// <param name="array">The array to be filled.</param>
292  /// <param name="val">The value to fill the array with.</param>
293  public static void Fill(System.Array array, System.Object val)
294  {
295  Fill(array, 0, array.Length, val);
296  }
297 
298  /// <summary>
299  /// Compares the entire members of one array whith the other one.
300  /// </summary>
301  /// <param name="array1">The array to be compared.</param>
302  /// <param name="array2">The array to be compared with.</param>
303  /// <returns>Returns true if the two specified arrays of Objects are equal
304  /// to one another. The two arrays are considered equal if both arrays
305  /// contain the same number of elements, and all corresponding pairs of
306  /// elements in the two arrays are equal. Two objects e1 and e2 are
307  /// considered equal if (e1==null ? e2==null : e1.equals(e2)). In other
308  /// words, the two arrays are equal if they contain the same elements in
309  /// the same order. Also, two array references are considered equal if
310  /// both are null.</returns>
311  public static bool Equals(System.Array array1, System.Array array2)
312  {
313  bool result = false;
314  if ((array1 == null) && (array2 == null))
315  result = true;
316  else if ((array1 != null) && (array2 != null))
317  {
318  if (array1.Length == array2.Length)
319  {
320  int length = array1.Length;
321  result = true;
322  for (int index = 0; index < length; index++)
323  {
324  System.Object o1 = array1.GetValue(index);
325  System.Object o2 = array2.GetValue(index);
326  if (o1 == null && o2 == null)
327  continue; // they match
328  else if (o1 == null || !o1.Equals(o2))
329  {
330  result = false;
331  break;
332  }
333  }
334  }
335  }
336  return result;
337  }
338  }
339 }