/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
using System;
using System.Diagnostics;
namespace Lucene.Net.Support
{
/// A collection of which can be
/// looked up by instances of .
/// The type of the items contains in this
/// collection.
/// The type of the keys that can be used to look
/// up the items.
internal class GeneralKeyedCollection : System.Collections.ObjectModel.KeyedCollection
{
/// Creates a new instance of the
/// class.
/// The which will convert
/// instances of to
/// when the override of is called.
internal GeneralKeyedCollection(Converter converter)
: base()
{
// If the converter is null, throw an exception.
if (converter == null) throw new ArgumentNullException("converter");
// Store the converter.
this.converter = converter;
// That's all folks.
return;
}
/// The which will convert
/// instances of to
/// when the override of is called.
private readonly Converter converter;
/// Converts an item that is added to the collection to
/// a key.
/// The instance of
/// to convert into an instance of .
/// The instance of which is the
/// key for this item.
protected override TKey GetKeyForItem(TItem item)
{
// The converter is not null.
Debug.Assert(converter != null);
// Call the converter.
return converter(item);
}
/// Determines if a key for an item exists in this
/// collection.
/// The instance of
/// to see if it exists in this collection.
/// True if the key exists in the collection, false otherwise.
public bool ContainsKey(TKey key)
{
// Call the dictionary - it is lazily created when the first item is added
if (Dictionary != null)
{
return Dictionary.ContainsKey(key);
}
else
{
return false;
}
}
public System.Collections.Generic.IList Values()
{
return base.Items;
}
}
}