//******************************************************************************************************
// Trackable.cs - Gbtc
//
// Copyright © 2016, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 07/07/2016 - Stephen C. Wills
// Generated original version of source code.
//
//******************************************************************************************************
using System.Collections.Generic;
using System.Linq;
namespace GSF
{
///
/// Represents the change history for a property.
///
///
///
/// This class is primarily designed to track changes to the
/// property of an object, as in the following example.
///
///
///
/// public class MyClass
/// {
/// private Trackable<string> m_trackableText = new Trackable<string>(nameof(Text), true);
///
/// public string Text
/// {
/// get
/// {
/// return m_trackableText.CurrentValue;
/// }
/// set
/// {
/// m_trackableText.CurrentValue = value;
/// }
/// }
/// }
///
///
public class Trackable : ITrackable
{
#region [ Members ]
// Fields
private string m_propertyName;
private T m_initialValue;
private List m_changeList;
private bool m_autoTrack;
#endregion
#region [ Constructors ]
///
/// Creates a new instance of the class.
///
/// The name of the property being tracked.
/// Determines the behavior of the setter for the property. Set to false to track only the original and current values.
public Trackable(string propertyName, bool autoTrack)
: this(propertyName, default(T), autoTrack)
{
}
///
/// Creates a new instance of the class.
///
/// The name of the property being tracked.
/// The initial value of the property.
/// Determines the behavior of the setter for the property. Set to false to track only the original and current values.
public Trackable(string propertyName, T initialValue = default(T), bool autoTrack = false)
{
m_propertyName = propertyName;
m_initialValue = initialValue;
m_changeList = new List() { initialValue };
m_autoTrack = autoTrack;
}
#endregion
#region [ Properties ]
///
/// Gets the name of the property being tracked.
///
public string PropertyName
{
get
{
return m_propertyName;
}
}
///
/// Gets or sets the current value after all changes have been applied to the property.
///
public T CurrentValue
{
get
{
return ChangeList.Last();
}
set
{
SetCurrentValue(value, !m_autoTrack);
}
}
///
/// Gets the original value before the first change was made to the property.
///
public T OriginalValue
{
get
{
return ChangeList.First();
}
}
///
/// Gets a value to indicate whether the property's value has been
/// changed since the last time the current value was committed.
///
public bool IsChanged
{
get
{
return !Equals(OriginalValue, CurrentValue);
}
}
///
/// Gets the list of changes to the property's value,
/// starting from its original value and up to the current value.
///
public List ChangeList
{
get
{
if (m_changeList.Count == 0)
m_changeList.Add(m_initialValue);
return m_changeList;
}
}
///
/// Gets the original value before the first change was made to the property.
///
object ITrackable.OriginalValue
{
get
{
return OriginalValue;
}
}
///
/// Gets the current value after all changes have been applied to the property.
///
object ITrackable.CurrentValue
{
get
{
return CurrentValue;
}
}
#endregion
#region [ Methods ]
///
/// Sets the current value of the property.
///
/// The new value of the property.
/// True to track the change; false to overwrite the previous change. Does not overwrite the original value.
public void SetCurrentValue(T value, bool overwrite)
{
if (overwrite && m_changeList.Count > 1)
m_changeList.RemoveAt(m_changeList.Count - 1);
if (Equals(CurrentValue, value))
return;
m_changeList.Add(value);
}
///
/// Erases all history in the change list and commits
/// the current value as the new original value.
///
public void AcceptChanges()
{
T currentValue = CurrentValue;
m_changeList.Clear();
m_changeList.Add(currentValue);
}
///
/// Erases all history in the change list and reverts
/// to the original value before the changes were made.
///
public void Revert()
{
T originalValue = OriginalValue;
m_changeList.Clear();
m_changeList.Add(originalValue);
}
///
/// Erases all history in the change list and sets
/// the original value back to its initial value.
///
public void Reset()
{
m_changeList.Clear();
m_changeList.Add(m_initialValue);
}
///
/// Returns a string that represents the current value.
///
/// A string that represents the current value.
public override string ToString()
{
return CurrentValue.ToString();
}
#endregion
}
}