/* 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.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
using System.Text;
namespace MongoDB.Driver
{
///
/// Represents an identity in MongoDB.
///
public abstract class MongoIdentity : IEquatable
{
// private fields
private readonly string _source;
private readonly string _username;
// constructors
///
/// Initializes a new instance of the class.
///
/// The source.
/// The username.
/// Whether to allow null usernames.
internal MongoIdentity(string source, string username, bool allowNullUsername = false)
{
if (source == null)
{
throw new ArgumentNullException("source");
}
if (username == null && !allowNullUsername)
{
throw new ArgumentNullException("username");
}
_source = source;
_username = username;
}
// public properties
///
/// Gets the source.
///
public string Source
{
get { return _source; }
}
///
/// Gets the username.
///
public string Username
{
get { return _username; }
}
// public operators
///
/// Compares two MongoIdentity values.
///
/// The first MongoIdentity.
/// The other MongoIdentity.
/// True if the two MongoIdentity values are equal (or both null).
public static bool operator ==(MongoIdentity lhs, MongoIdentity rhs)
{
return object.Equals(lhs, rhs);
}
///
/// Compares two MongoIdentity values.
///
/// The first MongoIdentity.
/// The other MongoIdentity.
/// True if the two MongoIdentity values are not equal (or one is null and the other is not).
public static bool operator !=(MongoIdentity lhs, MongoIdentity rhs)
{
return !(lhs == rhs);
}
// public methods
///
/// Determines whether the specified is equal to this instance.
///
/// The to compare with this instance.
///
/// true if the specified is equal to this instance; otherwise, false.
///
public override bool Equals(object obj)
{
return Equals(obj as MongoIdentity);
}
///
/// Determines whether the specified instance is equal to this instance.
///
/// The right-hand side.
///
/// true if the specified instance is equal to this instance; otherwise, false.
///
public bool Equals(MongoIdentity rhs)
{
if (object.ReferenceEquals(rhs, null) || GetType() != rhs.GetType()) { return false; }
return _username == rhs._username && _source == rhs._source;
}
///
/// Returns a hash code for this instance.
///
///
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
///
public override int GetHashCode()
{
var hash = 17;
hash += 37 * (_username == null ? 0 :_username.GetHashCode());
hash += 37 * _source.GetHashCode();
return hash;
}
}
}