// Copyright (C) Stichting Deltares 2018. 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;
using System.Collections.Generic;
using System.Linq;
using Core.Common.Base;
using Core.Common.Util;
using Ringtoets.Common.Data.Properties;
namespace Ringtoets.Common.Data.FailureMechanism
{
///
/// A collection of .
///
public class FailureMechanismSectionCollection : Observable, IEnumerable
{
private readonly List sections = new List();
///
/// Gets the last known file path from which the were imported.
///
/// The path where the elements originate
/// from, or null if the collection is cleared.
public string SourcePath { get; private set; }
///
/// Clears the imported items in the collection and the .
///
public void Clear()
{
SourcePath = null;
sections.Clear();
}
///
/// Sets all originating from a source file to the collection.
///
/// The collection of to set.
/// The path to the source file.
/// Thrown when any parameter is null.
/// Thrown when:
///
/// - is not a valid file path.
/// - contains sections that are not properly chained.
///
public void SetSections(IEnumerable failureMechanismSections,
string sourcePath)
{
if (failureMechanismSections == null)
{
throw new ArgumentNullException(nameof(failureMechanismSections));
}
if (sourcePath == null)
{
throw new ArgumentNullException(nameof(sourcePath));
}
if (!IOUtils.IsValidFilePath(sourcePath) && sourcePath.Length > 0)
{
throw new ArgumentException($@"'{sourcePath}' is not a valid file path.", nameof(sourcePath));
}
if (!failureMechanismSections.Any())
{
Clear();
SourcePath = sourcePath;
return;
}
FailureMechanismSection firstSection = failureMechanismSections.First();
var newSections = new List
{
firstSection
};
FailureMechanismSection previousSection = firstSection;
foreach (FailureMechanismSection section in failureMechanismSections.Skip(1))
{
ValidateSectionConnectivity(section, previousSection);
newSections.Add(section);
previousSection = section;
}
Clear();
sections.AddRange(newSections);
SourcePath = sourcePath;
}
public IEnumerator GetEnumerator()
{
return sections.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
///
/// Validates the section on its connectivity order (neighboring
/// must have same end points).
///
/// The new section.
/// The previous section.
/// Thrown when cannot
/// be connected to the previous section.
private static void ValidateSectionConnectivity(FailureMechanismSection section, FailureMechanismSection previousSection)
{
if (!previousSection.EndPoint.Equals(section.StartPoint))
{
string message = string.Format(Resources.FailureMechanismSectionCollection_ValidateSection_Section_0_must_connect_to_existing_sections,
section.Name);
throw new ArgumentException(message, nameof(section));
}
}
}
}