// Copyright (C) Stichting Deltares 2017. 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
{
public class FailureMechanismSectionCollection : Observable, IObservableEnumerable
{
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();
}
///
/// Adds all originating from a source file.
///
/// The collection of to add
/// The path to the source file.
/// Thrown when any input argument is null.
/// Thrown when:
///
/// - contains null.
/// - is not a valid file path.
///
///
public void AddRange(IEnumerable failureMechanismSections,
string filePath)
{
if (failureMechanismSections == null)
{
throw new ArgumentNullException(nameof(failureMechanismSections));
}
if (filePath == null)
{
throw new ArgumentNullException(nameof(filePath));
}
if (!IOUtils.IsValidFilePath(filePath) && filePath.Length > 0)
{
throw new ArgumentException($@"'{filePath}' is not a valid file path.", nameof(filePath));
}
if (failureMechanismSections.Contains(null))
{
throw new ArgumentException(@"Collection cannot contain null.", nameof(failureMechanismSections));
}
List sourceCollection = failureMechanismSections.ToList();
if (!sections.Any())
{
FailureMechanismSection firstSection = sourceCollection.First();
sections.Add(firstSection);
sourceCollection.Remove(firstSection);
}
foreach (FailureMechanismSection section in sourceCollection)
{
InsertSectionWhileMaintainingConnectivityOrder(section);
}
SourcePath = filePath;
}
public IEnumerator GetEnumerator()
{
return sections.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
///
/// Inserts the section to the collection while maintaining connectivity
/// order (neighboring have same end points).
///
/// The new section.
/// Thrown when cannot
/// be connected to elements already defined in this collection.
private void InsertSectionWhileMaintainingConnectivityOrder(FailureMechanismSection sectionToInsert)
{
if (sections.Last().EndPoint.Equals(sectionToInsert.StartPoint))
{
sections.Add(sectionToInsert);
}
else
{
string message = string.Format(Resources.BaseFailureMechanism_AddSection_Section_0_must_connect_to_existing_sections,
sectionToInsert.Name);
throw new ArgumentException(message, nameof(sectionToInsert));
}
}
}
}