/* 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;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Shared;
namespace MongoDB.Bson
{
///
/// Represents a BSON array.
///
#if NET452
[Serializable]
#endif
public class BsonArray : BsonValue, IComparable, IEquatable, IList
{
// private fields
private readonly List _values;
// constructors
///
/// Initializes a new instance of the BsonArray class.
///
public BsonArray()
: this(0)
{
}
///
/// Initializes a new instance of the BsonArray class.
///
/// A list of values to add to the array.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public BsonArray(IEnumerable values)
: this(0)
{
AddRange(values);
}
///
/// Initializes a new instance of the BsonArray class.
///
/// A list of values to add to the array.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public BsonArray(IEnumerable values)
: this(0)
{
AddRange(values);
}
///
/// Initializes a new instance of the BsonArray class.
///
/// A list of values to add to the array.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public BsonArray(IEnumerable values)
: this(0)
{
AddRange(values);
}
///
/// Initializes a new instance of the BsonArray class.
///
/// A list of values to add to the array.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public BsonArray(IEnumerable values)
: this(0)
{
AddRange(values);
}
///
/// Initializes a new instance of the BsonArray class.
///
/// A list of values to add to the array.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public BsonArray(IEnumerable values)
: this(0)
{
AddRange(values);
}
///
/// Initializes a new instance of the BsonArray class.
///
/// A list of values to add to the array.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public BsonArray(IEnumerable values)
: this(0)
{
AddRange(values);
}
///
/// Initializes a new instance of the BsonArray class.
///
/// A list of values to add to the array.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public BsonArray(IEnumerable values)
: this(0)
{
AddRange(values);
}
///
/// Initializes a new instance of the BsonArray class.
///
/// A list of values to add to the array.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public BsonArray(IEnumerable values)
: this(0)
{
AddRange(values);
}
///
/// Initializes a new instance of the BsonArray class.
///
/// A list of values to add to the array.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public BsonArray(IEnumerable values)
: this(0)
{
AddRange(values);
}
///
/// Initializes a new instance of the BsonArray class.
///
/// The initial capacity of the array.
public BsonArray(int capacity)
{
_values = new List(capacity);
}
// public operators
///
/// Compares two BsonArray values.
///
/// The first BsonArray.
/// The other BsonArray.
/// True if the two BsonArray values are not equal according to ==.
public static bool operator !=(BsonArray lhs, BsonArray rhs)
{
return !(lhs == rhs);
}
///
/// Compares two BsonArray values.
///
/// The first BsonArray.
/// The other BsonArray.
/// True if the two BsonArray values are equal according to ==.
public static bool operator ==(BsonArray lhs, BsonArray rhs)
{
return object.Equals(lhs, rhs); // handles lhs == null correctly
}
// public properties
///
/// Gets the BsonType of this BsonValue.
///
public override BsonType BsonType
{
get { return BsonType.Array; }
}
///
/// Gets or sets the total number of elements the internal data structure can hold without resizing.
///
public virtual int Capacity
{
get { return _values.Capacity; }
set { _values.Capacity = value; }
}
///
/// Gets the count of array elements.
///
public virtual int Count
{
get { return _values.Count; }
}
///
/// Gets whether the array is read-only.
///
public virtual bool IsReadOnly
{
get { return false; }
}
///
/// Gets the array elements as raw values (see BsonValue.RawValue).
///
[Obsolete("Use ToArray to ToList instead.")]
public virtual IEnumerable