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
GeneralKeyedCollection.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.Diagnostics;
24 
25 namespace Lucene.Net.Support
26 {
27  /// <summary>A collection of <typeparamref name="TItem"/> which can be
28  /// looked up by instances of <typeparamref name="TKey"/>.</summary>
29  /// <typeparam name="TItem">The type of the items contains in this
30  /// collection.</typeparam>
31  /// <typeparam name="TKey">The type of the keys that can be used to look
32  /// up the items.</typeparam>
33  internal class GeneralKeyedCollection<TKey, TItem> : System.Collections.ObjectModel.KeyedCollection<TKey, TItem>
34  {
35  /// <summary>Creates a new instance of the
36  /// <see cref="GeneralKeyedCollection{TKey, TItem}"/> class.</summary>
37  /// <param name="converter">The <see cref="Converter{TInput, TOutput}"/> which will convert
38  /// instances of <typeparamref name="TItem"/> to <typeparamref name="TKey"/>
39  /// when the override of <see cref="GetKeyForItem(TItem)"/> is called.</param>
40  internal GeneralKeyedCollection(Converter<TItem, TKey> converter)
41  : base()
42  {
43  // If the converter is null, throw an exception.
44  if (converter == null) throw new ArgumentNullException("converter");
45 
46  // Store the converter.
47  this.converter = converter;
48 
49  // That's all folks.
50  return;
51  }
52 
53  /// <summary>The <see cref="Converter{TInput, TOutput}"/> which will convert
54  /// instances of <typeparamref name="TItem"/> to <typeparamref name="TKey"/>
55  /// when the override of <see cref="GetKeyForItem(TItem)"/> is called.</summary>
56  private readonly Converter<TItem, TKey> converter;
57 
58  /// <summary>Converts an item that is added to the collection to
59  /// a key.</summary>
60  /// <param name="item">The instance of <typeparamref name="TItem"/>
61  /// to convert into an instance of <typeparamref name="TKey"/>.</param>
62  /// <returns>The instance of <typeparamref name="TKey"/> which is the
63  /// key for this item.</returns>
64  protected override TKey GetKeyForItem(TItem item)
65  {
66  // The converter is not null.
67  Debug.Assert(converter != null);
68 
69  // Call the converter.
70  return converter(item);
71  }
72 
73  /// <summary>Determines if a key for an item exists in this
74  /// collection.</summary>
75  /// <param name="key">The instance of <typeparamref name="TKey"/>
76  /// to see if it exists in this collection.</param>
77  /// <returns>True if the key exists in the collection, false otherwise.</returns>
78  public bool ContainsKey(TKey key)
79  {
80  // Call the dictionary - it is lazily created when the first item is added
81  if (Dictionary != null)
82  {
83  return Dictionary.ContainsKey(key);
84  }
85  else
86  {
87  return false;
88  }
89  }
90 
91  public System.Collections.Generic.IList<TItem> Values()
92  {
93  return base.Items;
94  }
95  }
96 }