// Copyright (C) Stichting Deltares 2016. All rights reserved.
//
// This file is part of Ringtoets.
//
// Ringtoets is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see .
//
// All names, logos, and references to "Deltares" are registered trademarks of
// Stichting Deltares and remain full property of Stichting Deltares at all times.
// All rights reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using Ringtoets.Common.Data;
using Ringtoets.Common.Data.Calculation;
using Ringtoets.Common.Data.FailureMechanism;
using Ringtoets.Common.Data.Structures;
namespace Ringtoets.Common.Utils
{
///
/// Class holds helper methods to match objects
/// with objects.
///
public static class StructuresHelper
{
///
/// Determine which objects are available for a
/// .
///
/// The objects.
/// The objects.
/// A containing a
/// of objects
/// for each section which has calculations.
/// Thrown when any input parameter is null
/// or when an element in is null
/// Thrown when an element in is
/// null.
public static Dictionary> CollectCalculationsPerSection(IEnumerable sections,
IEnumerable> calculations) where T : IStructuresCalculationInput, new()
{
return AssignUnassignCalculations.CollectCalculationsPerSection(sections, AsCalculationsWithLocations(calculations));
}
///
/// Updates the for each element of that have the
/// assigned, or should have the assigned.
///
/// The of to iterate while
/// possibly updating the assigned to it.
/// The which has a location that has been updated.
/// Thrown when is null
/// Thrown when element in is
/// null.
public static void Update(IEnumerable> sectionResults,
StructuresCalculation calculation) where T : IStructuresCalculationInput, new()
{
ValidateSectionResults(sectionResults);
CalculationWithLocation calculationWithLocation = AsCalculationWithLocation(calculation);
if (calculationWithLocation != null)
{
AssignUnassignCalculations.Update(sectionResults.Select(AsCalculationAssignment), calculationWithLocation);
}
}
///
/// Removed the for each
/// element of that have the assigned.
///
/// The of to iterate while
/// removing the reference to the if present.
/// The which has a location that has been updated.
/// The of that were left after removing
/// .
/// All affected objects by the deletion.
/// Thrown when any input parameter is null or when an element
/// in is null.
/// Thrown when element in is
/// null.
public static IEnumerable> Delete(IEnumerable> sectionResults,
StructuresCalculation calculation,
IEnumerable> calculations) where T : IStructuresCalculationInput, new()
{
ValidateSectionResults(sectionResults);
return AssignUnassignCalculations.Delete(
sectionResults.Select(AsCalculationAssignment),
calculation,
AsCalculationsWithLocations(calculations))
.Cast>()
.ToArray();
}
///
/// Transforms the into and filter out the calculations
/// for which a could not be made.
///
/// The collection to transform.
/// A collection of .
/// Thrown when is null or when
/// an element in is null.
private static IEnumerable AsCalculationsWithLocations(IEnumerable> calculations) where T : IStructuresCalculationInput, new()
{
if (calculations == null)
{
throw new ArgumentNullException("calculations");
}
return calculations.Select(AsCalculationWithLocation).Where(c => c != null);
}
private static void ValidateSectionResults(IEnumerable> sectionResults) where T : IStructuresCalculationInput, new()
{
if (sectionResults == null)
{
throw new ArgumentNullException("sectionResults");
}
if (sectionResults.Any(sr => sr == null))
{
throw new ArgumentException(@"SectionResults contains an entry without value.", "sectionResults");
}
}
private static CalculationWithLocation AsCalculationWithLocation(StructuresCalculation calculation) where T : IStructuresCalculationInput, new()
{
if (calculation == null)
{
throw new ArgumentNullException("calculation");
}
if (calculation.InputParameters.Structure == null)
{
return null;
}
return new CalculationWithLocation(calculation, calculation.InputParameters.Structure.Location);
}
private static SectionResultWithCalculationAssignment AsCalculationAssignment(StructuresFailureMechanismSectionResult failureMechanismSectionResult) where T : IStructuresCalculationInput, new()
{
return new SectionResultWithCalculationAssignment(
failureMechanismSectionResult,
result => ((StructuresFailureMechanismSectionResult) result).Calculation,
(result, calculation) => ((StructuresFailureMechanismSectionResult) result).Calculation = (StructuresCalculation) calculation);
}
}
}