/* Copyright 2010-present MongoDB Inc.
*
* Licensed 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.Collections;
using System.Collections.Generic;
namespace MongoDB.Bson.Serialization.Serializers
{
///
/// Represents a serializer for Queues.
///
public class QueueSerializer :
EnumerableSerializerBase,
IChildSerializerConfigurable,
IBsonArraySerializer
{
// constructors
///
/// Initializes a new instance of the class.
///
public QueueSerializer()
{
}
///
/// Initializes a new instance of the class.
///
/// The item serializer.
public QueueSerializer(IBsonSerializer itemSerializer)
: base(itemSerializer)
{
}
///
/// Initializes a new instance of the class.
///
///
public QueueSerializer(IBsonSerializerRegistry serializerRegistry)
: base(serializerRegistry)
{
}
// public methods
///
/// Returns a serializer that has been reconfigured with the specified item serializer.
///
/// The item serializer.
/// The reconfigured serializer.
public QueueSerializer WithItemSerializer(IBsonSerializer itemSerializer)
{
return new QueueSerializer(itemSerializer);
}
// protected methods
///
/// Adds the item.
///
/// The accumulator.
/// The item.
protected override void AddItem(object accumulator, object item)
{
((Queue)accumulator).Enqueue(item);
}
///
/// Creates the accumulator.
///
/// The accumulator.
protected override object CreateAccumulator()
{
return new Queue();
}
///
/// Enumerates the items.
///
/// The value.
/// The items.
protected override IEnumerable EnumerateItemsInSerializationOrder(Queue value)
{
return value;
}
///
/// Finalizes the result.
///
/// The instance.
/// The result.
protected override Queue FinalizeResult(object accumulator)
{
return (Queue)accumulator;
}
// explicit interface implementations
IBsonSerializer IChildSerializerConfigurable.ChildSerializer
{
get { return ItemSerializer; }
}
IBsonSerializer IChildSerializerConfigurable.WithChildSerializer(IBsonSerializer childSerializer)
{
return WithItemSerializer(childSerializer);
}
}
///
/// Represents a serializer for Queues.
///
/// The type of the elements.
public class QueueSerializer :
EnumerableSerializerBase, TItem>,
IChildSerializerConfigurable,
IBsonArraySerializer
{
// constructors
///
/// Initializes a new instance of the class.
///
public QueueSerializer()
{
}
///
/// Initializes a new instance of the class.
///
/// The item serializer.
public QueueSerializer(IBsonSerializer itemSerializer)
: base(itemSerializer)
{
}
///
/// Initializes a new instance of the class.
///
/// The serializer registry.
public QueueSerializer(IBsonSerializerRegistry serializerRegistry)
: base(serializerRegistry)
{
}
// public methods
///
/// Returns a serializer that has been reconfigured with the specified item serializer.
///
/// The item serializer.
/// The reconfigured serializer.
public QueueSerializer WithItemSerializer(IBsonSerializer itemSerializer)
{
return new QueueSerializer(itemSerializer);
}
// protected methods
///
/// Adds the item.
///
/// The accumulator.
/// The item.
protected override void AddItem(object accumulator, TItem item)
{
((Queue)accumulator).Enqueue(item);
}
///
/// Creates the accumulator.
///
/// The accumulator.
protected override object CreateAccumulator()
{
return new Queue();
}
///
/// Enumerates the items in serialization order.
///
/// The value.
/// The items.
protected override IEnumerable EnumerateItemsInSerializationOrder(Queue value)
{
return value;
}
///
/// Finalizes the result.
///
/// The accumulator.
/// The result.
protected override Queue FinalizeResult(object accumulator)
{
return (Queue)accumulator;
}
// explicit interface implementations
IBsonSerializer IChildSerializerConfigurable.ChildSerializer
{
get { return ItemSerializer; }
}
IBsonSerializer IChildSerializerConfigurable.WithChildSerializer(IBsonSerializer childSerializer)
{
return WithItemSerializer((IBsonSerializer)childSerializer);
}
}
}