/* Copyright 2010-2014 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.IO
{
///
/// Represents settings for a BsonReader.
///
[Serializable]
public abstract class BsonReaderSettings
{
// private fields
private GuidRepresentation _guidRepresentation = BsonDefaults.GuidRepresentation;
private bool _isFrozen;
// constructors
///
/// Initializes a new instance of the BsonReaderSettings class.
///
protected BsonReaderSettings()
{
}
///
/// Initializes a new instance of the BsonReaderSettings class.
///
/// The representation for Guids.
protected BsonReaderSettings(GuidRepresentation guidRepresentation)
{
_guidRepresentation = guidRepresentation;
}
// public properties
///
/// Gets or sets the representation for Guids.
///
public GuidRepresentation GuidRepresentation
{
get { return _guidRepresentation; }
set
{
if (_isFrozen) { ThrowFrozenException(); }
_guidRepresentation = value;
}
}
///
/// Gets whether the settings are frozen.
///
public bool IsFrozen
{
get { return _isFrozen; }
}
// public methods
///
/// Creates a clone of the settings.
///
/// A clone of the settings.
public BsonReaderSettings Clone()
{
return CloneImplementation();
}
///
/// Freezes the settings.
///
/// The frozen settings.
public BsonReaderSettings Freeze()
{
_isFrozen = true;
return this;
}
///
/// Returns a frozen copy of the settings.
///
/// A frozen copy of the settings.
public BsonReaderSettings FrozenCopy()
{
if (_isFrozen)
{
return this;
}
else
{
return Clone().Freeze();
}
}
// protected methods
///
/// Creates a clone of the settings.
///
/// A clone of the settings.
protected abstract BsonReaderSettings CloneImplementation();
///
/// Throws an InvalidOperationException when an attempt is made to change a setting after the settings are frozen.
///
protected void ThrowFrozenException()
{
var message = string.Format("{0} is frozen.", this.GetType().Name);
throw new InvalidOperationException(message);
}
}
}