/* Copyright 2010-2016 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; namespace MongoDB.Bson { /// /// Represents a BSON JavaScript value with a scope. /// #if NET45 [Serializable] #endif public class BsonJavaScriptWithScope : BsonJavaScript, IComparable, IEquatable { // private fields private readonly BsonDocument _scope; // constructors /// /// Initializes a new instance of the BsonJavaScriptWithScope class. /// /// The JavaScript code. /// A scope (a set of variables with values). public BsonJavaScriptWithScope(string code, BsonDocument scope) : base(code) { if (scope == null) { throw new ArgumentNullException("scope"); } _scope = scope; } // public operators /// /// Compares two BsonJavaScriptWithScope values. /// /// The first BsonJavaScriptWithScope. /// The other BsonJavaScriptWithScope. /// True if the two BsonJavaScriptWithScope values are not equal according to ==. public static bool operator !=(BsonJavaScriptWithScope lhs, BsonJavaScriptWithScope rhs) { return !(lhs == rhs); } /// /// Compares two BsonJavaScriptWithScope values. /// /// The first BsonJavaScriptWithScope. /// The other BsonJavaScriptWithScope. /// True if the two BsonJavaScriptWithScope values are equal according to ==. public static bool operator ==(BsonJavaScriptWithScope lhs, BsonJavaScriptWithScope rhs) { if (object.ReferenceEquals(lhs, null)) { return object.ReferenceEquals(rhs, null); } return lhs.Equals(rhs); } // public properties /// /// Gets the BsonType of this BsonValue. /// public override BsonType BsonType { get { return BsonType.JavaScriptWithScope; } } /// /// Gets the scope (a set of variables with values). /// public BsonDocument Scope { get { return _scope; } } // public static methods /// /// Creates a new BsonJavaScriptWithScope. /// /// An object to be mapped to a BsonJavaScriptWithScope. /// A BsonJavaScriptWithScope or null. public new static BsonJavaScriptWithScope Create(object value) { if (value == null) { throw new ArgumentNullException("value"); } return (BsonJavaScriptWithScope)BsonTypeMapper.MapToBsonValue(value, BsonType.JavaScriptWithScope); } // public methods /// /// Creates a shallow clone of the BsonJavaScriptWithScope (see also DeepClone). /// /// A shallow clone of the BsonJavaScriptWithScope. public override BsonValue Clone() { return new BsonJavaScriptWithScope(Code, (BsonDocument)_scope.Clone()); } /// /// Creates a deep clone of the BsonJavaScriptWithScope (see also Clone). /// /// A deep clone of the BsonJavaScriptWithScope. public override BsonValue DeepClone() { BsonJavaScriptWithScope clone = new BsonJavaScriptWithScope(Code, new BsonDocument()); foreach (BsonElement element in _scope) { clone._scope.Add(element.DeepClone()); } return clone; } /// /// Compares this BsonJavaScriptWithScope to another BsonJavaScriptWithScope. /// /// The other BsonJavaScriptWithScope. /// A 32-bit signed integer that indicates whether this BsonJavaScriptWithScope is less than, equal to, or greather than the other. public int CompareTo(BsonJavaScriptWithScope other) { if (other == null) { return 1; } int r = Code.CompareTo(other.Code); if (r != 0) { return r; } return _scope.CompareTo(other._scope); } /// /// Compares the BsonJavaScriptWithScope to another BsonValue. /// /// The other BsonValue. /// A 32-bit signed integer that indicates whether this BsonJavaScriptWithScope is less than, equal to, or greather than the other BsonValue. public override int CompareTo(BsonValue other) { if (other == null) { return 1; } var otherJavaScriptWithScope = other as BsonJavaScriptWithScope; if (otherJavaScriptWithScope != null) { return CompareTo(otherJavaScriptWithScope); } return CompareTypeTo(other); } /// /// Compares this BsonJavaScriptWithScope to another BsonJavaScriptWithScope. /// /// The other BsonJavaScriptWithScope. /// True if the two BsonJavaScriptWithScope values are equal. public bool Equals(BsonJavaScriptWithScope rhs) { if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; } return Code == rhs.Code && _scope == rhs._scope; } /// /// Compares this BsonJavaScriptWithScope to another object. /// /// The other object. /// True if the other object is a BsonJavaScriptWithScope and equal to this one. public override bool Equals(object obj) { return Equals(obj as BsonJavaScriptWithScope); // works even if obj is null or of a different type } /// /// Gets the hash code. /// /// The hash code. public override int GetHashCode() { // see Effective Java by Joshua Bloch int hash = 17; hash = 37 * hash + base.GetHashCode(); hash = 37 * hash + _scope.GetHashCode(); return hash; } /// /// Returns a string representation of the value. /// /// A string representation of the value. public override string ToString() { return string.Format("new BsonJavaScript(\"{0}\", {1})", Code, _scope.ToJson()); } } }