using StructureHelperCommon.Infrastructures.Interfaces;
using StructureHelperCommon.Services;
namespace StructureHelperLogics.NdmCalculations.Analyses.Curvatures
{
///
/// Provides an update strategy for ,
/// allowing transfer of child collections and nested objects from a source
/// instance to a target instance.
///
public class CurvatureCalculatorInputDataUpdateStrategy
: IParentUpdateStrategy
{
private IUpdateStrategy? _deflectionUpdateStrategy;
///
/// Gets the strategy used to update the object.
/// Lazily initialized to if none supplied.
///
private IUpdateStrategy DeflectionUpdateStrategy =>
_deflectionUpdateStrategy ??= new DeflectionFactorUpdateStrategy();
///
/// When true, the children's data (collections and nested objects)
/// will be updated from the source. Defaults to true.
///
public bool UpdateChildren { get; set; } = true;
///
/// Creates a new instance of the update strategy.
///
///
/// Optional custom update strategy for .
///
public CurvatureCalculatorInputDataUpdateStrategy(
IUpdateStrategy? deflectionUpdateStrategy = null)
{
_deflectionUpdateStrategy = deflectionUpdateStrategy;
}
///
public void Update(ICurvatureCalculatorInputData target, ICurvatureCalculatorInputData source)
{
CheckObject.ThrowIfNull(source, nameof(source));
CheckObject.ThrowIfNull(target, nameof(target));
if (ReferenceEquals(target, source))
return;
target.ConsiderSofteningFactor = source.ConsiderSofteningFactor;
if (UpdateChildren)
{
ValidateChildProperties(target, source);
UpdateCollections(target, source);
DeflectionUpdateStrategy.Update(target.DeflectionFactor, source.DeflectionFactor);
}
}
///
/// Validates that all required child properties are not null.
///
private static void ValidateChildProperties(
ICurvatureCalculatorInputData target,
ICurvatureCalculatorInputData source)
{
CheckObject.ThrowIfNull(source.Primitives, nameof(source.Primitives));
CheckObject.ThrowIfNull(target.Primitives, nameof(target.Primitives));
CheckObject.ThrowIfNull(source.ForceActions, nameof(source.ForceActions));
CheckObject.ThrowIfNull(target.ForceActions, nameof(target.ForceActions));
CheckObject.ThrowIfNull(source.DeflectionFactor, nameof(source.DeflectionFactor));
CheckObject.ThrowIfNull(target.DeflectionFactor, nameof(target.DeflectionFactor));
}
///
/// Replaces the target collections with copies of the source collections.
///
private static void UpdateCollections(
ICurvatureCalculatorInputData target,
ICurvatureCalculatorInputData source)
{
target.Primitives.Clear();
target.Primitives.AddRange(source.Primitives);
target.ForceActions.Clear();
target.ForceActions.AddRange(source.ForceActions);
}
}
}