// 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.Generic; using System.Linq; using Ringtoets.Piping.Primitives.Properties; namespace Ringtoets.Piping.Primitives { /// /// This class represents a soil profile, which was imported for use in a piping calculation. /// public class PipingSoilProfile { private PipingSoilLayer[] layers; /// /// Creates a new instance of , with the given , /// and . /// A new collection is created for and used in the . /// /// The name of the profile. /// The bottom level of the profile. /// The collection of layers that should be part of the profile. /// The type of soil profile used as data source /// to build this instance. /// Thrown when contains no layers. /// Thrown when or /// is null. public PipingSoilProfile(string name, double bottom, IEnumerable layers, SoilProfileType soilProfileSourceType) { if (name == null) { throw new ArgumentNullException(nameof(name)); } Name = name; Bottom = bottom; Layers = layers; SoilProfileSourceType = soilProfileSourceType; } /// /// Gets the bottom level of the . /// public double Bottom { get; } /// /// Gets the name of . /// public string Name { get; } /// /// Gets an ordered (by , descending) of /// for the . /// /// Thrown when the value is null. /// Thrown when the value contains no layers. public IEnumerable Layers { get { return layers; } private set { ValidateLayersCollection(value); layers = value.OrderByDescending(l => l.Top).ToArray(); } } /// /// Gets the type of soil profile used as data source to build this instance. /// public SoilProfileType SoilProfileSourceType { get; } /// /// Gets the thickness of the given layer in the . /// Thickness of a layer is determined by its top and the top of the layer below it. /// /// The to determine the thickness of. /// The thickness of the . /// does not contain . public double GetLayerThickness(PipingSoilLayer layer) { IEnumerable layersOrderedByTopAscending = layers.Reverse(); double previousLevel = Bottom; foreach (PipingSoilLayer oLayer in layersOrderedByTopAscending) { if (ReferenceEquals(layer, oLayer)) { return layer.Top - previousLevel; } previousLevel = oLayer.Top; } throw new ArgumentException("Layer not found in profile."); } public override string ToString() { return Name; } public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) { return false; } if (ReferenceEquals(this, obj)) { return true; } if (GetType() != obj.GetType()) { return false; } return Equals((PipingSoilProfile) obj); } public override int GetHashCode() { unchecked { int hashCode = Bottom.GetHashCode(); hashCode = (hashCode * 397) ^ Name.GetHashCode(); hashCode = (hashCode * 397) ^ (int) SoilProfileSourceType; return hashCode; } } private bool Equals(PipingSoilProfile other) { return layers.SequenceEqual(other.layers) && Bottom.Equals(other.Bottom) && Name.Equals(other.Name) && SoilProfileSourceType == other.SoilProfileSourceType; } /// /// Validates the given . A valid has layers which /// all have values for which are greater than or equal to . /// /// The collection of to validate. /// Thrown when is null. /// Thrown when /// /// contains no layers /// contains a layer with the less than /// /// private void ValidateLayersCollection(IEnumerable collection) { if (collection == null) { throw new ArgumentNullException(nameof(collection), string.Format(Resources.Error_Cannot_Construct_PipingSoilProfile_Without_Layers)); } if (!collection.Any()) { throw new ArgumentException(Resources.Error_Cannot_Construct_PipingSoilProfile_Without_Layers); } if (collection.Any(l => l.Top < Bottom)) { throw new ArgumentException(Resources.PipingSoilProfile_Layers_Layer_top_below_profile_bottom); } } } }