265 lines
9.6 KiB
C#
265 lines
9.6 KiB
C#
using Sarand.Net.Standard.Base.App;
|
|
using Sarand.Net.Standard.Base.Utils;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
|
|
namespace Sarand.Net.Standard.Base.Core
|
|
{
|
|
//=======================================================================
|
|
public class NamedValue
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Data members.
|
|
|
|
//-------------------------------------------------------------------
|
|
// Construction.
|
|
private NamedValue (String name, DataValue value)
|
|
{
|
|
Name = StringUtil.EmptyOnNull(name);
|
|
Value = value ?? new DataValue();
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Properties.
|
|
public String Name { get; private set; }
|
|
public DataValue Value { get; private set; }
|
|
|
|
//-------------------------------------------------------------------
|
|
/// <summary>Associative array of named values, case insensitive.</summary>
|
|
public class Map
|
|
{
|
|
//---------------------------------------------------------------
|
|
// Data members.
|
|
private readonly Dictionary<String,DataValue> values = new Dictionary<String, DataValue>(StringComparer.OrdinalIgnoreCase);
|
|
|
|
//---------------------------------------------------------------
|
|
// Construction.
|
|
public Map (Dictionary<String, DataValue> values = null)
|
|
{
|
|
if (values != null)
|
|
this.values.AddRange(values);
|
|
}
|
|
public Map (NamedValue.Map values)
|
|
{
|
|
if (values != null)
|
|
this.values.AddRange(values.values);
|
|
}
|
|
|
|
//---------------------------------------------------------------
|
|
// Properties.
|
|
/// <summary>Optional identifier for the set of values.</summary>
|
|
public String Id { get; set; } = String.Empty;
|
|
/// <summary>Retrieve a copy of the value list.</summary>
|
|
public Dictionary<String, DataValue> Values { get { return new Dictionary<String, DataValue>(values); } }
|
|
/// <summary>Number of values in collection.</summary>
|
|
public int Count { get { return values.Count; } }
|
|
|
|
//---------------------------------------------------------------
|
|
// Update list.
|
|
public void Clear ()
|
|
{
|
|
values.Clear();
|
|
}
|
|
public bool AddValue (String name, DataValue value)
|
|
{
|
|
if (String.IsNullOrEmpty(name) || value == null)
|
|
{
|
|
if (SacsSystem.DebugMode) SaLogger.Debug(GetType().Name + ".AddValue Invalid named value. Name: " + StringUtil.EmptyOnNull(name) + ". Value: " + (value != null ? value.GetDataType().ToString() : "null"));
|
|
return false;
|
|
}
|
|
values[name] = value;
|
|
return true;
|
|
}
|
|
public bool AddValues (NamedValue.Map values)
|
|
{
|
|
return AddValues(values.ToNamedValueList());
|
|
}
|
|
public bool AddValues (ICollection<NamedValue> values)
|
|
{
|
|
bool addValues = true;
|
|
foreach (NamedValue value in values)
|
|
{
|
|
if (!AddValue(value.Name, value.Value))
|
|
addValues = false;
|
|
}
|
|
return addValues;
|
|
}
|
|
/// <summary>Remove the named value from the list if present.</summary>
|
|
public bool RemoveValue (String name)
|
|
{
|
|
if (!HasValue(name)) return false;
|
|
return values.Remove(name);
|
|
}
|
|
/// <summary>Change the name of a value in the list.</summary>
|
|
public bool RenameValue (String nameOld, String nameNew)
|
|
{
|
|
if (StringUtil.IsEmpty(nameOld) || !HasValue(nameOld) || StringUtil.IsEmpty(nameNew) || nameOld.EqualsCI(nameNew)) return false;
|
|
DataValue value = GetValue(nameOld);
|
|
RemoveValue(nameOld);
|
|
return AddValue(nameNew, value);
|
|
}
|
|
|
|
//---------------------------------------------------------------
|
|
/// <summary>Does the list include a value with the specified name.</summary>
|
|
public bool HasValue (String name)
|
|
{
|
|
return values.HasValue(name);
|
|
}
|
|
|
|
//---------------------------------------------------------------
|
|
/// <summary>Retrieve the value with the specified name.</summary>
|
|
public DataValue GetValue (String name)
|
|
{
|
|
return values.GetValue(name);
|
|
}
|
|
/// <summary>Retrieve the named value as a String.</summary>
|
|
public String GetString (String name, String defaultValue = "")
|
|
{
|
|
return values.GetString(name, defaultValue);
|
|
}
|
|
/// <summary>Retrieve the named value as a boolean.</summary>
|
|
public bool GetBool (String name, bool defaultValue = false)
|
|
{
|
|
return values.GetBool(name, defaultValue);
|
|
}
|
|
/// <summary>Retrieve the named value as a long integer.</summary>
|
|
public long GetLong (String name, long defaultValue = 0)
|
|
{
|
|
return values.GetLong(name, defaultValue);
|
|
}
|
|
/// <summary>Retrieve the named value as a double.</summary>
|
|
public double GetDouble (String name, double defaultValue = 0)
|
|
{
|
|
return values.GetDouble(name, defaultValue);
|
|
}
|
|
/// <summary>Retrieve the named value as a decimal.</summary>
|
|
public decimal GetDecimal (String name, decimal defaultValue = 0)
|
|
{
|
|
return values.GetDecimal(name, defaultValue);
|
|
}
|
|
/// <summary>Retrieve the named value as a DateTime.</summary>
|
|
public DateTime GetDateTime (String name)
|
|
{
|
|
return values.GetDateTime(name);
|
|
}
|
|
/// <summary>Retrieve the named value as a date.</summary>
|
|
public DateTime GetDate (String name)
|
|
{
|
|
return values.GetDate(name);
|
|
}
|
|
/// <summary>Retrieve the named value as a time.</summary>
|
|
public DateTime GetTime (String name)
|
|
{
|
|
return values.GetTime(name);
|
|
}
|
|
|
|
//---------------------------------------------------------------
|
|
// Conversion.
|
|
//---------------------------------------------------------------
|
|
public List<NamedValue> ToNamedValueList ()
|
|
{
|
|
List<NamedValue> values = new List<NamedValue>();
|
|
foreach (String name in this.values.Keys)
|
|
values.Add(new NamedValue(name, this.values.GetValue(name)));
|
|
return values;
|
|
}
|
|
|
|
/// <summary>Concatenate the values with the specified delimiters.</summary>
|
|
public String Concat (String entryDelim = ".", String valueDelim = "=")
|
|
{
|
|
return values.Concat(entryDelim, valueDelim);
|
|
}
|
|
|
|
//---------------------------------------------------------------
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
|
|
//=======================================================================
|
|
public static class NamedValueDictionary
|
|
{
|
|
//-------------------------------------------------------------------
|
|
/// <summary>Does the list include a value with the specified name, case insensitive.</summary>
|
|
public static bool HasValue (this Dictionary<String, DataValue> values, String name)
|
|
{
|
|
if (String.IsNullOrEmpty(name)) return false;
|
|
return values.ContainsKey(name);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
/// <summary>Retrieve the value with the specified name.</summary>
|
|
public static DataValue GetValue (this Dictionary<String, DataValue> values, String name)
|
|
{
|
|
if (StringUtil.IsEmpty(name)) return DataValue.NullValue;
|
|
if (values.ContainsKey(name))
|
|
return values[name];
|
|
return DataValue.NullValue;
|
|
}
|
|
/// <summary>Retrieve the named value as a String.</summary>
|
|
public static String GetString (this Dictionary<String, DataValue> values, String name, String defaultValue = "")
|
|
{
|
|
if (values.HasValue(name)) return values.GetValue(name).ToString();
|
|
return defaultValue;
|
|
}
|
|
/// <summary>Retrieve the named value as a boolean.</summary>
|
|
public static bool GetBool (this Dictionary<String, DataValue> values, String name, bool defaultValue = false)
|
|
{
|
|
if (values.HasValue(name)) return values.GetValue(name).AsBool();
|
|
return defaultValue;
|
|
}
|
|
/// <summary>Retrieve the named value as a long integer.</summary>
|
|
public static long GetLong (this Dictionary<String, DataValue> values, String name, long defaultValue = 0)
|
|
{
|
|
if (values.HasValue(name)) return values.GetValue(name).AsLong();
|
|
return defaultValue;
|
|
}
|
|
/// <summary>Retrieve the named value as a double.</summary>
|
|
public static double GetDouble (this Dictionary<String, DataValue> values, String name, double defaultValue = 0)
|
|
{
|
|
if (values.HasValue(name)) return values.GetValue(name).AsDouble();
|
|
return defaultValue;
|
|
}
|
|
/// <summary>Retrieve the named value as a decimal.</summary>
|
|
public static decimal GetDecimal (this Dictionary<String, DataValue> values, String name, decimal defaultValue = 0)
|
|
{
|
|
if (values.HasValue(name)) return values.GetValue(name).AsDecimal();
|
|
return defaultValue;
|
|
}
|
|
/// <summary>Retrieve the named value as a DateTime.</summary>
|
|
public static DateTime GetDateTime (this Dictionary<String, DataValue> values, String name)
|
|
{
|
|
if (values.HasValue(name)) return values.GetValue(name).AsDateTime();
|
|
return DateTime.MinValue;
|
|
}
|
|
/// <summary>Retrieve the named value as a date.</summary>
|
|
public static DateTime GetDate (this Dictionary<String, DataValue> values, String name)
|
|
{
|
|
if (values.HasValue(name)) return values.GetValue(name).AsDate();
|
|
return DateTime.MinValue;
|
|
}
|
|
/// <summary>Retrieve the named value as a time.</summary>
|
|
public static DateTime GetTime (this Dictionary<String, DataValue> values, String name)
|
|
{
|
|
if (values.HasValue(name)) return values.GetValue(name).AsTime();
|
|
return DateTime.MinValue;
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
// Conversion.
|
|
/// <summary>Concatenate the values with the specified delimiters.</summary>
|
|
public static String Concat (this Dictionary<String, DataValue> values, String entryDelim = ".", String valueDelim = "=")
|
|
{
|
|
List<String> dataValues = new List<string>();
|
|
foreach (String name in values.Keys)
|
|
dataValues.Add(name + valueDelim + values[name]);
|
|
return dataValues.Concat(entryDelim);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
|
|
//=======================================================================
|
|
}
|