Files
DP44/Common/DTS.Common.Utilities/PowerOfTwoIntProperty.cs

141 lines
4.3 KiB
C#
Raw Permalink Normal View History

2026-04-17 14:55:32 -04:00
/*
PowerOfTwoIntProperty.cs
Copyright <EFBFBD> 2008
Diversified Technical Systems, Inc.
All Rights Reserved
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using DTS.Common.Utilities;
namespace DTS.Common.Utilities.DotNetProgrammingConstructs
{
/// <summary>
/// A class to implement self-checking, on-error-auto-syntax-building
/// power of 2 int properties.
/// </summary>
public class PowerOfTwoIntProperty : Property<int>
{
/// <summary>
/// Determine whether or not the specified value is valid for this property.
/// </summary>
///
/// <param name="value">
/// The <see cref="int"/> value to be validity checked.
/// </param>
///
/// <returns>
/// <see cref="bool"/> true if the value is valid; false otherwise.
/// </returns>
///
public override bool IsValidValue(int value)
{
try
{
return IsPowerOf2(value);
}
catch (System.Exception ex)
{
throw new Exception(
string.Format(Properties.Resources.PowerOfTwoProperty_IsValidValue_UnableToDetermineValidityString, value.ToString()),
ex);
}
}
/// <summary>
/// Determine whether or not the specified value is a power of two.
/// </summary>
///
/// <param name="value">
/// The <see cref="int"/> value to be power of 2-checked.
/// </param>
///
/// <returns>
/// <see cref="bool"/> true if the value is a power of 2, false otherwise.
/// </returns>
///
private bool IsPowerOf2(int value)
{
try
{
for (var r = value; r > 0; r >>= 1)
if (2 == r) return true;
return false;
}
catch (System.Exception ex)
{
throw new Exception(
string.Format(Properties.Resources.PowerOfTwoProperty_IsPowerOf2_UnableToDeterminePowerOf2nessString, value.ToString()),
ex);
}
}
/// <summary>
/// Generate a user-readable explanation as to why the specified value is
/// not valid for this property.
/// </summary>
///
/// <param name="value">
/// The <see cref="int"/> value to be described.
/// </param>
///
/// <returns>
/// A <see cref="string"/> description explaining why the specified value
/// is not valid for this property.
/// </returns>
///
public override string GetInvalidValueDescription(int value)
{
try
{
return string.Format(Properties.Resources.PowerOfTwoProperty_GetInvalidValueDescription_ProposedValueIsNotPowerOf2String, value.ToString());
}
catch (System.Exception ex)
{
throw new Exception(Properties.Resources.PowerOfTwoProperty_GetInvalidValueDescription_UnableToGetDescriptionString, ex);
}
}
/// <summary>
/// Initialize an instance of a power of two int property.
/// </summary>
///
/// <param name="initialValue">
/// The initialize <see cref="int"/> value of the int property.
/// </param>
///
public PowerOfTwoIntProperty(int initialValue)
: base(initialValue, true)
{
try
{
if (!IsValidValue(initialValue))
throw new InvalidValueException(GetInvalidValueDescription(initialValue));
else return;
}
catch (System.Exception ex)
{
throw new ConstructionException(
string.Format(Properties.Resources.Generic_EncounteredProblemConstructingClassString, GetType().FullName)
, ex);
}
}
/// <summary>
/// Initialize an instance of a power of two int property.
/// </summary>
public PowerOfTwoIntProperty()
: base(0, false)
{
}
}
}