StackSerializer.cs 7.2 KB
Newer Older
T
tanghai 已提交
1
/* Copyright 2010-2015 MongoDB Inc.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*
* 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;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
T
tanghai 已提交
20
using MongoDB.Bson.Serialization.Conventions;
21 22 23 24 25 26 27
using MongoDB.Bson.Serialization.Options;

namespace MongoDB.Bson.Serialization.Serializers
{
    /// <summary>
    /// Represents a serializer for Stacks.
    /// </summary>
T
tanghai 已提交
28 29 30 31
    public class StackSerializer :
        EnumerableSerializerBase<Stack>,
        IChildSerializerConfigurable,
        IBsonArraySerializer
32 33 34
    {
        // constructors
        /// <summary>
T
tanghai 已提交
35
        /// Initializes a new instance of the <see cref="StackSerializer"/> class.
36 37 38 39 40 41
        /// </summary>
        public StackSerializer()
        {
        }

        /// <summary>
T
tanghai 已提交
42
        /// Initializes a new instance of the <see cref="StackSerializer"/> class.
43
        /// </summary>
T
tanghai 已提交
44 45 46
        /// <param name="itemSerializer">The item serializer.</param>
        public StackSerializer(IBsonSerializer itemSerializer)
            : base(itemSerializer)
47 48 49
        {
        }

T
tanghai 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        /// <summary>
        /// Initializes a new instance of the <see cref="StackSerializer" /> class.
        /// </summary>
        /// <param name="serializerRegistry">The serializer registry.</param>
        public StackSerializer(IBsonSerializerRegistry serializerRegistry)
            : base(serializerRegistry)
        {
        }

        // public methods
        /// <summary>
        /// Returns a serializer that has been reconfigured with the specified item serializer.
        /// </summary>
        /// <param name="itemSerializer">The item serializer.</param>
        /// <returns>The reconfigured serializer.</returns>
        public StackSerializer WithItemSerializer(IBsonSerializer itemSerializer)
        {
            return new StackSerializer(itemSerializer);
        }

       // protected methods
71 72 73
        /// <summary>
        /// Adds the item.
        /// </summary>
T
tanghai 已提交
74
        /// <param name="accumulator">The accumulator.</param>
75
        /// <param name="item">The item.</param>
T
tanghai 已提交
76
        protected override void AddItem(object accumulator, object item)
77
        {
T
tanghai 已提交
78
            ((Stack)accumulator).Push(item);
79 80 81
        }

        /// <summary>
T
tanghai 已提交
82
        /// Creates the accumulator.
83
        /// </summary>
T
tanghai 已提交
84 85
        /// <returns>The accumulator.</returns>
        protected override object CreateAccumulator()
86 87 88 89 90
        {
            return new Stack();
        }

        /// <summary>
T
tanghai 已提交
91
        /// Enumerates the items in serialization order.
92
        /// </summary>
T
tanghai 已提交
93
        /// <param name="value">The value.</param>
94
        /// <returns>The items.</returns>
T
tanghai 已提交
95
        protected override IEnumerable EnumerateItemsInSerializationOrder(Stack value)
96
        {
T
tanghai 已提交
97
            return value.Cast<object>().Reverse();
98 99 100 101 102
        }

        /// <summary>
        /// Finalizes the result.
        /// </summary>
T
tanghai 已提交
103
        /// <param name="accumulator">The accumulator.</param>
104
        /// <returns>The result.</returns>
T
tanghai 已提交
105 106 107 108 109 110 111
        protected override Stack FinalizeResult(object accumulator)
        {
            return (Stack)accumulator;
        }

        // explicit interface implementations
        IBsonSerializer IChildSerializerConfigurable.ChildSerializer
112
        {
T
tanghai 已提交
113 114 115 116 117 118
            get { return ItemSerializer; }
        }

        IBsonSerializer IChildSerializerConfigurable.WithChildSerializer(IBsonSerializer childSerializer)
        {
            return WithItemSerializer(childSerializer);
119 120 121 122 123 124
        }
    }

    /// <summary>
    /// Represents a serializer for Stacks.
    /// </summary>
T
tanghai 已提交
125 126 127 128 129
    /// <typeparam name="TItem">The type of the elements.</typeparam>
    public class StackSerializer<TItem> :
        EnumerableSerializerBase<Stack<TItem>, TItem>,
        IChildSerializerConfigurable,
        IBsonArraySerializer
130 131 132
    {
        // constructors
        /// <summary>
T
tanghai 已提交
133
        /// Initializes a new instance of the <see cref="StackSerializer{TItem}"/> class.
134 135 136 137 138
        /// </summary>
        public StackSerializer()
        {
        }

T
tanghai 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        /// <summary>
        /// Initializes a new instance of the <see cref="StackSerializer{TItem}"/> class.
        /// </summary>
        /// <param name="itemSerializer">The item serializer.</param>
        public StackSerializer(IBsonSerializer<TItem> itemSerializer)
            : base(itemSerializer)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="StackSerializer{TItem}" /> class.
        /// </summary>
        /// <param name="serializerRegistry">The serializer registry.</param>
        public StackSerializer(IBsonSerializerRegistry serializerRegistry)
            : base(serializerRegistry)
        {
        }

        // public methods
        /// <summary>
        /// Returns a serializer that has been reconfigured with the specified item serializer.
        /// </summary>
        /// <param name="itemSerializer">The item serializer.</param>
        /// <returns>The reconfigured serializer.</returns>
        public StackSerializer<TItem> WithItemSerializer(IBsonSerializer<TItem> itemSerializer)
        {
            return new StackSerializer<TItem>(itemSerializer);
        }

168 169 170 171
        // protected methods
        /// <summary>
        /// Adds the item.
        /// </summary>
T
tanghai 已提交
172
        /// <param name="accumulator">The accumulator.</param>
173
        /// <param name="item">The item.</param>
T
tanghai 已提交
174
        protected override void AddItem(object accumulator, TItem item)
175
        {
T
tanghai 已提交
176
            ((Stack<TItem>)accumulator).Push(item);
177 178 179
        }

        /// <summary>
T
tanghai 已提交
180
        /// Creates the accumulator.
181
        /// </summary>
T
tanghai 已提交
182 183
        /// <returns>The accumulator.</returns>
        protected override object CreateAccumulator()
184
        {
T
tanghai 已提交
185
            return new Stack<TItem>();
186 187 188
        }

        /// <summary>
T
tanghai 已提交
189
        /// Enumerates the items in serialization order.
190
        /// </summary>
T
tanghai 已提交
191
        /// <param name="value">The value.</param>
192
        /// <returns>The items.</returns>
T
tanghai 已提交
193
        protected override IEnumerable<TItem> EnumerateItemsInSerializationOrder(Stack<TItem> value)
194
        {
T
tanghai 已提交
195
            return value.Reverse();
196 197 198 199 200
        }

        /// <summary>
        /// Finalizes the result.
        /// </summary>
T
tanghai 已提交
201
        /// <param name="accumulator">The accumulator.</param>
202
        /// <returns>The result.</returns>
T
tanghai 已提交
203 204 205 206 207 208 209 210 211 212 213 214
        protected override Stack<TItem> FinalizeResult(object accumulator)
        {
            return (Stack<TItem>)accumulator;
        }

        // explicit interface implementations
        IBsonSerializer IChildSerializerConfigurable.ChildSerializer
        {
            get { return ItemSerializer; }
        }

        IBsonSerializer IChildSerializerConfigurable.WithChildSerializer(IBsonSerializer childSerializer)
215
        {
T
tanghai 已提交
216
            return WithItemSerializer((IBsonSerializer<TItem>)childSerializer);
217 218 219
        }
    }
}