// 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.MacroStabilityInwards.Primitives.Properties; namespace Ringtoets.MacroStabilityInwards.Primitives { /// /// This class represents a soil profile, which was imported for use in a macro stability inwards calculation. /// public class MacroStabilityInwardsSoilProfile2D { private MacroStabilityInwardsSoilLayer2D[] layers; /// /// Creates a new instance ofL , with the given and . /// A new collection is created for and used in the . /// /// The name 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. /// Identifier of the profile. /// Thrown when contains no layers. /// Thrown when is null. public MacroStabilityInwardsSoilProfile2D(string name, IEnumerable layers, SoilProfileType sourceProfileType, long soilProfileId) { Name = name; Layers = layers; SoilProfileType = sourceProfileType; MacroStabilityInwardsSoilProfileId = soilProfileId; } /// /// Gets the database identifier of the . /// public long MacroStabilityInwardsSoilProfileId { get; } /// /// Gets the name of . /// public string Name { get; } /// /// Gets an 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.ToArray(); } } /// /// Gets the type of soil profile used as data source to build this instance. /// public SoilProfileType SoilProfileType { get; } public override string ToString() { return Name ?? string.Empty; } public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) { return false; } if (ReferenceEquals(this, obj)) { return true; } if (obj.GetType() != GetType()) { return false; } return Equals((MacroStabilityInwardsSoilProfile2D) obj); } public override int GetHashCode() { unchecked { int hashCode = layers?.GetHashCode() ?? 0; hashCode = (hashCode * 397) ^ (Name?.GetHashCode() ?? 0); hashCode = (hashCode * 397) ^ (int) SoilProfileType; return hashCode; } } private bool Equals(MacroStabilityInwardsSoilProfile2D other) { return AreLayersEqual(other.layers) && string.Equals(Name, other.Name) && SoilProfileType == other.SoilProfileType; } private bool AreLayersEqual(MacroStabilityInwardsSoilLayer2D[] otherLayers) { int layerCount = layers.Length; if (layerCount != otherLayers.Length) { return false; } for (var i = 0; i < layerCount; i++) { if (!layers[i].Equals(otherLayers[i])) { return false; } } return true; } /// /// Validates the given . A valid has layers. /// /// The collection of to validate. /// Thrown when is null. /// Thrown when contains no layers. private void ValidateLayersCollection(IEnumerable collection) { if (collection == null) { throw new ArgumentNullException(nameof(collection), string.Format(Resources.Error_Cannot_Construct_MacroStabilityInwardsSoilProfile_Without_Layers)); } if (!collection.Any()) { throw new ArgumentException(Resources.Error_Cannot_Construct_MacroStabilityInwardsSoilProfile_Without_Layers); } } } }