// 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 Lesser 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser 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;
namespace Core.Common.Base.Data
{
///
/// Defines a range of values with inclusive bounds.
///
/// The variable type used to define the range in.
public class Range : IFormattable where T : IComparable
{
///
/// Creates a new instance of .
///
/// The minimum bound of the range.
/// The maximum bound of the range.
/// Thrown when is greater
/// then .
public Range(T min, T max)
{
if (min.CompareTo(max) > 0)
{
throw new ArgumentException("Minimum must be smaller or equal to Maximum.", nameof(min));
}
Minimum = min;
Maximum = max;
}
///
/// Gets the minimum bound of the range.
///
public T Minimum { get; }
///
/// Gets the maximum bound of the range.
///
public T Maximum { get; }
///
/// Checks if an value falls within the inclusive bounds of the range.
///
/// The value to check.
/// true if the value falls within the inclusive bounds, or false otherwise.
public bool InRange(T value)
{
return Minimum.CompareTo(value) <= 0 && Maximum.CompareTo(value) >= 0;
}
public override string ToString()
{
return $"[{Minimum}, {Maximum}]";
}
public string ToString(string format, IFormatProvider formatProvider)
{
var formattableMinimum = Minimum as IFormattable;
if (formattableMinimum != null)
{
var formattableMaximum = (IFormattable) Maximum;
return $"[{formattableMinimum.ToString(format, formatProvider)}, {formattableMaximum.ToString(format, formatProvider)}]";
}
return ToString();
}
}
}