mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-01-27 03:15:00 +00:00
Compare commits
69 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf8061046b | ||
|
|
201639b30b | ||
|
|
89f0214cce | ||
|
|
49f8f116fd | ||
|
|
352121e25c | ||
|
|
0d5ee31d61 | ||
|
|
ec7004e053 | ||
|
|
ea0bcec2c3 | ||
|
|
55bf458a2b | ||
|
|
3deec98f44 | ||
|
|
eb52b164bf | ||
|
|
fb8abe2aa4 | ||
|
|
f61305856f | ||
|
|
de00acbf79 | ||
|
|
5ffca5e7a3 | ||
|
|
2037c990b2 | ||
|
|
13a2493f4c | ||
|
|
09e0a20443 | ||
|
|
7870d2c375 | ||
|
|
7cf3d18127 | ||
|
|
f39b3897fc | ||
|
|
ab2fab0b3a | ||
|
|
9b69b2a712 | ||
|
|
0a1f637b45 | ||
|
|
e0ace12ef9 | ||
|
|
9f1541f843 | ||
|
|
b9f5349055 | ||
|
|
0c7f82835c | ||
|
|
41755cb472 | ||
|
|
238d6518ab | ||
|
|
89cde5c9a0 | ||
|
|
38204314e7 | ||
|
|
9e8cedfbfa | ||
|
|
52229c1472 | ||
|
|
38c24d42a7 | ||
|
|
cff1ec8d7e | ||
|
|
5a64a6c269 | ||
|
|
68d98021a5 | ||
|
|
0e16606d75 | ||
|
|
602f170091 | ||
|
|
aae8bc5323 | ||
|
|
11004085c5 | ||
|
|
6ca015a561 | ||
|
|
73b1c4819c | ||
|
|
f913af6efe | ||
|
|
099517df7d | ||
|
|
6a7b58f50f | ||
|
|
2273d5b3a1 | ||
|
|
aa17f29305 | ||
|
|
e65578584a | ||
|
|
96d2df8871 | ||
|
|
b4d82a855e | ||
|
|
83bc344ab3 | ||
|
|
0821dcb3ca | ||
|
|
b6f64bb9b2 | ||
|
|
6ae110a997 | ||
|
|
efdb48f2c3 | ||
|
|
9d02ee0022 | ||
|
|
8c0416f70e | ||
|
|
0f27d68edf | ||
|
|
f8fc68e08f | ||
|
|
fc4ae67e40 | ||
|
|
0f618198b5 | ||
|
|
9a6f197aa0 | ||
|
|
45ee3e70b0 | ||
|
|
b09f614ef5 | ||
|
|
027c9ffe82 | ||
|
|
dc1b60e629 | ||
|
|
091ad10068 |
55
CHANGELOG.md
55
CHANGELOG.md
@@ -6,6 +6,57 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [10.3.0] - 2020-01-20
|
||||
### Changed
|
||||
- Network/MAC/DNS address utils are now enumerating all adapter types
|
||||
- Ignoring Crestron ethernet parameters that say "Invalid Value"
|
||||
- Skipping network interfaces with an invalid adapter id
|
||||
|
||||
## [10.2.0] - 2019-12-04
|
||||
### Added
|
||||
- Added shim methods for finding closest DateTimes from a sequence
|
||||
|
||||
## [10.1.0] - 2019-11-18
|
||||
### Added
|
||||
- Added PathUtils methods for getting ProgramData paths
|
||||
- Added a method for determining if a URI is defaults
|
||||
- Added MaxOrDefault extension method for enumerables
|
||||
- Added a method for finding an item in a sorted list by a given predicate
|
||||
|
||||
### Changed
|
||||
- NullObject implements IComparable, fixes issues with null keys in ordered dictionaries
|
||||
- IcdSqliteConnection CreateFile method will create directories recursively
|
||||
|
||||
## [10.0.0] - 2019-10-07
|
||||
### Added
|
||||
- IcdEnvironment.GetUtcTime() to get UTC representaiton of current time.
|
||||
- Extension methods for determining if a sequence is in order
|
||||
- Overload for calculating the modulus of longs
|
||||
- Default implementation for AbstractGenericXmlConverter Instantiate method
|
||||
- Additional binary search extensions, now working for all ILists
|
||||
- Added NullObject as a means of having null keys in hash tables
|
||||
|
||||
### Changed
|
||||
- Potential fix for unhelpful exception messages coming from SafeCriticalSection.Execute
|
||||
- Small performance improvement when copying arrays
|
||||
|
||||
## [9.9.0] - 2019-09-16
|
||||
### Added
|
||||
- Added a method for converting 24 hour to 12 hour format
|
||||
- Added a method for determining if a culture uses 24 hour format
|
||||
- Added math util method for modulus
|
||||
- Added TimeSpan extension methods for cycling hours and minutes without modifying the day
|
||||
- Added a dictionary extension method for getting or adding a new value via func
|
||||
- Added CultureInfo extensions for converting between 12 hour and 24 hour time formatting
|
||||
- Added environment methods for setting the current date and time
|
||||
- Added BinarySearch extension method for all IList types
|
||||
- Added PathUtils methods to get ProgramData directory
|
||||
|
||||
### Changed
|
||||
- The Root Config path in Net Standard will now be the ICD.Connect folder in the current environments ProgramData directory
|
||||
- Fixed a bug where CultureInfo was not being cloned correctly
|
||||
- List AddSorted extensions now work for all IList types
|
||||
|
||||
## [9.8.0] - 2019-09-03
|
||||
### Added
|
||||
- Added Public API Properties to get the program install date based on the creation date of core dll file for NetStandard and SimplSharp
|
||||
@@ -98,6 +149,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
|
||||
- Better VC-4 support for IcdConsole
|
||||
- JSON refactoring for simpler deserialization
|
||||
|
||||
## [8.7.2] - 2019-10-29
|
||||
### Changed
|
||||
- Fixed a bug with PriorityQueue de-duplication where a new command would be inserted in the wrong position
|
||||
|
||||
## [8.7.1] - 2019-08-22
|
||||
### Changed
|
||||
- Fixed a bug with the IcdOrderedDict index setter that was creating additional values
|
||||
|
||||
@@ -133,8 +133,8 @@ namespace ICD.Common.Utils.Tests.Collections
|
||||
};
|
||||
|
||||
Assert.AreEqual(1, dequeue[0]);
|
||||
Assert.AreEqual(4, dequeue[1]);
|
||||
Assert.AreEqual(3, dequeue[2]);
|
||||
Assert.AreEqual(3, dequeue[1]);
|
||||
Assert.AreEqual(4, dequeue[2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
|
||||
17
ICD.Common.Utils.Tests/DateTimeUtilsTest.cs
Normal file
17
ICD.Common.Utils.Tests/DateTimeUtilsTest.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace ICD.Common.Utils.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public sealed class DateTimeUtilsTest
|
||||
{
|
||||
[TestCase(1, 1)]
|
||||
[TestCase(0, 12)]
|
||||
[TestCase(12, 12)]
|
||||
[TestCase(23, 11)]
|
||||
public void To12HourTest(int hour, int expected)
|
||||
{
|
||||
Assert.AreEqual(expected, DateTimeUtils.To12Hour(hour));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,16 +3,16 @@
|
||||
namespace ICD.Common.Utils.Tests.Extensions
|
||||
{
|
||||
[TestFixture]
|
||||
public sealed class DateTimeExtensionsTest
|
||||
{
|
||||
[Test]
|
||||
public static void ToShortTimeStringTest()
|
||||
{
|
||||
Assert.Inconclusive();
|
||||
}
|
||||
public sealed class DateTimeExtensionsTest
|
||||
{
|
||||
[Test]
|
||||
public void ToShortTimeStringTest()
|
||||
{
|
||||
Assert.Inconclusive();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public static void ToLongTimeStringWithMillisecondsTest()
|
||||
public void ToLongTimeStringWithMillisecondsTest()
|
||||
{
|
||||
Assert.Inconclusive();
|
||||
}
|
||||
|
||||
@@ -284,6 +284,15 @@ namespace ICD.Common.Utils.Tests.Extensions
|
||||
Assert.AreEqual(5, items.Length);
|
||||
}
|
||||
|
||||
[TestCase(true)]
|
||||
[TestCase(true, 1, 2, 3, 4)]
|
||||
[TestCase(true, 1, 2, 2, 3, 4)]
|
||||
[TestCase(false, 4, 3, 2, 1)]
|
||||
public void AreOrderedTest(bool expected, params int[] items)
|
||||
{
|
||||
Assert.AreEqual(expected, items.AreOrdered());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void OrderTest()
|
||||
{
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ICD.Common.Utils.Extensions;
|
||||
using NUnit.Framework;
|
||||
|
||||
@@ -9,14 +8,14 @@ namespace ICD.Common.Utils.Tests.Extensions
|
||||
public sealed class ListExtensionsTest
|
||||
{
|
||||
[Test]
|
||||
public void AddSortedTest()
|
||||
public void InsertSortedTest()
|
||||
{
|
||||
List<int> testList = new List<int>();
|
||||
|
||||
Assert.AreEqual(0, testList.AddSorted(2));
|
||||
Assert.AreEqual(1, testList.AddSorted(3));
|
||||
Assert.AreEqual(0, testList.AddSorted(1));
|
||||
Assert.AreEqual(1, testList.AddSorted(2));
|
||||
Assert.AreEqual(0, testList.InsertSorted(2));
|
||||
Assert.AreEqual(1, testList.InsertSorted(3));
|
||||
Assert.AreEqual(0, testList.InsertSorted(1));
|
||||
Assert.AreEqual(1, testList.InsertSorted(2));
|
||||
|
||||
Assert.AreEqual(4, testList.Count);
|
||||
Assert.AreEqual(1, testList[0]);
|
||||
@@ -26,15 +25,15 @@ namespace ICD.Common.Utils.Tests.Extensions
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddSortedComparerTest()
|
||||
public void InsertSortedComparerTest()
|
||||
{
|
||||
List<int> testList = new List<int>();
|
||||
IComparer<int> comparer = new InverseComparer();
|
||||
|
||||
Assert.AreEqual(0, testList.AddSorted(2, comparer));
|
||||
Assert.AreEqual(0, testList.AddSorted(3, comparer));
|
||||
Assert.AreEqual(2, testList.AddSorted(1, comparer));
|
||||
Assert.AreEqual(1, testList.AddSorted(2, comparer));
|
||||
Assert.AreEqual(0, testList.InsertSorted(2, comparer));
|
||||
Assert.AreEqual(0, testList.InsertSorted(3, comparer));
|
||||
Assert.AreEqual(2, testList.InsertSorted(1, comparer));
|
||||
Assert.AreEqual(1, testList.InsertSorted(2, comparer));
|
||||
|
||||
Assert.AreEqual(4, testList.Count);
|
||||
Assert.AreEqual(3, testList[0]);
|
||||
@@ -43,82 +42,6 @@ namespace ICD.Common.Utils.Tests.Extensions
|
||||
Assert.AreEqual(1, testList[3]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PadRightTest()
|
||||
{
|
||||
List<int> testList = new List<int>();
|
||||
testList.PadRight(10);
|
||||
|
||||
Assert.AreEqual(10, testList.Count);
|
||||
Assert.AreEqual(0, testList.Sum());
|
||||
|
||||
testList = new List<int>
|
||||
{
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5
|
||||
};
|
||||
|
||||
testList.PadRight(10);
|
||||
|
||||
Assert.AreEqual(10, testList.Count);
|
||||
Assert.AreEqual(15, testList.Sum());
|
||||
|
||||
testList = new List<int>
|
||||
{
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5
|
||||
};
|
||||
|
||||
testList.PadRight(1);
|
||||
|
||||
Assert.AreEqual(5, testList.Count);
|
||||
Assert.AreEqual(15, testList.Sum());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PadRightDefaultTest()
|
||||
{
|
||||
List<int> testList = new List<int>();
|
||||
testList.PadRight(10, 1);
|
||||
|
||||
Assert.AreEqual(10, testList.Count);
|
||||
Assert.AreEqual(10, testList.Sum());
|
||||
|
||||
testList = new List<int>
|
||||
{
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5
|
||||
};
|
||||
|
||||
testList.PadRight(10, 1);
|
||||
|
||||
Assert.AreEqual(10, testList.Count);
|
||||
Assert.AreEqual(20, testList.Sum());
|
||||
|
||||
testList = new List<int>
|
||||
{
|
||||
1,
|
||||
2,
|
||||
3,
|
||||
4,
|
||||
5
|
||||
};
|
||||
|
||||
testList.PadRight(1, 1);
|
||||
|
||||
Assert.AreEqual(5, testList.Count);
|
||||
Assert.AreEqual(15, testList.Sum());
|
||||
}
|
||||
|
||||
private sealed class InverseComparer : IComparer<int>
|
||||
{
|
||||
public int Compare(int x, int y)
|
||||
|
||||
43
ICD.Common.Utils.Tests/Extensions/TimeSpanExtensionsTest.cs
Normal file
43
ICD.Common.Utils.Tests/Extensions/TimeSpanExtensionsTest.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using ICD.Common.Utils.Extensions;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace ICD.Common.Utils.Tests.Extensions
|
||||
{
|
||||
[TestFixture]
|
||||
public sealed class TimeSpanExtensionsTest
|
||||
{
|
||||
[Test]
|
||||
public void ToReadableStringTest()
|
||||
{
|
||||
Assert.Inconclusive();
|
||||
}
|
||||
|
||||
[TestCase(0, 0, 0)]
|
||||
[TestCase(12, 1, 13)]
|
||||
[TestCase(23, 1, 0)]
|
||||
[TestCase(6, -12, 18)]
|
||||
public void AddHoursAndWrapTest(int hours, int addHours, int expectedHours)
|
||||
{
|
||||
Assert.AreEqual(expectedHours, new TimeSpan(hours, 0, 0).AddHoursAndWrap(addHours).Hours);
|
||||
}
|
||||
|
||||
[TestCase(0, 0, 0)]
|
||||
[TestCase(12, 1, 13)]
|
||||
[TestCase(23, 1, 12)]
|
||||
[TestCase(6, -12, 6)]
|
||||
public void AddHoursAndWrap12Hour(int hours, int addHours, int expectedHours)
|
||||
{
|
||||
Assert.AreEqual(expectedHours, new TimeSpan(hours, 0, 0).AddHoursAndWrap12Hour(addHours).Hours);
|
||||
}
|
||||
|
||||
[TestCase(0, 0, 0)]
|
||||
[TestCase(30, 1, 31)]
|
||||
[TestCase(59, 1, 0)]
|
||||
[TestCase(30, -60, 30)]
|
||||
public void AddMinutesAndWrap(int minutes, int addMinutes, int expectedMinutes)
|
||||
{
|
||||
Assert.AreEqual(expectedMinutes, new TimeSpan(0, minutes, 0).AddMinutesAndWrap(addMinutes).Minutes);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
|
||||
<PackageReference Include="NUnit" Version="3.12.0" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="3.15.0" />
|
||||
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
using NUnit.Framework;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ICD.Common.Properties;
|
||||
|
||||
namespace ICD.Common.Utils.Tests
|
||||
{
|
||||
[TestFixture, UsedImplicitly]
|
||||
[TestFixture]
|
||||
public sealed class MathUtilsTest
|
||||
{
|
||||
[Test, UsedImplicitly]
|
||||
[Test]
|
||||
public void ClampTest()
|
||||
{
|
||||
Assert.AreEqual(MathUtils.Clamp(-10, 0, 0), 0);
|
||||
@@ -24,7 +23,7 @@ namespace ICD.Common.Utils.Tests
|
||||
Assert.AreEqual(MathUtils.Clamp(20, 10, 10), 10);
|
||||
}
|
||||
|
||||
[Test, UsedImplicitly]
|
||||
[Test]
|
||||
public void MapRangeTest()
|
||||
{
|
||||
Assert.AreEqual(5, MathUtils.MapRange(-100, 100, 0, 10, 0));
|
||||
@@ -40,7 +39,7 @@ namespace ICD.Common.Utils.Tests
|
||||
Assert.AreEqual(100, MathUtils.MapRange(0, 10, 0, 100, 10));
|
||||
}
|
||||
|
||||
[Test, UsedImplicitly]
|
||||
[Test]
|
||||
public void GetRangesTest()
|
||||
{
|
||||
IEnumerable<int> values = new [] { 1, 3, 5, 6, 7, 8, 9, 10, 12 };
|
||||
@@ -61,11 +60,21 @@ namespace ICD.Common.Utils.Tests
|
||||
Assert.AreEqual(12, ranges[3][1]);
|
||||
}
|
||||
|
||||
[Test, UsedImplicitly]
|
||||
[Test]
|
||||
public void RoundToNearestTest()
|
||||
{
|
||||
IEnumerable<int> values = new [] { 0, 15, 30, 45 };
|
||||
Assert.AreEqual(15, MathUtils.RoundToNearest(21, values));
|
||||
}
|
||||
|
||||
[TestCase(10, 10, 0)]
|
||||
[TestCase(-10, 10, 0)]
|
||||
[TestCase(9, 3, 0)]
|
||||
[TestCase(3, 2, 1)]
|
||||
[TestCase(-3, 2, 1)]
|
||||
public void ModulusTest(int value, int mod, int expected)
|
||||
{
|
||||
Assert.AreEqual(expected, MathUtils.Modulus(value, mod));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using ICD.Common.Properties;
|
||||
using ICD.Common.Utils.Extensions;
|
||||
|
||||
namespace ICD.Common.Utils.Attributes
|
||||
@@ -285,12 +286,37 @@ namespace ICD.Common.Utils.Attributes
|
||||
|
||||
#region Methods
|
||||
|
||||
/// <summary>
|
||||
/// Remaps the numeric value into the min-max range of the target numeric type.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
[NotNull]
|
||||
public static object Remap([NotNull] object value, [NotNull] Type type)
|
||||
{
|
||||
if (value == null)
|
||||
throw new ArgumentNullException("value");
|
||||
|
||||
if (type == null)
|
||||
throw new ArgumentNullException("type");
|
||||
|
||||
if (!type.IsNumeric())
|
||||
throw new ArgumentException("Target type is not numeric");
|
||||
|
||||
if (!value.GetType().IsNumeric())
|
||||
throw new ArgumentException("Source value is not numeric");
|
||||
|
||||
double intermediate = RemapToDouble(value);
|
||||
return RemapFromDouble(intermediate, type);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remaps the given numeric value from its min/max range into double min/max range.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public static double RemapToDouble(object value)
|
||||
public static double RemapToDouble([NotNull] object value)
|
||||
{
|
||||
if (value == null)
|
||||
throw new ArgumentNullException("value");
|
||||
@@ -308,7 +334,8 @@ namespace ICD.Common.Utils.Attributes
|
||||
/// <param name="value"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public static object RemapFromDouble(double value, Type type)
|
||||
[NotNull]
|
||||
public static object RemapFromDouble(double value, [NotNull] Type type)
|
||||
{
|
||||
if (type == null)
|
||||
throw new ArgumentNullException("type");
|
||||
@@ -320,13 +347,41 @@ namespace ICD.Common.Utils.Attributes
|
||||
return remap(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remaps the given numeric value to the defined min/max.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
[NotNull]
|
||||
public object RemapMinMax([NotNull] object value)
|
||||
{
|
||||
if (value == null)
|
||||
throw new ArgumentNullException("value");
|
||||
|
||||
if (!value.GetType().IsNumeric())
|
||||
throw new ArgumentException("Source value is not numeric");
|
||||
|
||||
double sourceMin = GetMinAsDouble(value.GetType());
|
||||
double sourceMax = GetMaxAsDouble(value.GetType());
|
||||
|
||||
double targetMin = Convert.ToDouble(Min);
|
||||
double targetMax = Convert.ToDouble(Max);
|
||||
|
||||
double doubleValue = Convert.ToDouble(value);
|
||||
|
||||
double remapped = MathUtils.MapRange(sourceMin, sourceMax, targetMin, targetMax, doubleValue);
|
||||
|
||||
return Convert.ChangeType(remapped, value.GetType(), CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clamps the given numeric value into the valid ranges of the target numeric type.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public static object Clamp(object value, Type type)
|
||||
[NotNull]
|
||||
public static object Clamp([NotNull] object value, [NotNull] Type type)
|
||||
{
|
||||
if (value == null)
|
||||
throw new ArgumentNullException("value");
|
||||
@@ -352,7 +407,7 @@ namespace ICD.Common.Utils.Attributes
|
||||
/// <param name="value"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public static double Clamp(double value, Type type)
|
||||
public static double Clamp(double value, [NotNull] Type type)
|
||||
{
|
||||
if (type == null)
|
||||
throw new ArgumentNullException("type");
|
||||
@@ -364,37 +419,14 @@ namespace ICD.Common.Utils.Attributes
|
||||
return clamp(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remaps the numeric value into the min-max range of the target numeric type.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public static object Remap(object value, Type type)
|
||||
{
|
||||
if (value == null)
|
||||
throw new ArgumentNullException("value");
|
||||
|
||||
if (type == null)
|
||||
throw new ArgumentNullException("type");
|
||||
|
||||
if (!type.IsNumeric())
|
||||
throw new ArgumentException("Target type is not numeric");
|
||||
|
||||
if (!value.GetType().IsNumeric())
|
||||
throw new ArgumentException("Source value is not numeric");
|
||||
|
||||
double intermediate = RemapToDouble(value);
|
||||
return RemapFromDouble(intermediate, type);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clamps the given numeric value to the defined min/max then remaps to the target numeric type.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
public object ClampMinMaxThenRemap(object value, Type type)
|
||||
[NotNull]
|
||||
public object ClampMinMaxThenRemap([NotNull] object value, [NotNull] Type type)
|
||||
{
|
||||
if (value == null)
|
||||
throw new ArgumentNullException("value");
|
||||
@@ -418,33 +450,12 @@ namespace ICD.Common.Utils.Attributes
|
||||
return Convert.ChangeType(remapped, value.GetType(), CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remaps the given numeric value to the defined min/max.
|
||||
/// </summary>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
public object RemapMinMax(object value)
|
||||
{
|
||||
if (value == null)
|
||||
throw new ArgumentNullException("value");
|
||||
#endregion
|
||||
|
||||
if (!value.GetType().IsNumeric())
|
||||
throw new ArgumentException("Source value is not numeric");
|
||||
#region Private Methods
|
||||
|
||||
double sourceMin = GetMinAsDouble(value.GetType());
|
||||
double sourceMax = GetMaxAsDouble(value.GetType());
|
||||
|
||||
double targetMin = Convert.ToDouble(Min);
|
||||
double targetMax = Convert.ToDouble(Max);
|
||||
|
||||
double doubleValue = Convert.ToDouble(value);
|
||||
|
||||
double remapped = MathUtils.MapRange(sourceMin, sourceMax, targetMin, targetMax, doubleValue);
|
||||
|
||||
return Convert.ChangeType(remapped, value.GetType(), CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
private object RemapMinMax(object value, Type type)
|
||||
[NotNull]
|
||||
private object RemapMinMax([NotNull] object value, [NotNull] Type type)
|
||||
{
|
||||
if (value == null)
|
||||
throw new ArgumentNullException("value");
|
||||
@@ -471,16 +482,12 @@ namespace ICD.Common.Utils.Attributes
|
||||
return Convert.ChangeType(remapped, type, CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
/// <summary>
|
||||
/// Gets the min value for the given numeric type as a double.
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
private static double GetMinAsDouble(Type type)
|
||||
private static double GetMinAsDouble([NotNull] Type type)
|
||||
{
|
||||
if (type == null)
|
||||
throw new ArgumentNullException("type");
|
||||
@@ -500,7 +507,7 @@ namespace ICD.Common.Utils.Attributes
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <returns></returns>
|
||||
private static double GetMaxAsDouble(Type type)
|
||||
private static double GetMaxAsDouble([NotNull] Type type)
|
||||
{
|
||||
if (type == null)
|
||||
throw new ArgumentNullException("type");
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using ICD.Common.Properties;
|
||||
|
||||
namespace ICD.Common.Utils.Collections
|
||||
{
|
||||
@@ -20,8 +21,10 @@ namespace ICD.Common.Utils.Collections
|
||||
|
||||
public bool IsReadOnly { get { return false; } }
|
||||
|
||||
[NotNull]
|
||||
public ICollection<TKey> Keys { get { return m_KeyToValue.Keys; } }
|
||||
|
||||
[NotNull]
|
||||
public ICollection<TValue> Values { get { return m_ValueToKey.Keys; } }
|
||||
|
||||
#endregion
|
||||
@@ -30,21 +33,20 @@ namespace ICD.Common.Utils.Collections
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
public BiDictionary()
|
||||
: this(null)
|
||||
{
|
||||
m_KeyToValue = new Dictionary<TKey, TValue>();
|
||||
m_ValueToKey = new Dictionary<TValue, TKey>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="dict"></param>
|
||||
public BiDictionary(Dictionary<TKey, TValue> dict)
|
||||
public BiDictionary([NotNull] Dictionary<TKey, TValue> dict)
|
||||
: this()
|
||||
{
|
||||
m_KeyToValue = new Dictionary<TKey, TValue>();
|
||||
m_ValueToKey = new Dictionary<TValue, TKey>();
|
||||
|
||||
if (dict == null)
|
||||
return;
|
||||
throw new ArgumentNullException("dict");
|
||||
|
||||
foreach (KeyValuePair<TKey, TValue> kvp in dict)
|
||||
Add(kvp.Key, kvp.Value);
|
||||
@@ -58,24 +60,31 @@ namespace ICD.Common.Utils.Collections
|
||||
m_ValueToKey.Clear();
|
||||
}
|
||||
|
||||
public bool ContainsKey(TKey key)
|
||||
public bool ContainsKey([NotNull] TKey key)
|
||||
{
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
return m_KeyToValue.ContainsKey(key);
|
||||
}
|
||||
|
||||
public bool ContainsValue(TValue value)
|
||||
public bool ContainsValue([NotNull] TValue value)
|
||||
{
|
||||
return m_ValueToKey.ContainsKey(value);
|
||||
}
|
||||
|
||||
public void Add(TKey key, TValue value)
|
||||
public void Add([NotNull] TKey key, [NotNull] TValue value)
|
||||
{
|
||||
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (value == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("value");
|
||||
|
||||
if (ContainsKey(key))
|
||||
@@ -88,14 +97,16 @@ namespace ICD.Common.Utils.Collections
|
||||
m_ValueToKey.Add(value, key);
|
||||
}
|
||||
|
||||
public void Set(TKey key, TValue value)
|
||||
public void Set([NotNull] TKey key, [NotNull] TValue value)
|
||||
{
|
||||
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (value == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("value");
|
||||
|
||||
RemoveKey(key);
|
||||
@@ -104,18 +115,35 @@ namespace ICD.Common.Utils.Collections
|
||||
Add(key, value);
|
||||
}
|
||||
|
||||
public TKey GetKey(TValue value)
|
||||
[NotNull]
|
||||
public TKey GetKey([NotNull] TValue value)
|
||||
{
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (value == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("value");
|
||||
|
||||
return m_ValueToKey[value];
|
||||
}
|
||||
|
||||
public TValue GetValue(TKey key)
|
||||
[NotNull]
|
||||
public TValue GetValue([NotNull] TKey key)
|
||||
{
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
return m_KeyToValue[key];
|
||||
}
|
||||
|
||||
public bool RemoveKey(TKey key)
|
||||
public bool RemoveKey([NotNull] TKey key)
|
||||
{
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
if (!ContainsKey(key))
|
||||
return false;
|
||||
|
||||
@@ -127,8 +155,13 @@ namespace ICD.Common.Utils.Collections
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool RemoveValue(TValue value)
|
||||
public bool RemoveValue([NotNull] TValue value)
|
||||
{
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (value == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("value");
|
||||
|
||||
if (!ContainsValue(value))
|
||||
return false;
|
||||
|
||||
@@ -137,13 +170,23 @@ namespace ICD.Common.Utils.Collections
|
||||
return RemoveKey(key);
|
||||
}
|
||||
|
||||
public bool TryGetValue(TKey key, out TValue value)
|
||||
public bool TryGetValue([NotNull] TKey key, out TValue value)
|
||||
{
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
return m_KeyToValue.TryGetValue(key, out value);
|
||||
}
|
||||
|
||||
public bool TryGetKey(TValue value, out TKey key)
|
||||
public bool TryGetKey([NotNull] TValue value, out TKey key)
|
||||
{
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (value == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("value");
|
||||
|
||||
return m_ValueToKey.TryGetValue(value, out key);
|
||||
}
|
||||
|
||||
@@ -151,9 +194,10 @@ namespace ICD.Common.Utils.Collections
|
||||
|
||||
#region IDictionary
|
||||
|
||||
TValue IDictionary<TKey, TValue>.this[TKey key] { get { return GetValue(key); } set { Set(key, value); } }
|
||||
[NotNull]
|
||||
TValue IDictionary<TKey, TValue>.this[[NotNull] TKey key] { get { return GetValue(key); } set { Set(key, value); } }
|
||||
|
||||
bool IDictionary<TKey, TValue>.Remove(TKey key)
|
||||
bool IDictionary<TKey, TValue>.Remove([NotNull] TKey key)
|
||||
{
|
||||
return RemoveKey(key);
|
||||
}
|
||||
@@ -177,7 +221,7 @@ namespace ICD.Common.Utils.Collections
|
||||
return RemoveKey(item.Key);
|
||||
}
|
||||
|
||||
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
|
||||
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo([NotNull] KeyValuePair<TKey, TValue>[] array, int arrayIndex)
|
||||
{
|
||||
(m_KeyToValue as IDictionary<TKey, TValue>).CopyTo(array, arrayIndex);
|
||||
}
|
||||
@@ -186,11 +230,13 @@ namespace ICD.Common.Utils.Collections
|
||||
|
||||
#region IEnumerable
|
||||
|
||||
[NotNull]
|
||||
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
|
||||
{
|
||||
return m_KeyToValue.GetEnumerator();
|
||||
}
|
||||
|
||||
[NotNull]
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
|
||||
@@ -49,7 +49,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="items"></param>
|
||||
public IcdHashSet(IEnumerable<T> items)
|
||||
public IcdHashSet([NotNull] IEnumerable<T> items)
|
||||
: this(EqualityComparer<T>.Default, items)
|
||||
{
|
||||
}
|
||||
@@ -58,7 +58,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="comparer"></param>
|
||||
public IcdHashSet(IEqualityComparer<T> comparer)
|
||||
public IcdHashSet([NotNull] IEqualityComparer<T> comparer)
|
||||
: this(comparer, Enumerable.Empty<T>())
|
||||
{
|
||||
}
|
||||
@@ -68,7 +68,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// </summary>
|
||||
/// <param name="comparer"></param>
|
||||
/// <param name="items"></param>
|
||||
public IcdHashSet(IEqualityComparer<T> comparer, IEnumerable<T> items)
|
||||
public IcdHashSet([NotNull] IEqualityComparer<T> comparer, [NotNull] IEnumerable<T> items)
|
||||
{
|
||||
if (comparer == null)
|
||||
throw new ArgumentNullException("comparer");
|
||||
@@ -91,7 +91,8 @@ namespace ICD.Common.Utils.Collections
|
||||
/// <param name="set"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public IcdHashSet<T> Union(IEnumerable<T> set)
|
||||
[NotNull]
|
||||
public IcdHashSet<T> Union([NotNull] IEnumerable<T> set)
|
||||
{
|
||||
if (set == null)
|
||||
throw new ArgumentNullException("set");
|
||||
@@ -108,7 +109,8 @@ namespace ICD.Common.Utils.Collections
|
||||
/// <param name="set"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public IcdHashSet<T> Subtract(IEnumerable<T> set)
|
||||
[NotNull]
|
||||
public IcdHashSet<T> Subtract([NotNull] IEnumerable<T> set)
|
||||
{
|
||||
if (set == null)
|
||||
throw new ArgumentNullException("set");
|
||||
@@ -127,7 +129,8 @@ namespace ICD.Common.Utils.Collections
|
||||
/// <param name="set"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public IcdHashSet<T> Intersection(IEnumerable<T> set)
|
||||
[NotNull]
|
||||
public IcdHashSet<T> Intersection([NotNull] IEnumerable<T> set)
|
||||
{
|
||||
if (set == null)
|
||||
throw new ArgumentNullException("set");
|
||||
@@ -146,7 +149,8 @@ namespace ICD.Common.Utils.Collections
|
||||
/// <param name="set"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public IcdHashSet<T> NonIntersection(IEnumerable<T> set)
|
||||
[NotNull]
|
||||
public IcdHashSet<T> NonIntersection([NotNull] IEnumerable<T> set)
|
||||
{
|
||||
if (set == null)
|
||||
throw new ArgumentNullException("set");
|
||||
@@ -170,7 +174,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// <param name="set"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public bool IsSubsetOf(IcdHashSet<T> set)
|
||||
public bool IsSubsetOf([NotNull] IcdHashSet<T> set)
|
||||
{
|
||||
if (set == null)
|
||||
throw new ArgumentNullException("set");
|
||||
@@ -184,7 +188,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// <param name="set"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public bool IsProperSubsetOf(IcdHashSet<T> set)
|
||||
public bool IsProperSubsetOf([NotNull] IcdHashSet<T> set)
|
||||
{
|
||||
if (set == null)
|
||||
throw new ArgumentNullException("set");
|
||||
@@ -198,7 +202,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// <param name="set"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public bool IsSupersetOf(IcdHashSet<T> set)
|
||||
public bool IsSupersetOf([NotNull] IcdHashSet<T> set)
|
||||
{
|
||||
if (set == null)
|
||||
throw new ArgumentNullException("set");
|
||||
@@ -212,7 +216,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// <param name="set"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public bool IsProperSupersetOf(IcdHashSet<T> set)
|
||||
public bool IsProperSupersetOf([NotNull] IcdHashSet<T> set)
|
||||
{
|
||||
if (set == null)
|
||||
throw new ArgumentNullException("set");
|
||||
@@ -226,7 +230,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// <param name="set"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public bool SetEquals(IcdHashSet<T> set)
|
||||
public bool SetEquals([NotNull] IcdHashSet<T> set)
|
||||
{
|
||||
if (set == null)
|
||||
throw new ArgumentNullException("set");
|
||||
@@ -243,7 +247,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
public bool Add(T item)
|
||||
public bool Add([NotNull] T item)
|
||||
{
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (item == null)
|
||||
@@ -261,7 +265,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// Adds the item to the collection.
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
void ICollection<T>.Add(T item)
|
||||
void ICollection<T>.Add([NotNull] T item)
|
||||
{
|
||||
Add(item);
|
||||
}
|
||||
@@ -270,7 +274,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// Adds each of the items in the sequence to the collection.
|
||||
/// </summary>
|
||||
/// <param name="items"></param>
|
||||
public void AddRange(IEnumerable<T> items)
|
||||
public void AddRange([NotNull] IEnumerable<T> items)
|
||||
{
|
||||
if (items == null)
|
||||
throw new ArgumentNullException("items");
|
||||
@@ -293,7 +297,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
public bool Contains(T item)
|
||||
public bool Contains([NotNull] T item)
|
||||
{
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (item == null)
|
||||
@@ -307,8 +311,11 @@ namespace ICD.Common.Utils.Collections
|
||||
/// Copies the items of the <see cref="T:System.Collections.Generic.ICollection`1"/> to an <see cref="T:System.Array"/>, starting at a particular <see cref="T:System.Array"/> index.
|
||||
/// </summary>
|
||||
/// <param name="array">The one-dimensional <see cref="T:System.Array"/> that is the destination of the items copied from <see cref="T:System.Collections.Generic.ICollection`1"/>. The <see cref="T:System.Array"/> must have zero-based indexing.</param><param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param><exception cref="T:System.ArgumentNullException"><paramref name="array"/> is null.</exception><exception cref="T:System.ArgumentOutOfRangeException"><paramref name="arrayIndex"/> is less than 0.</exception><exception cref="T:System.ArgumentException"><paramref name="array"/> is multidimensional.-or-<paramref name="arrayIndex"/> is equal to or greater than the length of <paramref name="array"/>.-or-The number of items in the source <see cref="T:System.Collections.Generic.ICollection`1"/> is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/>.-or-Type T cannot be cast automatically to the type of the destination <paramref name="array"/>.</exception>
|
||||
public void CopyTo(T[] array, int arrayIndex)
|
||||
public void CopyTo([NotNull] T[] array, int arrayIndex)
|
||||
{
|
||||
if (array == null)
|
||||
throw new ArgumentNullException("array");
|
||||
|
||||
m_Dict.Keys.CopyTo(array, arrayIndex);
|
||||
}
|
||||
|
||||
@@ -319,7 +326,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// true if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>.
|
||||
/// </returns>
|
||||
/// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param><exception cref="T:System.NotSupportedException">The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.</exception>
|
||||
public bool Remove(T item)
|
||||
public bool Remove([NotNull] T item)
|
||||
{
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (item == null)
|
||||
@@ -333,7 +340,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// Removes each of the items in the sequence from the collection.
|
||||
/// </summary>
|
||||
/// <param name="items"></param>
|
||||
public void RemoveRange(IEnumerable<T> items)
|
||||
public void RemoveRange([NotNull] IEnumerable<T> items)
|
||||
{
|
||||
if (items == null)
|
||||
throw new ArgumentNullException("items");
|
||||
@@ -352,6 +359,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
|
||||
/// </returns>
|
||||
/// <filterpriority>1</filterpriority>
|
||||
[NotNull]
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
return m_Dict.Keys.GetEnumerator();
|
||||
@@ -364,6 +372,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
|
||||
/// </returns>
|
||||
/// <filterpriority>2</filterpriority>
|
||||
[NotNull]
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ICD.Common.Properties;
|
||||
using ICD.Common.Utils.Extensions;
|
||||
|
||||
namespace ICD.Common.Utils.Collections
|
||||
@@ -19,11 +20,14 @@ namespace ICD.Common.Utils.Collections
|
||||
|
||||
public bool IsReadOnly { get { return false; } }
|
||||
|
||||
[NotNull]
|
||||
public ICollection<TKey> Keys { get { return m_OrderedKeys; } }
|
||||
|
||||
[NotNull]
|
||||
public ICollection<TValue> Values { get { return m_ValuesOrderedByKey; } }
|
||||
|
||||
public TValue this[TKey key]
|
||||
[CanBeNull]
|
||||
public TValue this[[NotNull] TKey key]
|
||||
{
|
||||
get { return m_Dictionary[key]; }
|
||||
set
|
||||
@@ -51,7 +55,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="comparer"></param>
|
||||
public IcdOrderedDictionary(IComparer<TKey> comparer)
|
||||
public IcdOrderedDictionary([NotNull] IComparer<TKey> comparer)
|
||||
: this(comparer, EqualityComparer<TKey>.Default)
|
||||
{
|
||||
}
|
||||
@@ -61,7 +65,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// </summary>
|
||||
/// <param name="comparer"></param>
|
||||
/// <param name="equalityComparer"></param>
|
||||
public IcdOrderedDictionary(IComparer<TKey> comparer, IEqualityComparer<TKey> equalityComparer)
|
||||
public IcdOrderedDictionary([NotNull] IComparer<TKey> comparer, [NotNull] IEqualityComparer<TKey> equalityComparer)
|
||||
{
|
||||
if (comparer == null)
|
||||
throw new ArgumentNullException("comparer");
|
||||
@@ -79,7 +83,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="dictionary"></param>
|
||||
public IcdOrderedDictionary(IEnumerable<KeyValuePair<TKey, TValue>> dictionary)
|
||||
public IcdOrderedDictionary([NotNull] IEnumerable<KeyValuePair<TKey, TValue>> dictionary)
|
||||
: this()
|
||||
{
|
||||
if (dictionary == null)
|
||||
@@ -91,22 +95,24 @@ namespace ICD.Common.Utils.Collections
|
||||
|
||||
#region Methods
|
||||
|
||||
[NotNull]
|
||||
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator()
|
||||
{
|
||||
return m_OrderedKeys.Select(k => new KeyValuePair<TKey, TValue>(k, m_Dictionary[k]))
|
||||
.GetEnumerator();
|
||||
}
|
||||
|
||||
public void Add(TKey key, TValue value)
|
||||
public void Add([NotNull] TKey key, [CanBeNull] TValue value)
|
||||
{
|
||||
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
if (m_Dictionary.ContainsKey(key))
|
||||
throw new ArgumentOutOfRangeException("key", "An item with the same key has already been added.");
|
||||
|
||||
int index = m_OrderedKeys.AddSorted(key, m_Comparer);
|
||||
int index = m_OrderedKeys.InsertSorted(key, m_Comparer);
|
||||
m_ValuesOrderedByKey.Insert(index, value);
|
||||
|
||||
m_Dictionary[key] = value;
|
||||
@@ -119,15 +125,21 @@ namespace ICD.Common.Utils.Collections
|
||||
m_Dictionary.Clear();
|
||||
}
|
||||
|
||||
public bool ContainsKey(TKey key)
|
||||
public bool ContainsKey([NotNull] TKey key)
|
||||
{
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
return m_Dictionary.ContainsKey(key);
|
||||
}
|
||||
|
||||
public bool Remove(TKey key)
|
||||
public bool Remove([NotNull] TKey key)
|
||||
{
|
||||
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
if (!m_Dictionary.Remove(key))
|
||||
@@ -141,8 +153,13 @@ namespace ICD.Common.Utils.Collections
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool TryGetValue(TKey key, out TValue value)
|
||||
public bool TryGetValue([NotNull] TKey key, out TValue value)
|
||||
{
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
return m_Dictionary.TryGetValue(key, out value);
|
||||
}
|
||||
|
||||
@@ -150,6 +167,7 @@ namespace ICD.Common.Utils.Collections
|
||||
|
||||
#region Private Methods
|
||||
|
||||
[NotNull]
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
@@ -167,8 +185,11 @@ namespace ICD.Common.Utils.Collections
|
||||
EqualityComparer<TValue>.Default.Equals(value, item.Value);
|
||||
}
|
||||
|
||||
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo(KeyValuePair<TKey, TValue>[] array, int index)
|
||||
void ICollection<KeyValuePair<TKey, TValue>>.CopyTo([NotNull] KeyValuePair<TKey, TValue>[] array, int index)
|
||||
{
|
||||
if (array == null)
|
||||
throw new ArgumentNullException("array");
|
||||
|
||||
foreach (KeyValuePair<TKey, TValue> kvp in this)
|
||||
{
|
||||
array.SetValue(kvp, index);
|
||||
|
||||
@@ -30,6 +30,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// <summary>
|
||||
/// Gets a reference for locking.
|
||||
/// </summary>
|
||||
[NotNull]
|
||||
public object SyncRoot { get { return this; } }
|
||||
|
||||
#endregion
|
||||
@@ -59,7 +60,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
[PublicAPI]
|
||||
public void Enqueue(T item)
|
||||
public void Enqueue([CanBeNull] T item)
|
||||
{
|
||||
Enqueue(item, int.MaxValue);
|
||||
}
|
||||
@@ -71,16 +72,10 @@ namespace ICD.Common.Utils.Collections
|
||||
/// <param name="item"></param>
|
||||
/// <param name="priority"></param>
|
||||
[PublicAPI]
|
||||
public void Enqueue(T item, int priority)
|
||||
public void Enqueue([CanBeNull] T item, int priority)
|
||||
{
|
||||
List<T> queue;
|
||||
if (!m_PriorityToQueue.TryGetValue(priority, out queue))
|
||||
{
|
||||
queue = new List<T>();
|
||||
m_PriorityToQueue.Add(priority, queue);
|
||||
}
|
||||
|
||||
queue.Add(item);
|
||||
m_PriorityToQueue.GetOrAddNew(priority, () => new List<T>())
|
||||
.Add(item);
|
||||
m_Count++;
|
||||
}
|
||||
|
||||
@@ -89,30 +84,22 @@ namespace ICD.Common.Utils.Collections
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
[PublicAPI]
|
||||
public void EnqueueFirst(T item)
|
||||
public void EnqueueFirst([CanBeNull] T item)
|
||||
{
|
||||
const int priority = int.MinValue;
|
||||
|
||||
List<T> queue;
|
||||
if (!m_PriorityToQueue.TryGetValue(priority, out queue))
|
||||
{
|
||||
queue = new List<T>();
|
||||
m_PriorityToQueue.Add(priority, queue);
|
||||
}
|
||||
|
||||
queue.Insert(0, item);
|
||||
m_PriorityToQueue.GetOrAddNew(int.MinValue, () => new List<T>())
|
||||
.Insert(0, item);
|
||||
m_Count++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes any items in the queue matching the predicate.
|
||||
/// Inserts the given item in the position of the first removed item, or at the end of the queue.
|
||||
/// This is useful for reducing duplication, or replacing items with something more pertinant.
|
||||
/// Appends the given item at the end of the given priority level.
|
||||
/// This is useful for reducing duplication, or replacing items with something more pertinent.
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="remove"></param>
|
||||
[PublicAPI]
|
||||
public void EnqueueRemove(T item, Func<T, bool> remove)
|
||||
public void EnqueueRemove([CanBeNull] T item, [NotNull] Func<T, bool> remove)
|
||||
{
|
||||
if (remove == null)
|
||||
throw new ArgumentNullException("remove");
|
||||
@@ -122,20 +109,18 @@ namespace ICD.Common.Utils.Collections
|
||||
|
||||
/// <summary>
|
||||
/// Removes any items in the queue matching the predicate.
|
||||
/// Inserts the given item in the position of the first removed item, or at the end of the queue.
|
||||
/// This is useful for reducing duplication, or replacing items with something more pertinant.
|
||||
/// Appends the given item at the end of the given priority level.
|
||||
/// This is useful for reducing duplication, or replacing items with something more pertinent.
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="remove"></param>
|
||||
/// <param name="priority"></param>
|
||||
[PublicAPI]
|
||||
public void EnqueueRemove(T item, Func<T, bool> remove, int priority)
|
||||
public void EnqueueRemove([CanBeNull] T item, [NotNull] Func<T, bool> remove, int priority)
|
||||
{
|
||||
if (remove == null)
|
||||
throw new ArgumentNullException("remove");
|
||||
|
||||
bool inserted = false;
|
||||
|
||||
foreach (KeyValuePair<int, List<T>> kvp in m_PriorityToQueue.ToArray())
|
||||
{
|
||||
int[] removeIndices =
|
||||
@@ -144,35 +129,17 @@ namespace ICD.Common.Utils.Collections
|
||||
.Reverse()
|
||||
.ToArray();
|
||||
|
||||
if (removeIndices.Length == 0)
|
||||
continue;
|
||||
|
||||
foreach (int removeIndex in removeIndices)
|
||||
{
|
||||
kvp.Value.RemoveAt(removeIndex);
|
||||
m_Count--;
|
||||
}
|
||||
|
||||
if (!inserted)
|
||||
{
|
||||
int insertIndex = removeIndices[0];
|
||||
|
||||
if (insertIndex >= kvp.Value.Count)
|
||||
kvp.Value.Add(item);
|
||||
else
|
||||
kvp.Value.Insert(insertIndex, item);
|
||||
|
||||
m_Count++;
|
||||
|
||||
inserted = true;
|
||||
}
|
||||
|
||||
if (kvp.Value.Count == 0)
|
||||
m_PriorityToQueue.Remove(kvp.Key);
|
||||
}
|
||||
|
||||
if (!inserted)
|
||||
Enqueue(item, priority);
|
||||
Enqueue(item, priority);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -180,6 +147,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
[CanBeNull]
|
||||
public T Dequeue()
|
||||
{
|
||||
T output;
|
||||
@@ -227,6 +195,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// Gets an enumerator for the items.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[NotNull]
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
return m_PriorityToQueue.Values
|
||||
@@ -239,8 +208,11 @@ namespace ICD.Common.Utils.Collections
|
||||
/// </summary>
|
||||
/// <param name="array"></param>
|
||||
/// <param name="index"></param>
|
||||
public void CopyTo(Array array, int index)
|
||||
public void CopyTo([NotNull] Array array, int index)
|
||||
{
|
||||
if (array == null)
|
||||
throw new ArgumentNullException("array");
|
||||
|
||||
foreach (T item in this)
|
||||
array.SetValue(item, index++);
|
||||
}
|
||||
@@ -253,6 +225,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// Gets an enumerator for the items.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[NotNull]
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using ICD.Common.Properties;
|
||||
using ICD.Common.Utils.EventArguments;
|
||||
using ICD.Common.Utils.Extensions;
|
||||
using ICD.Common.Utils.Timers;
|
||||
@@ -32,6 +33,7 @@ namespace ICD.Common.Utils.Collections
|
||||
|
||||
bool ICollection.IsSynchronized { get { return true; } }
|
||||
|
||||
[NotNull]
|
||||
object ICollection.SyncRoot { get { return this; } }
|
||||
|
||||
#endregion
|
||||
@@ -72,7 +74,7 @@ namespace ICD.Common.Utils.Collections
|
||||
/// Enqueues the given item.
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
public void Enqueue(T item)
|
||||
public void Enqueue([CanBeNull] T item)
|
||||
{
|
||||
m_QueueSection.Enter();
|
||||
|
||||
@@ -158,18 +160,23 @@ namespace ICD.Common.Utils.Collections
|
||||
|
||||
#region IEnumerable/ICollection
|
||||
|
||||
[NotNull]
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
return m_QueueSection.Execute(() => m_Queue.ToList(m_Queue.Count).GetEnumerator());
|
||||
}
|
||||
|
||||
[NotNull]
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
|
||||
void ICollection.CopyTo(Array array, int index)
|
||||
void ICollection.CopyTo([NotNull] Array array, int index)
|
||||
{
|
||||
if (array == null)
|
||||
throw new ArgumentNullException("array");
|
||||
|
||||
m_QueueSection.Enter();
|
||||
|
||||
try
|
||||
|
||||
15
ICD.Common.Utils/DateTimeUtils.cs
Normal file
15
ICD.Common.Utils/DateTimeUtils.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
namespace ICD.Common.Utils
|
||||
{
|
||||
public static class DateTimeUtils
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts the hour in 24 hour format to 12 hour format (1 through 12).
|
||||
/// </summary>
|
||||
/// <param name="hour"></param>
|
||||
/// <returns></returns>
|
||||
public static int To12Hour(int hour)
|
||||
{
|
||||
return MathUtils.Modulus(hour + 11, 12) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[CanBeNull]
|
||||
public static string GetPath(this Assembly extends)
|
||||
public static string GetPath([NotNull]this Assembly extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -29,7 +29,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
#endif
|
||||
.CodeBase;
|
||||
|
||||
if (path == null)
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
#if STANDARD
|
||||
path = extends.Location;
|
||||
@@ -51,7 +51,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static DateTime GetCreationTime(this Assembly extends)
|
||||
public static DateTime GetCreationTime([NotNull]this Assembly extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -66,7 +66,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static string GetInformationalVersion(this Assembly extends)
|
||||
public static string GetInformationalVersion([NotNull]this Assembly extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -85,7 +85,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="version"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool TryGetInformationalVersion(this Assembly extends, out string version)
|
||||
public static bool TryGetInformationalVersion([NotNull]this Assembly extends, out string version)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
91
ICD.Common.Utils/Extensions/CultureInfoExtensions.cs
Normal file
91
ICD.Common.Utils/Extensions/CultureInfoExtensions.cs
Normal file
@@ -0,0 +1,91 @@
|
||||
using System;
|
||||
using System.Globalization;
|
||||
using ICD.Common.Properties;
|
||||
|
||||
namespace ICD.Common.Utils.Extensions
|
||||
{
|
||||
public static class CultureInfoExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns true if the given culture uses a 24 hour time format.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static bool Uses24HourFormat([NotNull]this CultureInfo extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
return extends.DateTimeFormat.ShortTimePattern.Contains("H");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the time patterns for the given culture to use 12 hour time.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
public static void ConvertTo12HourCulture([NotNull]this CultureInfo extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (extends.IsReadOnly)
|
||||
throw new InvalidOperationException("Culture is readonly");
|
||||
|
||||
DateTimeFormatInfo dateTimeFormat = extends.DateTimeFormat;
|
||||
|
||||
dateTimeFormat.FullDateTimePattern = To12HourPattern(dateTimeFormat.FullDateTimePattern);
|
||||
dateTimeFormat.LongTimePattern = To12HourPattern(dateTimeFormat.LongTimePattern);
|
||||
dateTimeFormat.ShortTimePattern = To12HourPattern(dateTimeFormat.ShortTimePattern);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates the time patterns for the given culture to use 24 hour time.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
public static void ConvertTo24HourCulture([NotNull]this CultureInfo extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (extends.IsReadOnly)
|
||||
throw new InvalidOperationException("Culture is readonly");
|
||||
|
||||
DateTimeFormatInfo dateTimeFormat = extends.DateTimeFormat;
|
||||
|
||||
dateTimeFormat.FullDateTimePattern = To24HourPattern(dateTimeFormat.FullDateTimePattern);
|
||||
dateTimeFormat.LongTimePattern = To24HourPattern(dateTimeFormat.LongTimePattern);
|
||||
dateTimeFormat.ShortTimePattern = To24HourPattern(dateTimeFormat.ShortTimePattern);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the given format pattern to use 24 hour time.
|
||||
/// </summary>
|
||||
/// <param name="pattern"></param>
|
||||
/// <returns></returns>
|
||||
private static string To24HourPattern(string pattern)
|
||||
{
|
||||
if (pattern == null)
|
||||
return null;
|
||||
|
||||
pattern = pattern.Replace(" tt", "");
|
||||
|
||||
return pattern.Replace("h", "H");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts the given format pattern to use 12 hour time.
|
||||
/// </summary>
|
||||
/// <param name="pattern"></param>
|
||||
/// <returns></returns>
|
||||
private static string To12HourPattern(string pattern)
|
||||
{
|
||||
if (pattern == null)
|
||||
return null;
|
||||
|
||||
if (!pattern.Contains("t"))
|
||||
pattern = pattern + " tt";
|
||||
|
||||
return pattern.Replace("H", "h");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using ICD.Common.Properties;
|
||||
@@ -38,14 +39,29 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="inclusive">Whether or not to include times equal to the target time</param>
|
||||
/// <param name="times"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime? NextEarliestTime(this DateTime target, bool inclusive, params DateTime[] times)
|
||||
public static DateTime? NextEarliestTime(this DateTime target, bool inclusive, [NotNull] params DateTime[] times)
|
||||
{
|
||||
if (times == null)
|
||||
throw new ArgumentNullException("times");
|
||||
|
||||
return target.NextEarliestTime(inclusive, (IEnumerable<DateTime>)times);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the closest DateTime to the target time that is greater than the target time
|
||||
/// </summary>
|
||||
/// <param name="target"></param>
|
||||
/// <param name="inclusive">Whether or not to include times equal to the target time</param>
|
||||
/// <param name="times"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime? NextEarliestTime(this DateTime target, bool inclusive, [NotNull] IEnumerable<DateTime> times)
|
||||
{
|
||||
if (times == null)
|
||||
throw new ArgumentNullException("times");
|
||||
|
||||
DateTime earliestTime;
|
||||
bool success = times.OrderBy(dt => dt).TryFirst(dt => inclusive ? target <= dt : target < dt, out earliestTime);
|
||||
return success ? earliestTime : (DateTime?) null;
|
||||
return success ? earliestTime : (DateTime?)null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -55,14 +71,29 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="inclusive">Whether or not to include times equal to the target time</param>
|
||||
/// <param name="times"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime? PreviousLatestTime(this DateTime target, bool inclusive, params DateTime[] times)
|
||||
public static DateTime? PreviousLatestTime(this DateTime target, bool inclusive, [NotNull] params DateTime[] times)
|
||||
{
|
||||
if (times == null)
|
||||
throw new ArgumentNullException("null");
|
||||
throw new ArgumentNullException("times");
|
||||
|
||||
return target.PreviousLatestTime(inclusive, (IEnumerable<DateTime>)times);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the closest DateTime to the target time that is less than the target time
|
||||
/// </summary>
|
||||
/// <param name="target"></param>
|
||||
/// <param name="inclusive">Whether or not to include times equal to the target time</param>
|
||||
/// <param name="times"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime? PreviousLatestTime(this DateTime target, bool inclusive, [NotNull] IEnumerable<DateTime> times)
|
||||
{
|
||||
if (times == null)
|
||||
throw new ArgumentNullException("times");
|
||||
|
||||
DateTime latestTime;
|
||||
bool success = times.OrderByDescending(dt => dt).TryFirst(dt => inclusive ? target >= dt : target > dt, out latestTime);
|
||||
return success ? latestTime : (DateTime?) null;
|
||||
return success ? latestTime : (DateTime?)null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -85,6 +116,11 @@ namespace ICD.Common.Utils.Extensions
|
||||
return extends.StartOfDay() + new TimeSpan(24, 0, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the given date in unix timestamp format.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static double ToUnixTimestamp(this DateTime extends)
|
||||
{
|
||||
|
||||
@@ -14,7 +14,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <typeparam name="TValue"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="keys"></param>
|
||||
public static void RemoveAll<TKey, TValue>(this IDictionary<TKey, TValue> extends, IEnumerable<TKey> keys)
|
||||
public static void RemoveAll<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
|
||||
[NotNull] IEnumerable<TKey> keys)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -35,7 +36,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="value"></param>
|
||||
/// <returns>False if value is not found in the dictionary.</returns>
|
||||
[PublicAPI]
|
||||
public static bool RemoveValue<TKey, TValue>(this IDictionary<TKey, TValue> extends, TValue value)
|
||||
public static bool RemoveValue<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends, TValue value)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -52,7 +53,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="value"></param>
|
||||
[PublicAPI]
|
||||
public static void RemoveAllValues<TKey, TValue>(this IDictionary<TKey, TValue> extends, TValue value)
|
||||
public static void RemoveAllValues<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends, TValue value)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -71,12 +72,12 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <returns></returns>
|
||||
[CanBeNull]
|
||||
[PublicAPI]
|
||||
public static TValue GetDefault<TKey, TValue>(this IDictionary<TKey, TValue> extends, TKey key)
|
||||
public static TValue GetDefault<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
|
||||
[NotNull] TKey key)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
@@ -93,12 +94,13 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static TValue GetDefault<TKey, TValue>(this IDictionary<TKey, TValue> extends, TKey key, TValue defaultValue)
|
||||
public static TValue GetDefault<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
|
||||
[NotNull] TKey key,
|
||||
TValue defaultValue)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
@@ -116,13 +118,13 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static TValue GetOrAddDefault<TKey, TValue>(this IDictionary<TKey, TValue> extends, TKey key,
|
||||
public static TValue GetOrAddDefault<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
|
||||
[NotNull] TKey key,
|
||||
TValue defaultValue)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
@@ -141,20 +143,50 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static TValue GetOrAddNew<TKey, TValue>(this IDictionary<TKey, TValue> extends, TKey key)
|
||||
public static TValue GetOrAddNew<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
|
||||
[NotNull] TKey key)
|
||||
where TValue : new()
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
// ReSharper disable once CompareNonConstrainedGenericWithNull
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
return extends.GetOrAddNew(key, () => ReflectionUtils.CreateInstance<TValue>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// If the key is present in the dictionary return the value, otherwise add a new value to the dictionary and return it.
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
/// <typeparam name="TValue"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="valueFunc"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static TValue GetOrAddNew<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
|
||||
[NotNull] TKey key,
|
||||
[NotNull] Func<TValue> valueFunc)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
if (key == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
throw new ArgumentNullException("key");
|
||||
|
||||
if (valueFunc == null)
|
||||
throw new ArgumentNullException("valueFunc");
|
||||
|
||||
TValue value;
|
||||
if (!extends.TryGetValue(key, out value))
|
||||
{
|
||||
value = ReflectionUtils.CreateInstance<TValue>();
|
||||
value = valueFunc();
|
||||
extends.Add(key, value);
|
||||
}
|
||||
|
||||
@@ -171,7 +203,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <returns></returns>
|
||||
/// <exception cref="ArgumentOutOfRangeException">The value does not exist in the dictionary.</exception>
|
||||
[PublicAPI]
|
||||
public static TKey GetKey<TKey, TValue>(this IDictionary<TKey, TValue> extends, TValue value)
|
||||
public static TKey GetKey<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends, TValue value)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -194,7 +226,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool TryGetKey<TKey, TValue>(this IDictionary<TKey, TValue> extends, TValue value, out TKey key)
|
||||
public static bool TryGetKey<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends, TValue value,
|
||||
out TKey key)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -211,7 +244,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static IEnumerable<TKey> GetKeys<TKey, TValue>(this IDictionary<TKey, TValue> extends, TValue value)
|
||||
public static IEnumerable<TKey> GetKeys<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
|
||||
TValue value)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -229,8 +263,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="other"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool Update<TKey, TValue>(this IDictionary<TKey, TValue> extends,
|
||||
IEnumerable<KeyValuePair<TKey, TValue>> other)
|
||||
public static bool Update<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
|
||||
[NotNull] IEnumerable<KeyValuePair<TKey, TValue>> other)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -251,9 +285,9 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="comparer"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool Update<TKey, TValue>(this IDictionary<TKey, TValue> extends,
|
||||
IEnumerable<KeyValuePair<TKey, TValue>> other,
|
||||
IEqualityComparer<TValue> comparer)
|
||||
public static bool Update<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
|
||||
[NotNull] IEnumerable<KeyValuePair<TKey, TValue>> other,
|
||||
[NotNull] IEqualityComparer<TValue> comparer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -261,6 +295,9 @@ namespace ICD.Common.Utils.Extensions
|
||||
if (other == null)
|
||||
throw new ArgumentNullException("other");
|
||||
|
||||
if (comparer == null)
|
||||
throw new ArgumentNullException("comparer");
|
||||
|
||||
bool change = false;
|
||||
|
||||
foreach (KeyValuePair<TKey, TValue> pair in other)
|
||||
@@ -284,7 +321,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="items"></param>
|
||||
[PublicAPI]
|
||||
public static void AddRange<TKey, TValue>(this IDictionary<TKey, TValue> extends, IEnumerable<KeyValuePair<TKey, TValue>> items)
|
||||
public static void AddRange<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
|
||||
[NotNull] IEnumerable<KeyValuePair<TKey, TValue>> items)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -305,8 +343,9 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="items"></param>
|
||||
/// <param name="getKey"></param>
|
||||
[PublicAPI]
|
||||
public static void AddRange<TKey, TValue>(this IDictionary<TKey, TValue> extends, IEnumerable<TValue> items,
|
||||
Func<TValue, TKey> getKey)
|
||||
public static void AddRange<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
|
||||
[NotNull] IEnumerable<TValue> items,
|
||||
[NotNull] Func<TValue, TKey> getKey)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -330,8 +369,9 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="items"></param>
|
||||
/// <param name="getValue"></param>
|
||||
[PublicAPI]
|
||||
public static void AddRange<TKey, TValue>(this IDictionary<TKey, TValue> extends, IEnumerable<TKey> items,
|
||||
Func<TKey, TValue> getValue)
|
||||
public static void AddRange<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
|
||||
[NotNull] IEnumerable<TKey> items,
|
||||
[NotNull] Func<TKey, TValue> getValue)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -354,8 +394,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="items"></param>
|
||||
[PublicAPI]
|
||||
public static void AddRange<TKey, TValue>(this Dictionary<TKey, TValue> extends,
|
||||
IEnumerable<KeyValuePair<TKey, TValue>> items)
|
||||
public static void AddRange<TKey, TValue>([NotNull] this Dictionary<TKey, TValue> extends,
|
||||
[NotNull] IEnumerable<KeyValuePair<TKey, TValue>> items)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -376,12 +416,15 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="other"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool DictionaryEqual<TKey, TValue>(this IDictionary<TKey, TValue> extends,
|
||||
IDictionary<TKey, TValue> other)
|
||||
public static bool DictionaryEqual<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
|
||||
[NotNull] IDictionary<TKey, TValue> other)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (other == null)
|
||||
throw new ArgumentNullException("other");
|
||||
|
||||
return extends.DictionaryEqual(other, EqualityComparer<TValue>.Default);
|
||||
}
|
||||
|
||||
@@ -395,13 +438,16 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="valueComparer"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool DictionaryEqual<TKey, TValue>(this IDictionary<TKey, TValue> extends,
|
||||
IDictionary<TKey, TValue> other,
|
||||
IEqualityComparer<TValue> valueComparer)
|
||||
public static bool DictionaryEqual<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
|
||||
[NotNull] IDictionary<TKey, TValue> other,
|
||||
[NotNull] IEqualityComparer<TValue> valueComparer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (other == null)
|
||||
throw new ArgumentNullException("other");
|
||||
|
||||
if (valueComparer == null)
|
||||
throw new ArgumentNullException("valueComparer");
|
||||
|
||||
@@ -418,20 +464,21 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="valueComparer"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool DictionaryEqual<TKey, TValue>(this IDictionary<TKey, TValue> extends,
|
||||
IDictionary<TKey, TValue> other,
|
||||
Func<TValue, TValue, bool> valueComparer)
|
||||
public static bool DictionaryEqual<TKey, TValue>([NotNull] this IDictionary<TKey, TValue> extends,
|
||||
[NotNull] IDictionary<TKey, TValue> other,
|
||||
[NotNull] Func<TValue, TValue, bool> valueComparer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (other == null)
|
||||
throw new ArgumentNullException("other");
|
||||
|
||||
if (valueComparer == null)
|
||||
throw new ArgumentNullException("valueComparer");
|
||||
|
||||
if (extends == other)
|
||||
return true;
|
||||
if (other == null)
|
||||
return false;
|
||||
if (extends.Count != other.Count)
|
||||
return false;
|
||||
|
||||
@@ -443,6 +490,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
if (!valueComparer(kvp.Value, secondValue))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -455,7 +503,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static IEnumerable<KeyValuePair<TKey, TValue>> OrderByKey<TKey, TValue>(
|
||||
this IEnumerable<KeyValuePair<TKey, TValue>> extends)
|
||||
[NotNull] this IEnumerable<KeyValuePair<TKey, TValue>> extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -471,7 +519,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static IEnumerable<TValue> OrderValuesByKey<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> extends)
|
||||
public static IEnumerable<TValue> OrderValuesByKey<TKey, TValue>(
|
||||
[NotNull] this IEnumerable<KeyValuePair<TKey, TValue>> extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -487,7 +536,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static Dictionary<TValue, List<TKey>> ToInverse<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> extends)
|
||||
public static Dictionary<TValue, List<TKey>> ToInverse<TKey, TValue>(
|
||||
[NotNull] this IEnumerable<KeyValuePair<TKey, TValue>> extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -508,5 +558,22 @@ namespace ICD.Common.Utils.Extensions
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Turns an enumerable of KeyValuePairs back into a dictionary
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
/// <typeparam name="TValue"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(
|
||||
[NotNull] this IEnumerable<KeyValuePair<TKey, TValue>> extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
return extends.ToDictionary(x => x.Key, x => x.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="defaultItem"></param>
|
||||
/// <returns></returns>
|
||||
public static T FirstOrDefault<T>(this IEnumerable<T> extends, T defaultItem)
|
||||
public static T FirstOrDefault<T>([NotNull] this IEnumerable<T> extends, T defaultItem)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -34,7 +34,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="defaultItem"></param>
|
||||
/// <returns></returns>
|
||||
public static T FirstOrDefault<T>(this IEnumerable<T> extends, Func<T, bool> predicate, T defaultItem)
|
||||
public static T FirstOrDefault<T>([NotNull] this IEnumerable<T> extends, [NotNull] Func<T, bool> predicate,
|
||||
T defaultItem)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -53,7 +54,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item">Outputs the first item in the sequence.</param>
|
||||
/// <returns></returns>
|
||||
public static bool TryFirst<T>(this IEnumerable<T> extends, out T item)
|
||||
public static bool TryFirst<T>([NotNull] this IEnumerable<T> extends, out T item)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -80,7 +81,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="item">Outputs the first item in the sequence.</param>
|
||||
/// <returns></returns>
|
||||
public static bool TryFirst<T>(this IEnumerable<T> extends, Func<T, bool> predicate, out T item)
|
||||
public static bool TryFirst<T>([NotNull] this IEnumerable<T> extends, [NotNull] Func<T, bool> predicate,
|
||||
out T item)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -112,7 +114,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item">Outputs the last item in the sequence.</param>
|
||||
/// <returns></returns>
|
||||
public static bool TryLast<T>(this IEnumerable<T> extends, out T item)
|
||||
public static bool TryLast<T>([NotNull] this IEnumerable<T> extends, out T item)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -139,7 +141,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="predicate"></param>
|
||||
/// <param name="item">Outputs the last item in the sequence.</param>
|
||||
/// <returns></returns>
|
||||
public static bool TryLast<T>(this IEnumerable<T> extends, Func<T, bool> predicate, out T item)
|
||||
public static bool TryLast<T>([NotNull] this IEnumerable<T> extends, [NotNull] Func<T, bool> predicate,
|
||||
out T item)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -173,7 +176,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="index"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
public static bool TryElementAt<T>(this IEnumerable<T> extends, int index, out T item)
|
||||
public static bool TryElementAt<T>([NotNull] this IEnumerable<T> extends, int index, out T item)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -200,6 +203,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
item = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
current++;
|
||||
}
|
||||
|
||||
@@ -214,7 +218,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="index"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public static T ElementAtOrDefault<T>(this IEnumerable<T> extends, int index, T defaultValue)
|
||||
public static T ElementAtOrDefault<T>([NotNull] this IEnumerable<T> extends, int index, T defaultValue)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -231,7 +235,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="other"></param>
|
||||
/// <param name="comparer"></param>
|
||||
/// <returns></returns>
|
||||
public static bool SequenceEqual<T>(this IEnumerable<T> extends, IEnumerable<T> other, IEqualityComparer<T> comparer)
|
||||
public static bool SequenceEqual<T>([NotNull] this IEnumerable<T> extends, [NotNull] IEnumerable<T> other,
|
||||
[NotNull] IEqualityComparer<T> comparer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -253,7 +258,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="other"></param>
|
||||
/// <param name="comparer"></param>
|
||||
/// <returns></returns>
|
||||
public static bool SequenceEqual<T>(this IEnumerable<T> extends, IEnumerable<T> other, Func<T, T, bool> comparer)
|
||||
public static bool SequenceEqual<T>([NotNull] this IEnumerable<T> extends, [NotNull] IEnumerable<T> other,
|
||||
[NotNull] Func<T, T, bool> comparer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -298,7 +304,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="other"></param>
|
||||
/// <returns></returns>
|
||||
public static bool ScrambledEquals<T>(this IEnumerable<T> extends, IEnumerable<T> other)
|
||||
public static bool ScrambledEquals<T>([NotNull] this IEnumerable<T> extends, [NotNull] IEnumerable<T> other)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -317,7 +323,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="other"></param>
|
||||
/// <param name="comparer"></param>
|
||||
/// <returns></returns>
|
||||
public static bool ScrambledEquals<T>(this IEnumerable<T> extends, IEnumerable<T> other, IEqualityComparer<T> comparer)
|
||||
public static bool ScrambledEquals<T>([NotNull] this IEnumerable<T> extends, [NotNull] IEnumerable<T> other,
|
||||
[NotNull] IEqualityComparer<T> comparer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -352,7 +359,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="match"></param>
|
||||
/// <returns></returns>
|
||||
public static int FindIndex<T>(this IEnumerable<T> extends, Predicate<T> match)
|
||||
public static int FindIndex<T>([NotNull] this IEnumerable<T> extends, [NotNull] Predicate<T> match)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -370,7 +377,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="match"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<int> FindIndices<T>(this IEnumerable<T> extends, Predicate<T> match)
|
||||
public static IEnumerable<int> FindIndices<T>([NotNull] this IEnumerable<T> extends,
|
||||
[NotNull] Predicate<T> match)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -388,7 +396,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="sequence"></param>
|
||||
/// <param name="match"></param>
|
||||
/// <returns></returns>
|
||||
private static IEnumerable<int> FindIndicesIterator<T>(IEnumerable<T> sequence, Predicate<T> match)
|
||||
private static IEnumerable<int> FindIndicesIterator<T>([NotNull] IEnumerable<T> sequence,
|
||||
[NotNull] Predicate<T> match)
|
||||
{
|
||||
int index = 0;
|
||||
|
||||
@@ -408,7 +417,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="selectors"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<TResult> SelectMulti<TSource, TResult>(this IEnumerable<TSource> extends,
|
||||
public static IEnumerable<TResult> SelectMulti<TSource, TResult>([NotNull] this IEnumerable<TSource> extends,
|
||||
[NotNull]
|
||||
params Func<TSource, TResult>[] selectors)
|
||||
{
|
||||
if (extends == null)
|
||||
@@ -427,7 +437,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
[PublicAPI]
|
||||
public static void Execute<T>(this IEnumerable<T> extends)
|
||||
public static void Execute<T>([NotNull] this IEnumerable<T> extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -446,7 +456,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="action"></param>
|
||||
[PublicAPI]
|
||||
public static void ForEach<T>(this IEnumerable<T> extends, Action<T> action)
|
||||
public static void ForEach<T>([NotNull] this IEnumerable<T> extends, [NotNull] Action<T> action)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -464,7 +474,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="action"></param>
|
||||
[PublicAPI]
|
||||
public static void ForEach<T>(this IEnumerable<T> extends, Action<T, int> action)
|
||||
public static void ForEach<T>([NotNull] this IEnumerable<T> extends, [NotNull] Action<T, int> action)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -485,7 +495,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<T> Prepend<T>(this IEnumerable<T> extends, T item)
|
||||
public static IEnumerable<T> Prepend<T>([NotNull]this IEnumerable<T> extends, T item)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -500,7 +510,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="sequence"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
private static IEnumerable<T> PrependIterator<T>(IEnumerable<T> sequence, T item)
|
||||
private static IEnumerable<T> PrependIterator<T>([NotNull]IEnumerable<T> sequence, T item)
|
||||
{
|
||||
yield return item;
|
||||
|
||||
@@ -517,7 +527,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="items"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static IEnumerable<T> PrependMany<T>(this IEnumerable<T> extends, params T[] items)
|
||||
public static IEnumerable<T> PrependMany<T>([NotNull] this IEnumerable<T> extends, [NotNull] params T[] items)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -535,7 +545,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="sequence"></param>
|
||||
/// <param name="items"></param>
|
||||
/// <returns></returns>
|
||||
private static IEnumerable<T> PrependManyIterator<T>(IEnumerable<T> sequence, params T[] items)
|
||||
private static IEnumerable<T> PrependManyIterator<T>([NotNull] IEnumerable<T> sequence,
|
||||
[NotNull] params T[] items)
|
||||
{
|
||||
foreach (T item in items)
|
||||
yield return item;
|
||||
@@ -552,7 +563,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<T> Append<T>(this IEnumerable<T> extends, T item)
|
||||
public static IEnumerable<T> Append<T>([NotNull]this IEnumerable<T> extends, T item)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -567,7 +578,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="sequence"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
private static IEnumerable<T> AppendIterator<T>(IEnumerable<T> sequence, T item)
|
||||
private static IEnumerable<T> AppendIterator<T>([NotNull]IEnumerable<T> sequence, T item)
|
||||
{
|
||||
foreach (T first in sequence)
|
||||
yield return first;
|
||||
@@ -584,7 +595,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="items"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static IEnumerable<T> AppendMany<T>(this IEnumerable<T> extends, params T[] items)
|
||||
public static IEnumerable<T> AppendMany<T>([NotNull] this IEnumerable<T> extends, [NotNull] params T[] items)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -602,7 +613,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="sequence"></param>
|
||||
/// <param name="items"></param>
|
||||
/// <returns></returns>
|
||||
private static IEnumerable<T> AppendManyIterator<T>(IEnumerable<T> sequence, params T[] items)
|
||||
private static IEnumerable<T> AppendManyIterator<T>([NotNull] IEnumerable<T> sequence,
|
||||
[NotNull] params T[] items)
|
||||
{
|
||||
foreach (T each in sequence)
|
||||
yield return each;
|
||||
@@ -619,7 +631,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="count"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static IEnumerable<T> PadRight<T>(this IEnumerable<T> extends, int count)
|
||||
public static IEnumerable<T> PadRight<T>([NotNull] this IEnumerable<T> extends, int count)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -634,7 +646,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="sequence"></param>
|
||||
/// <param name="count"></param>
|
||||
/// <returns></returns>
|
||||
private static IEnumerable<T> PadRightIterator<T>(IEnumerable<T> sequence, int count)
|
||||
private static IEnumerable<T> PadRightIterator<T>([NotNull] IEnumerable<T> sequence, int count)
|
||||
{
|
||||
int index = 0;
|
||||
|
||||
@@ -648,13 +660,57 @@ namespace ICD.Common.Utils.Extensions
|
||||
yield return default(T);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the given sequence is ordered.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static bool AreOrdered<T>([NotNull] this IEnumerable<T> extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
return extends.AreOrdered(Comparer<T>.Default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the given sequence is ordered.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="comparer"></param>
|
||||
/// <returns></returns>
|
||||
public static bool AreOrdered<T>([NotNull] this IEnumerable<T> extends, [NotNull] IComparer<T> comparer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (comparer == null)
|
||||
throw new ArgumentNullException("comparer");
|
||||
|
||||
bool first = true;
|
||||
T previous = default(T);
|
||||
|
||||
foreach (T item in extends)
|
||||
{
|
||||
if (!first && comparer.Compare(item, previous) < 0)
|
||||
return false;
|
||||
|
||||
first = false;
|
||||
previous = item;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Default ordering for the items in the sequence.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static IOrderedEnumerable<T> Order<T>(this IEnumerable<T> extends)
|
||||
public static IOrderedEnumerable<T> Order<T>([NotNull] this IEnumerable<T> extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -669,7 +725,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="comparer"></param>
|
||||
/// <returns></returns>
|
||||
public static IOrderedEnumerable<T> Order<T>(this IEnumerable<T> extends, IComparer<T> comparer)
|
||||
public static IOrderedEnumerable<T> Order<T>([NotNull] this IEnumerable<T> extends,
|
||||
[NotNull] IComparer<T> comparer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -687,7 +744,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="comparer"></param>
|
||||
/// <returns></returns>
|
||||
public static IOrderedEnumerable<T> OrderDescending<T>(this IEnumerable<T> extends, IComparer<T> comparer)
|
||||
public static IOrderedEnumerable<T> OrderDescending<T>([NotNull] this IEnumerable<T> extends,
|
||||
[NotNull] IComparer<T> comparer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -705,7 +763,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<T> Except<T>(this IEnumerable<T> extends, T item)
|
||||
public static IEnumerable<T> Except<T>([NotNull] this IEnumerable<T> extends, T item)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -721,7 +779,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="item"></param>
|
||||
/// <param name="comparer"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<T> Except<T>(this IEnumerable<T> extends, T item, IEqualityComparer<T> comparer)
|
||||
public static IEnumerable<T> Except<T>([NotNull] this IEnumerable<T> extends, T item,
|
||||
[NotNull] IEqualityComparer<T> comparer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -732,6 +791,22 @@ namespace ICD.Common.Utils.Extensions
|
||||
return extends.Where(i => !comparer.Equals(item, i));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes any null elements from an enumerable of nullable value types
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static IEnumerable<T> ExceptNulls<T>([NotNull] this IEnumerable<T?> extends)
|
||||
where T : struct
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
return extends.OfType<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies all the elements of the current one-dimensional array to the specified one-dimensional array
|
||||
/// starting at the specified destination array index. The index is specified as a 32-bit integer.
|
||||
@@ -740,7 +815,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="array"></param>
|
||||
/// <param name="index"></param>
|
||||
public static void CopyTo<T>(this IEnumerable<T> extends, T[] array, int index)
|
||||
public static void CopyTo<T>([NotNull] this IEnumerable<T> extends, [NotNull] T[] array, int index)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -769,7 +844,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static IcdHashSet<T> ToIcdHashSet<T>(this IEnumerable<T> extends)
|
||||
public static IcdHashSet<T> ToIcdHashSet<T>([NotNull] this IEnumerable<T> extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -784,7 +859,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="comparer"></param>
|
||||
/// <returns></returns>
|
||||
public static IcdHashSet<T> ToIcdHashSet<T>(this IEnumerable<T> extends, IEqualityComparer<T> comparer)
|
||||
public static IcdHashSet<T> ToIcdHashSet<T>([NotNull] this IEnumerable<T> extends,
|
||||
[NotNull] IEqualityComparer<T> comparer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -802,7 +878,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="count"></param>
|
||||
/// <returns></returns>
|
||||
public static T[] ToArray<T>(this IEnumerable<T> extends, int count)
|
||||
public static T[] ToArray<T>([NotNull] this IEnumerable<T> extends, int count)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -810,11 +886,29 @@ namespace ICD.Common.Utils.Extensions
|
||||
if (count < 0)
|
||||
throw new ArgumentOutOfRangeException("count");
|
||||
|
||||
T[] array = new T[count];
|
||||
int i = 0;
|
||||
// Source is already an array
|
||||
T[] arrayCast = extends as T[];
|
||||
if (arrayCast != null)
|
||||
{
|
||||
T[] output = new T[count];
|
||||
Array.Copy(arrayCast, output, count);
|
||||
return output;
|
||||
}
|
||||
|
||||
// Dumb sequence case
|
||||
T[] array = new T[count];
|
||||
|
||||
int i = 0;
|
||||
foreach (T item in extends)
|
||||
{
|
||||
array[i++] = item;
|
||||
if (i >= count)
|
||||
break;
|
||||
}
|
||||
|
||||
if (i != count)
|
||||
throw new ArgumentOutOfRangeException("count");
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
@@ -825,7 +919,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="count"></param>
|
||||
/// <returns></returns>
|
||||
public static List<T> ToList<T>(this IEnumerable<T> extends, int count)
|
||||
public static List<T> ToList<T>([NotNull] this IEnumerable<T> extends, int count)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -845,7 +939,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static Dictionary<int, T> ToIndexedDictionary<T>(this IEnumerable<T> extends)
|
||||
public static Dictionary<int, T> ToIndexedDictionary<T>([NotNull] this IEnumerable<T> extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -862,7 +956,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static Dictionary<uint, T> ToIndexedDictionaryUInt<T>(this IEnumerable<T> extends)
|
||||
public static Dictionary<uint, T> ToIndexedDictionaryUInt<T>([NotNull] this IEnumerable<T> extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -872,22 +966,6 @@ namespace ICD.Common.Utils.Extensions
|
||||
return output;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Turns an enumerable of KeyValuePairs back into a dictionary
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey"></typeparam>
|
||||
/// <typeparam name="TValue"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
return extends.ToDictionary(x => x.Key, x => x.Value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets distinct elements from the sequence based on given property.
|
||||
/// </summary>
|
||||
@@ -897,8 +975,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="getProperty"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static IEnumerable<TItem> Distinct<TItem, TProperty>(this IEnumerable<TItem> extends,
|
||||
Func<TItem, TProperty> getProperty)
|
||||
public static IEnumerable<TItem> Distinct<TItem, TProperty>([NotNull] this IEnumerable<TItem> extends,
|
||||
[NotNull] Func<TItem, TProperty> getProperty)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -920,8 +998,9 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="propertyComparer"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static IEnumerable<TItem> Distinct<TItem, TProperty>(this IEnumerable<TItem> extends,
|
||||
public static IEnumerable<TItem> Distinct<TItem, TProperty>([NotNull] this IEnumerable<TItem> extends,
|
||||
Func<TItem, TProperty> getProperty,
|
||||
[NotNull]
|
||||
IEqualityComparer<TProperty> propertyComparer)
|
||||
{
|
||||
if (extends == null)
|
||||
@@ -942,7 +1021,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static T Random<T>(this IEnumerable<T> extends)
|
||||
public static T Random<T>([NotNull] this IEnumerable<T> extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -967,7 +1046,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="other"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static T Unanimous<T>(this IEnumerable<T> extends, T other)
|
||||
public static T Unanimous<T>([NotNull] this IEnumerable<T> extends, T other)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -985,7 +1064,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="result"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool Unanimous<T>(this IEnumerable<T> extends, out T result)
|
||||
public static bool Unanimous<T>([NotNull] this IEnumerable<T> extends, out T result)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -1003,7 +1082,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="result"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool Unanimous<T>(this IEnumerable<T> extends, IEqualityComparer<T> comparer, out T result)
|
||||
public static bool Unanimous<T>([NotNull] this IEnumerable<T> extends, [NotNull] IEqualityComparer<T> comparer,
|
||||
out T result)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -1042,7 +1122,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="partitionSize"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static IEnumerable<IEnumerable<T>> Partition<T>(this IEnumerable<T> extends, int partitionSize)
|
||||
public static IEnumerable<IEnumerable<T>> Partition<T>([NotNull] this IEnumerable<T> extends, int partitionSize)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -1056,7 +1136,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="sequence"></param>
|
||||
/// <param name="partitionSize"></param>
|
||||
/// <returns></returns>
|
||||
private static IEnumerable<IEnumerable<T>> PartitionIterator<T>(IEnumerable<T> sequence, int partitionSize)
|
||||
private static IEnumerable<IEnumerable<T>> PartitionIterator<T>([NotNull] IEnumerable<T> sequence,
|
||||
int partitionSize)
|
||||
{
|
||||
using (IEnumerator<T> enumerator = sequence.GetEnumerator())
|
||||
{
|
||||
@@ -1065,7 +1146,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
}
|
||||
}
|
||||
|
||||
private static IEnumerable<T> YieldBatchElements<T>(IEnumerator<T> source, int partitionSize)
|
||||
private static IEnumerable<T> YieldBatchElements<T>([NotNull] IEnumerator<T> source, int partitionSize)
|
||||
{
|
||||
if (source == null)
|
||||
throw new ArgumentNullException("source");
|
||||
@@ -1086,7 +1167,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <typeparam name="T">Type of the object.</typeparam>
|
||||
/// <param name="item">The instance that will be wrapped.</param>
|
||||
/// <returns>An IEnumerable<T> consisting of a single item.</returns>
|
||||
public static IEnumerable<T> Yield<T>(this T item)
|
||||
public static IEnumerable<T> Yield<T>([CanBeNull] this T item)
|
||||
{
|
||||
yield return item;
|
||||
}
|
||||
@@ -1097,7 +1178,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<T[]> GetAdjacentPairs<T>(this IEnumerable<T> extends)
|
||||
public static IEnumerable<T[]> GetAdjacentPairs<T>([NotNull] this IEnumerable<T> extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -1111,7 +1192,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
private static IEnumerable<T[]> GetAdjacentPairsIterator<T>(IEnumerable<T> extends)
|
||||
private static IEnumerable<T[]> GetAdjacentPairsIterator<T>([NotNull] IEnumerable<T> extends)
|
||||
{
|
||||
T previous = default(T);
|
||||
bool first = true;
|
||||
@@ -1119,7 +1200,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
foreach (T item in extends)
|
||||
{
|
||||
if (!first)
|
||||
yield return new[] { previous, item };
|
||||
yield return new[] {previous, item};
|
||||
|
||||
first = false;
|
||||
previous = item;
|
||||
@@ -1133,7 +1214,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="getDelta"></param>
|
||||
/// <returns></returns>
|
||||
public static T GetClosest<T>(this IEnumerable<T> extends, Func<T, int> getDelta)
|
||||
public static T GetClosest<T>([NotNull] this IEnumerable<T> extends, [NotNull] Func<T, int> getDelta)
|
||||
{
|
||||
return extends.MinBy(n => Math.Abs(getDelta(n)));
|
||||
}
|
||||
@@ -1156,8 +1237,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null</exception>
|
||||
/// <exception cref="InvalidOperationException"><paramref name="source"/> is empty</exception>
|
||||
[PublicAPI]
|
||||
public static TSource MinBy<TSource, TKey>(this IEnumerable<TSource> source,
|
||||
Func<TSource, TKey> selector)
|
||||
public static TSource MinBy<TSource, TKey>([NotNull] this IEnumerable<TSource> source,
|
||||
[NotNull] Func<TSource, TKey> selector)
|
||||
{
|
||||
if (source == null)
|
||||
throw new ArgumentNullException("source");
|
||||
@@ -1187,8 +1268,9 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// or <paramref name="comparer"/> is null</exception>
|
||||
/// <exception cref="InvalidOperationException"><paramref name="source"/> is empty</exception>
|
||||
[PublicAPI]
|
||||
public static TSource MinBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> selector,
|
||||
IComparer<TKey> comparer)
|
||||
public static TSource MinBy<TSource, TKey>([NotNull] this IEnumerable<TSource> source,
|
||||
[NotNull] Func<TSource, TKey> selector,
|
||||
[NotNull] IComparer<TKey> comparer)
|
||||
{
|
||||
if (source == null)
|
||||
throw new ArgumentNullException("source");
|
||||
@@ -1224,19 +1306,30 @@ namespace ICD.Common.Utils.Extensions
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes any null elements from an enumerable of nullable value types
|
||||
/// Returns the maximum value from the sequence, otherwise the default value.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static IEnumerable<T> ExceptNulls<T>(this IEnumerable<T?> extends)
|
||||
where T : struct
|
||||
public static T MaxOrDefault<T>([NotNull] this IEnumerable<T> extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
return extends.OfType<T>();
|
||||
Comparer<T> comparer = Comparer<T>.Default;
|
||||
T value = default(T);
|
||||
|
||||
using (IEnumerator<T> enumerator = extends.GetEnumerator())
|
||||
{
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
T x = enumerator.Current;
|
||||
if (comparer.Compare(x, value) > 0)
|
||||
value = x;
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -1245,7 +1338,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static byte Sum(this IEnumerable<byte> extends)
|
||||
public static byte Sum([NotNull] this IEnumerable<byte> extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -1264,7 +1357,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static IEnumerable<T> Consolidate<T>(this IEnumerable<T> extends)
|
||||
public static IEnumerable<T> Consolidate<T>([NotNull] this IEnumerable<T> extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -1284,7 +1377,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="comparer"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static IEnumerable<T> Consolidate<T>(this IEnumerable<T> extends, IComparer<T> comparer)
|
||||
public static IEnumerable<T> Consolidate<T>([NotNull] this IEnumerable<T> extends,
|
||||
[NotNull] IComparer<T> comparer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -1306,7 +1400,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="sequence"></param>
|
||||
/// <param name="comparer"></param>
|
||||
/// <returns></returns>
|
||||
private static IEnumerable<T> ConsolidateIterator<T>(IEnumerable<T> sequence, IComparer<T> comparer)
|
||||
private static IEnumerable<T> ConsolidateIterator<T>([NotNull] IEnumerable<T> sequence,
|
||||
[NotNull] IComparer<T> comparer)
|
||||
{
|
||||
bool first = true;
|
||||
T last = default(T);
|
||||
@@ -1332,7 +1427,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="predicate"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool AnyAndAll<T>(this IEnumerable<T> extends, Func<T, bool> predicate)
|
||||
public static bool AnyAndAll<T>([NotNull] this IEnumerable<T> extends, [NotNull] Func<T, bool> predicate)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -1351,19 +1446,40 @@ namespace ICD.Common.Utils.Extensions
|
||||
return any;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Combines the corresponding elements of two sequences, producing a sequence of KeyValuePairs.
|
||||
/// </summary>
|
||||
/// <typeparam name="TFirst"></typeparam>
|
||||
/// <typeparam name="TSecond"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="second"></param>
|
||||
public static IEnumerable<KeyValuePair<TFirst, TSecond>> Zip<TFirst, TSecond>(
|
||||
[NotNull] this IEnumerable<TFirst> extends,
|
||||
[NotNull] IEnumerable<TSecond> second)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (second == null)
|
||||
throw new ArgumentNullException("second");
|
||||
|
||||
return extends.Zip(second, (f, s) => new KeyValuePair<TFirst, TSecond>(f, s));
|
||||
}
|
||||
|
||||
#if SIMPLSHARP
|
||||
/// <summary>
|
||||
/// Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.
|
||||
/// </summary>
|
||||
/// <typeparam name="TFirst"></typeparam>
|
||||
/// <typeparam name="TSecond"></typeparam>
|
||||
/// <param name="first"></param>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="second"></param>
|
||||
/// <param name="callback"></param>
|
||||
public static void Zip<TFirst, TSecond>(this IEnumerable<TFirst> first,
|
||||
IEnumerable<TSecond> second,
|
||||
Action<TFirst, TSecond> callback)
|
||||
public static void Zip<TFirst, TSecond>([NotNull]this IEnumerable<TFirst> extends,
|
||||
[NotNull]IEnumerable<TSecond> second,
|
||||
[NotNull]Action<TFirst, TSecond> callback)
|
||||
{
|
||||
if (first == null)
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("first");
|
||||
|
||||
if (second == null)
|
||||
@@ -1372,7 +1488,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
if (callback == null)
|
||||
throw new ArgumentNullException("callback");
|
||||
|
||||
using (IEnumerator<TFirst> enumerator1 = first.GetEnumerator())
|
||||
using (IEnumerator<TFirst> enumerator1 = extends.GetEnumerator())
|
||||
{
|
||||
using (IEnumerator<TSecond> enumerator2 = second.GetEnumerator())
|
||||
{
|
||||
@@ -1382,51 +1498,6 @@ namespace ICD.Common.Utils.Extensions
|
||||
}
|
||||
}
|
||||
|
||||
// since S# can't do anonymous types
|
||||
private struct TryParseStruct<T>
|
||||
{
|
||||
public readonly T value;
|
||||
public readonly bool isParsed;
|
||||
|
||||
public TryParseStruct(T value, bool isParsed)
|
||||
{
|
||||
this.value = value;
|
||||
this.isParsed = isParsed;
|
||||
}
|
||||
}
|
||||
|
||||
// since Func<...,T> can't specify `out` parameters
|
||||
public delegate bool TryParseDelegate<T>(string input, out T output);
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to parse each value of the enumerable,
|
||||
/// throwing away the values that don't parse correctly.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">type to parse to</typeparam>
|
||||
/// <param name="extends">enumerable of strings to parse</param>
|
||||
/// <param name="tryParseFunc">TryParse function for given type</param>
|
||||
/// <returns>enumerable of successfully parsed values</returns>
|
||||
public static IEnumerable<T> TryParseSkipFailures<T>(this IEnumerable<string> extends,
|
||||
TryParseDelegate<T> tryParseFunc)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (tryParseFunc == null)
|
||||
throw new ArgumentNullException("tryParseFunc");
|
||||
|
||||
return extends.Select(str =>
|
||||
{
|
||||
T value;
|
||||
bool isParsed = tryParseFunc(str, out value);
|
||||
return new TryParseStruct<T>(value, isParsed);
|
||||
})
|
||||
.Where(v => v.isParsed)
|
||||
.Select(v => v.value);
|
||||
}
|
||||
|
||||
#if SIMPLSHARP
|
||||
|
||||
/// <summary>
|
||||
/// Applies a specified function to the corresponding elements of two sequences, producing a sequence of the results.
|
||||
/// </summary>
|
||||
@@ -1437,9 +1508,9 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="other"></param>
|
||||
/// <param name="callback"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(this IEnumerable<TFirst> extends,
|
||||
IEnumerable<TSecond> other,
|
||||
Func<TFirst, TSecond, TResult> callback)
|
||||
public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>([NotNull]this IEnumerable<TFirst> extends,
|
||||
[NotNull]IEnumerable<TSecond> other,
|
||||
[NotNull]Func<TFirst, TSecond, TResult> callback)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -1463,9 +1534,9 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="second"></param>
|
||||
/// <param name="callback"></param>
|
||||
/// <returns></returns>
|
||||
private static IEnumerable<TResult> ZipIterator<TFirst, TSecond, TResult>(IEnumerable<TFirst> first,
|
||||
IEnumerable<TSecond> second,
|
||||
Func<TFirst, TSecond, TResult> callback)
|
||||
private static IEnumerable<TResult> ZipIterator<TFirst, TSecond, TResult>([NotNull]IEnumerable<TFirst> first,
|
||||
[NotNull]IEnumerable<TSecond> second,
|
||||
[NotNull]Func<TFirst, TSecond, TResult> callback)
|
||||
{
|
||||
using (IEnumerator<TFirst> enumerator1 = first.GetEnumerator())
|
||||
{
|
||||
@@ -1476,7 +1547,6 @@ namespace ICD.Common.Utils.Extensions
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
using System;
|
||||
#if SIMPLSHARP
|
||||
using Crestron.SimplSharp.Reflection;
|
||||
#else
|
||||
using System.Reflection;
|
||||
#endif
|
||||
using ICD.Common.Properties;
|
||||
|
||||
namespace ICD.Common.Utils.Extensions
|
||||
{
|
||||
@@ -17,7 +13,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="sender"></param>
|
||||
public static void Raise(this EventHandler extends, object sender)
|
||||
public static void Raise([CanBeNull] this EventHandler extends, object sender)
|
||||
{
|
||||
if (extends != null)
|
||||
extends(sender, EventArgs.Empty);
|
||||
@@ -30,7 +26,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="args"></param>
|
||||
public static void Raise<T>(this EventHandler<T> extends, object sender, T args)
|
||||
public static void Raise<T>([CanBeNull]this EventHandler<T> extends, object sender, [NotNull]T args)
|
||||
where T : EventArgs
|
||||
{
|
||||
if (args == null)
|
||||
@@ -39,22 +35,5 @@ namespace ICD.Common.Utils.Extensions
|
||||
if (extends != null)
|
||||
extends(sender, args);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cross-platform shim for getting MethodInfo for the delegate.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static MethodInfo GetMethodInfo(this Delegate extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
#if SIMPLSHARP
|
||||
return extends.GetMethod();
|
||||
#else
|
||||
return extends.Method;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static T ReadAsObject<T>(this JsonReader extends)
|
||||
public static T ReadAsObject<T>([NotNull] this JsonReader extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -36,7 +36,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="serializer"></param>
|
||||
/// <returns></returns>
|
||||
public static T ReadAsObject<T>(this JsonReader extends, JsonSerializer serializer)
|
||||
public static T ReadAsObject<T>([NotNull] this JsonReader extends, [NotNull] JsonSerializer serializer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -53,8 +53,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="serializer"></param>
|
||||
/// <param name="readPropertyValue"></param>
|
||||
public static void ReadObject(this JsonReader extends, JsonSerializer serializer,
|
||||
Action<string, JsonReader, JsonSerializer> readPropertyValue)
|
||||
public static void ReadObject([NotNull] this JsonReader extends, [NotNull] JsonSerializer serializer,
|
||||
[NotNull] Action<string, JsonReader, JsonSerializer> readPropertyValue)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -69,7 +69,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
return;
|
||||
|
||||
if (extends.TokenType != JsonToken.StartObject)
|
||||
throw new FormatException(string.Format("Expected {0} got {1}", JsonToken.StartObject, extends.TokenType));
|
||||
throw new FormatException(string.Format("Expected {0} got {1}", JsonToken.StartObject,
|
||||
extends.TokenType));
|
||||
|
||||
while (extends.Read())
|
||||
{
|
||||
@@ -94,7 +95,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static Type GetValueAsType(this JsonReader extends)
|
||||
public static Type GetValueAsType([NotNull] this JsonReader extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -109,7 +110,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static uint GetValueAsUInt(this JsonReader extends)
|
||||
public static uint GetValueAsUInt([NotNull] this JsonReader extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -117,7 +118,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
if (extends.TokenType == JsonToken.Integer)
|
||||
return (uint)(long)extends.Value;
|
||||
|
||||
string message = string.Format("Token {0} {1} is not {2}", extends.TokenType, extends.Value, JsonToken.Integer);
|
||||
string message = string.Format("Token {0} {1} is not {2}", extends.TokenType, extends.Value,
|
||||
JsonToken.Integer);
|
||||
throw new InvalidCastException(message);
|
||||
}
|
||||
|
||||
@@ -127,7 +129,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static int GetValueAsInt(this JsonReader extends)
|
||||
public static int GetValueAsInt([NotNull] this JsonReader extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -135,7 +137,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
if (extends.TokenType == JsonToken.Integer)
|
||||
return (int)(long)extends.Value;
|
||||
|
||||
string message = string.Format("Token {0} {1} is not {2}", extends.TokenType, extends.Value, JsonToken.Integer);
|
||||
string message = string.Format("Token {0} {1} is not {2}", extends.TokenType, extends.Value,
|
||||
JsonToken.Integer);
|
||||
throw new InvalidCastException(message);
|
||||
}
|
||||
|
||||
@@ -145,7 +148,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static string GetValueAsString(this JsonReader extends)
|
||||
public static string GetValueAsString([NotNull] this JsonReader extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -168,7 +171,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool GetValueAsBool(this JsonReader extends)
|
||||
public static bool GetValueAsBool([NotNull] this JsonReader extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -176,7 +179,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
if (extends.TokenType == JsonToken.Boolean)
|
||||
return (bool)extends.Value;
|
||||
|
||||
string message = string.Format("Token {0} {1} is not {2}", extends.TokenType, extends.Value, JsonToken.Boolean);
|
||||
string message = string.Format("Token {0} {1} is not {2}", extends.TokenType, extends.Value,
|
||||
JsonToken.Boolean);
|
||||
throw new InvalidCastException(message);
|
||||
}
|
||||
|
||||
@@ -186,7 +190,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static T GetValueAsEnum<T>(this JsonReader extends)
|
||||
public static T GetValueAsEnum<T>([NotNull] this JsonReader extends)
|
||||
where T : struct, IConvertible
|
||||
{
|
||||
if (extends == null)
|
||||
@@ -204,7 +208,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static Guid GetValueAsGuid(this JsonReader extends)
|
||||
public static Guid GetValueAsGuid([NotNull] this JsonReader extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -218,7 +222,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime GetValueAsDateTime(this JsonReader extends)
|
||||
public static DateTime GetValueAsDateTime([NotNull] this JsonReader extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -240,11 +244,15 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="format"></param>
|
||||
/// <param name="provider"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime GetValueAsDateTimeExact(this JsonReader extends, string format, IFormatProvider provider)
|
||||
public static DateTime GetValueAsDateTimeExact([NotNull] this JsonReader extends, [NotNull] string format,
|
||||
IFormatProvider provider)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (format == null)
|
||||
throw new ArgumentNullException("format");
|
||||
|
||||
#if !SIMPLSHARP
|
||||
// Newer NewtonSoft tries to be helpful by assuming that anything that looks like a DateTime must be a date.
|
||||
if (extends.DateParseHandling != DateParseHandling.None)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ICD.Common.Properties;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace ICD.Common.Utils.Extensions
|
||||
@@ -16,8 +17,16 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <typeparam name="TItem"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="reader"></param>
|
||||
public static IEnumerable<TItem> DeserializeArray<TItem>(this JsonSerializer extends, JsonReader reader)
|
||||
[CanBeNull]
|
||||
public static IEnumerable<TItem> DeserializeArray<TItem>([NotNull] this JsonSerializer extends,
|
||||
[NotNull] JsonReader reader)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (reader == null)
|
||||
throw new ArgumentNullException("reader");
|
||||
|
||||
return extends.DeserializeArray(reader, (s, r) => extends.Deserialize<TItem>(reader));
|
||||
}
|
||||
|
||||
@@ -28,8 +37,10 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="reader"></param>
|
||||
/// <param name="read"></param>
|
||||
public static IEnumerable<TItem> DeserializeArray<TItem>(this JsonSerializer extends, JsonReader reader,
|
||||
Func<JsonSerializer, JsonReader, TItem> read)
|
||||
[CanBeNull]
|
||||
public static IEnumerable<TItem> DeserializeArray<TItem>([NotNull] this JsonSerializer extends,
|
||||
[NotNull] JsonReader reader,
|
||||
[NotNull] Func<JsonSerializer, JsonReader, TItem> read)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -41,10 +52,11 @@ namespace ICD.Common.Utils.Extensions
|
||||
throw new ArgumentNullException("read");
|
||||
|
||||
if (reader.TokenType == JsonToken.Null)
|
||||
return Enumerable.Empty<TItem>();
|
||||
return null;
|
||||
|
||||
if (reader.TokenType != JsonToken.StartArray)
|
||||
throw new FormatException(string.Format("Expected token {0} got {1}", JsonToken.StartArray, reader.TokenType));
|
||||
throw new FormatException(string.Format("Expected token {0} got {1}", JsonToken.StartArray,
|
||||
reader.TokenType));
|
||||
|
||||
// ToArray to ensure everything gets read before moving onto the next token
|
||||
return DeserializeArrayIterator(extends, reader, read).ToArray();
|
||||
@@ -57,8 +69,10 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="serializer"></param>
|
||||
/// <param name="reader"></param>
|
||||
/// <param name="read"></param>
|
||||
private static IEnumerable<TItem> DeserializeArrayIterator<TItem>(JsonSerializer serializer, JsonReader reader,
|
||||
Func<JsonSerializer, JsonReader, TItem> read)
|
||||
[NotNull]
|
||||
private static IEnumerable<TItem> DeserializeArrayIterator<TItem>(
|
||||
[NotNull] JsonSerializer serializer, [NotNull] JsonReader reader,
|
||||
[NotNull] Func<JsonSerializer, JsonReader, TItem> read)
|
||||
{
|
||||
// Step into the first value
|
||||
reader.Read();
|
||||
@@ -80,8 +94,10 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <typeparam name="TValue"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="reader"></param>
|
||||
public static IEnumerable<KeyValuePair<TKey, TValue>> DeserializeDict<TKey, TValue>(this JsonSerializer extends,
|
||||
JsonReader reader)
|
||||
[CanBeNull]
|
||||
public static IEnumerable<KeyValuePair<TKey, TValue>> DeserializeDict<TKey, TValue>(
|
||||
[NotNull] this JsonSerializer extends,
|
||||
[NotNull] JsonReader reader)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -103,12 +119,12 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="reader"></param>
|
||||
/// <param name="readKey"></param>
|
||||
/// <param name="readValue"></param>
|
||||
public static IEnumerable<KeyValuePair<TKey, TValue>> DeserializeDict<TKey, TValue>(this JsonSerializer extends,
|
||||
JsonReader reader,
|
||||
Func<JsonSerializer, JsonReader,
|
||||
TKey> readKey,
|
||||
Func<JsonSerializer, JsonReader,
|
||||
TValue> readValue)
|
||||
[CanBeNull]
|
||||
public static IEnumerable<KeyValuePair<TKey, TValue>> DeserializeDict<TKey, TValue>(
|
||||
[NotNull] this JsonSerializer extends,
|
||||
[NotNull] JsonReader reader,
|
||||
[NotNull] Func<JsonSerializer, JsonReader, TKey> readKey,
|
||||
[NotNull] Func<JsonSerializer, JsonReader, TValue> readValue)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -125,11 +141,11 @@ namespace ICD.Common.Utils.Extensions
|
||||
return extends.DeserializeArray(reader, (s, r) => s.DeserializeKeyValuePair(r, readKey, readValue));
|
||||
}
|
||||
|
||||
public static KeyValuePair<TKey, TValue> DeserializeKeyValuePair<TKey, TValue>(this JsonSerializer extends, JsonReader reader,
|
||||
Func<JsonSerializer, JsonReader,
|
||||
TKey> readKey,
|
||||
Func<JsonSerializer, JsonReader,
|
||||
TValue> readValue)
|
||||
public static KeyValuePair<TKey, TValue> DeserializeKeyValuePair<TKey, TValue>(
|
||||
[NotNull] this JsonSerializer extends,
|
||||
[NotNull] JsonReader reader,
|
||||
[NotNull] Func<JsonSerializer, JsonReader, TKey> readKey,
|
||||
[NotNull] Func<JsonSerializer, JsonReader, TValue> readValue)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -144,7 +160,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
throw new ArgumentNullException("readValue");
|
||||
|
||||
if (reader.TokenType != JsonToken.StartObject)
|
||||
throw new FormatException(string.Format("Expected token {0} got {1}", JsonToken.StartObject, reader.TokenType));
|
||||
throw new FormatException(string.Format("Expected token {0} got {1}", JsonToken.StartObject,
|
||||
reader.TokenType));
|
||||
|
||||
TKey key = default(TKey);
|
||||
TValue value = default(TValue);
|
||||
@@ -155,7 +172,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
while (reader.TokenType != JsonToken.EndObject)
|
||||
{
|
||||
if (reader.TokenType != JsonToken.PropertyName)
|
||||
throw new FormatException(string.Format("Expected token {0} got {1}", JsonToken.PropertyName, reader.TokenType));
|
||||
throw new FormatException(string.Format("Expected token {0} got {1}", JsonToken.PropertyName,
|
||||
reader.TokenType));
|
||||
|
||||
string propertyName = (string)reader.Value;
|
||||
|
||||
@@ -190,8 +208,15 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="writer"></param>
|
||||
/// <param name="items"></param>
|
||||
public static void SerializeArray<TItem>(this JsonSerializer extends, JsonWriter writer, IEnumerable<TItem> items)
|
||||
public static void SerializeArray<TItem>([NotNull] this JsonSerializer extends, [NotNull] JsonWriter writer,
|
||||
[CanBeNull] IEnumerable<TItem> items)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (writer == null)
|
||||
throw new ArgumentNullException("writer");
|
||||
|
||||
extends.SerializeArray(writer, items, (s, w, item) => s.Serialize(w, item));
|
||||
}
|
||||
|
||||
@@ -203,8 +228,9 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="writer"></param>
|
||||
/// <param name="items"></param>
|
||||
/// <param name="write"></param>
|
||||
public static void SerializeArray<TItem>(this JsonSerializer extends, JsonWriter writer, IEnumerable<TItem> items,
|
||||
Action<JsonSerializer, JsonWriter, TItem> write)
|
||||
public static void SerializeArray<TItem>([NotNull] this JsonSerializer extends, [NotNull] JsonWriter writer,
|
||||
[CanBeNull] IEnumerable<TItem> items,
|
||||
[NotNull] Action<JsonSerializer, JsonWriter, TItem> write)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -237,8 +263,9 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="writer"></param>
|
||||
/// <param name="items"></param>
|
||||
public static void SerializeDict<TKey, TValue>(this JsonSerializer extends, JsonWriter writer,
|
||||
IEnumerable<KeyValuePair<TKey, TValue>> items)
|
||||
public static void SerializeDict<TKey, TValue>([NotNull] this JsonSerializer extends,
|
||||
[NotNull] JsonWriter writer,
|
||||
[CanBeNull] IEnumerable<KeyValuePair<TKey, TValue>> items)
|
||||
{
|
||||
extends.SerializeDict(writer, items,
|
||||
(s, w, k) => s.Serialize(w, k),
|
||||
@@ -255,10 +282,11 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="items"></param>
|
||||
/// <param name="writeKey"></param>
|
||||
/// <param name="writeValue"></param>
|
||||
public static void SerializeDict<TKey, TValue>(this JsonSerializer extends, JsonWriter writer,
|
||||
IEnumerable<KeyValuePair<TKey, TValue>> items,
|
||||
Action<JsonSerializer, JsonWriter, TKey> writeKey,
|
||||
Action<JsonSerializer, JsonWriter, TValue> writeValue)
|
||||
public static void SerializeDict<TKey, TValue>([NotNull] this JsonSerializer extends,
|
||||
[NotNull] JsonWriter writer,
|
||||
[CanBeNull] IEnumerable<KeyValuePair<TKey, TValue>> items,
|
||||
[NotNull] Action<JsonSerializer, JsonWriter, TKey> writeKey,
|
||||
[NotNull] Action<JsonSerializer, JsonWriter, TValue> writeValue)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -275,10 +303,11 @@ namespace ICD.Common.Utils.Extensions
|
||||
extends.SerializeArray(writer, items, (s, w, kvp) => s.SerializeKeyValuePair(w, kvp, writeKey, writeValue));
|
||||
}
|
||||
|
||||
public static void SerializeKeyValuePair<TKey, TValue>(this JsonSerializer extends, JsonWriter writer,
|
||||
KeyValuePair<TKey, TValue> kvp,
|
||||
Action<JsonSerializer, JsonWriter, TKey> writeKey,
|
||||
Action<JsonSerializer, JsonWriter, TValue> writeValue)
|
||||
public static void SerializeKeyValuePair<TKey, TValue>(
|
||||
[NotNull] this JsonSerializer extends,
|
||||
[NotNull] JsonWriter writer, KeyValuePair<TKey, TValue> kvp,
|
||||
[NotNull] Action<JsonSerializer, JsonWriter, TKey> writeKey,
|
||||
[NotNull] Action<JsonSerializer, JsonWriter, TValue> writeValue)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -303,4 +332,4 @@ namespace ICD.Common.Utils.Extensions
|
||||
writer.WriteEndObject();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="type"></param>
|
||||
[PublicAPI]
|
||||
public static void WriteType(this JsonWriter extends, Type type)
|
||||
public static void WriteType([NotNull]this JsonWriter extends, [CanBeNull]Type type)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -33,7 +33,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="propertyName"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void WriteProperty(this JsonWriter extends, string propertyName, object value)
|
||||
public static void WriteProperty([NotNull]this JsonWriter extends, string propertyName, object value)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -48,7 +48,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="propertyName"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void WriteProperty(this JsonWriter extends, string propertyName, string value)
|
||||
public static void WriteProperty([NotNull]this JsonWriter extends, string propertyName, string value)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -63,7 +63,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="propertyName"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void WriteProperty(this JsonWriter extends, string propertyName, DateTime value)
|
||||
public static void WriteProperty([NotNull]this JsonWriter extends, string propertyName, DateTime value)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -78,7 +78,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="propertyName"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void WriteProperty(this JsonWriter extends, string propertyName, bool value)
|
||||
public static void WriteProperty([NotNull]this JsonWriter extends, string propertyName, bool value)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -93,7 +93,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="propertyName"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void WriteProperty(this JsonWriter extends, string propertyName, int value)
|
||||
public static void WriteProperty([NotNull]this JsonWriter extends, string propertyName, int value)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -108,7 +108,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="propertyName"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void WriteProperty(this JsonWriter extends, string propertyName, Guid value)
|
||||
public static void WriteProperty([NotNull]this JsonWriter extends, string propertyName, Guid value)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -123,7 +123,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="propertyName"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void WriteProperty(this JsonWriter extends, string propertyName, Type value)
|
||||
public static void WriteProperty([NotNull]this JsonWriter extends, string propertyName, Type value)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ICD.Common.Properties;
|
||||
using ICD.Common.Utils.Comparers;
|
||||
|
||||
@@ -11,14 +10,159 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
public static class ListExtensions
|
||||
{
|
||||
#region Add Sorted
|
||||
|
||||
/// <summary>
|
||||
/// Adds the items into a sorted list.
|
||||
/// Attempts to add the item to the sorted list.
|
||||
/// Returns false if the item already exists in the list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool AddSorted<T>([NotNull] this IList<T> extends, T item)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
return extends.AddSorted(item, Comparer<T>.Default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to add the item to the sorted list.
|
||||
/// Returns false if the item already exists in the list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <typeparam name="TProp"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="predicate"></param>
|
||||
[PublicAPI]
|
||||
public static bool AddSorted<T, TProp>([NotNull] this IList<T> extends, T item,
|
||||
[NotNull] Func<T, TProp> predicate)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (predicate == null)
|
||||
throw new ArgumentNullException("predicate");
|
||||
|
||||
PredicateComparer<T, TProp> comparer = new PredicateComparer<T, TProp>(predicate);
|
||||
return extends.AddSorted(item, comparer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to add the item to the sorted list.
|
||||
/// Returns false if the item already exists in the list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="comparer"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool AddSorted<T>([NotNull] this IList<T> extends, T item, [NotNull] IComparer<T> comparer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (comparer == null)
|
||||
throw new ArgumentNullException("comparer");
|
||||
|
||||
int index = extends.BinarySearch(item, comparer);
|
||||
if (index >= 0)
|
||||
return false;
|
||||
|
||||
index = ~index;
|
||||
extends.Insert(index, item);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Remove Sorted
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to remove the item from the sorted list.
|
||||
/// Returns false if the item does not exist in the list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool RemoveSorted<T>([NotNull] this IList<T> extends, T item)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
return extends.RemoveSorted(item, Comparer<T>.Default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to remove the item from the sorted list.
|
||||
/// Returns false if the item does not exist in the list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <typeparam name="TProp"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="predicate"></param>
|
||||
[PublicAPI]
|
||||
public static bool RemoveSorted<T, TProp>([NotNull] this IList<T> extends, T item,
|
||||
[NotNull] Func<T, TProp> predicate)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (predicate == null)
|
||||
throw new ArgumentNullException("predicate");
|
||||
|
||||
PredicateComparer<T, TProp> comparer = new PredicateComparer<T, TProp>(predicate);
|
||||
return extends.RemoveSorted(item, comparer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to remove the item from the sorted list.
|
||||
/// Returns false if the item does not exist in the list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="comparer"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool RemoveSorted<T>([NotNull] this IList<T> extends, T item, [NotNull] IComparer<T> comparer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (comparer == null)
|
||||
throw new ArgumentNullException("comparer");
|
||||
|
||||
int index = extends.BinarySearch(item, comparer);
|
||||
if (index < 0)
|
||||
return false;
|
||||
|
||||
extends.RemoveAt(index);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Insert Sorted
|
||||
|
||||
/// <summary>
|
||||
/// Inserts the items into a sorted list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="items"></param>
|
||||
[PublicAPI]
|
||||
public static void AddSorted<T>(this List<T> extends, IEnumerable<T> items)
|
||||
public static void InsertSorted<T>([NotNull] this IList<T> extends, [NotNull] IEnumerable<T> items)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -26,18 +170,19 @@ namespace ICD.Common.Utils.Extensions
|
||||
if (items == null)
|
||||
throw new ArgumentNullException("items");
|
||||
|
||||
extends.AddSorted(items, Comparer<T>.Default);
|
||||
extends.InsertSorted(items, Comparer<T>.Default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the items into a sorted list.
|
||||
/// Inserts the items into a sorted list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="items"></param>
|
||||
/// <param name="comparer"></param>
|
||||
[PublicAPI]
|
||||
public static void AddSorted<T>(this List<T> extends, IEnumerable<T> items, IComparer<T> comparer)
|
||||
public static void InsertSorted<T>([NotNull] this IList<T> extends, [NotNull] IEnumerable<T> items,
|
||||
[NotNull] IComparer<T> comparer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -48,11 +193,11 @@ namespace ICD.Common.Utils.Extensions
|
||||
if (comparer == null)
|
||||
throw new ArgumentNullException("comparer");
|
||||
|
||||
items.ForEach(i => extends.AddSorted(i, comparer));
|
||||
items.ForEach(i => extends.InsertSorted(i, comparer));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the items into a sorted list.
|
||||
/// Inserts the items into a sorted list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <typeparam name="TProp"></typeparam>
|
||||
@@ -60,7 +205,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="items"></param>
|
||||
/// <param name="predicate"></param>
|
||||
[PublicAPI]
|
||||
public static void AddSorted<T, TProp>(this List<T> extends, IEnumerable<T> items, Func<T, TProp> predicate)
|
||||
public static void InsertSorted<T, TProp>([NotNull] this IList<T> extends, [NotNull] IEnumerable<T> items,
|
||||
[NotNull] Func<T, TProp> predicate)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -72,33 +218,55 @@ namespace ICD.Common.Utils.Extensions
|
||||
throw new ArgumentNullException("predicate");
|
||||
|
||||
PredicateComparer<T, TProp> comparer = new PredicateComparer<T, TProp>(predicate);
|
||||
extends.AddSorted(items, comparer);
|
||||
extends.InsertSorted(items, comparer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the item into a sorted list.
|
||||
/// Inserts the item into a sorted list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item"></param>
|
||||
[PublicAPI]
|
||||
public static int AddSorted<T>(this List<T> extends, T item)
|
||||
public static int InsertSorted<T>([NotNull] this IList<T> extends, T item)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
return extends.AddSorted(item, Comparer<T>.Default);
|
||||
return extends.InsertSorted(item, Comparer<T>.Default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the item into a sorted list.
|
||||
/// Inserts the item into a sorted list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <typeparam name="TProp"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="predicate"></param>
|
||||
[PublicAPI]
|
||||
public static int InsertSorted<T, TProp>([NotNull] this IList<T> extends, T item,
|
||||
[NotNull] Func<T, TProp> predicate)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (predicate == null)
|
||||
throw new ArgumentNullException("predicate");
|
||||
|
||||
PredicateComparer<T, TProp> comparer = new PredicateComparer<T, TProp>(predicate);
|
||||
return extends.InsertSorted(item, comparer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Inserts the item into a sorted list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="comparer"></param>
|
||||
[PublicAPI]
|
||||
public static int AddSorted<T>(this List<T> extends, T item, IComparer<T> comparer)
|
||||
public static int InsertSorted<T>([NotNull] this IList<T> extends, T item, [NotNull] IComparer<T> comparer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -115,8 +283,48 @@ namespace ICD.Common.Utils.Extensions
|
||||
return index;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Contains Sorted
|
||||
|
||||
/// <summary>
|
||||
/// Adds the item into a sorted list.
|
||||
/// Returns true if the sorted list contains the given item.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool ContainsSorted<T>([NotNull] this IList<T> extends, T item)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
return extends.ContainsSorted(item, Comparer<T>.Default);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the sorted list contains the given item.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="comparer"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool ContainsSorted<T>([NotNull] this IList<T> extends, T item, [NotNull] IComparer<T> comparer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (comparer == null)
|
||||
throw new ArgumentNullException("comparer");
|
||||
|
||||
return extends.BinarySearch(item, comparer) >= 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the sorted list contains the given item.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <typeparam name="TProp"></typeparam>
|
||||
@@ -124,7 +332,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="item"></param>
|
||||
/// <param name="predicate"></param>
|
||||
[PublicAPI]
|
||||
public static int AddSorted<T, TProp>(this List<T> extends, T item, Func<T, TProp> predicate)
|
||||
public static bool ContainsSorted<T, TProp>([NotNull] this IList<T> extends, T item,
|
||||
[NotNull] Func<T, TProp> predicate)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -133,48 +342,61 @@ namespace ICD.Common.Utils.Extensions
|
||||
throw new ArgumentNullException("predicate");
|
||||
|
||||
PredicateComparer<T, TProp> comparer = new PredicateComparer<T, TProp>(predicate);
|
||||
return extends.AddSorted(item, comparer);
|
||||
return extends.ContainsSorted(item, comparer);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Binary Search
|
||||
|
||||
/// <summary>
|
||||
/// Pads the list to the given total length.
|
||||
/// Returns the index of the item in the list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="totalLength"></param>
|
||||
[PublicAPI]
|
||||
public static void PadRight<T>(this List<T> extends, int totalLength)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (totalLength < 0)
|
||||
throw new ArgumentOutOfRangeException("totalLength", "totalLength must be greater or equal to 0");
|
||||
|
||||
extends.PadRight(totalLength, default(T));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pads the list to the given total length with the given item.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="totalLength"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="comparer"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static void PadRight<T>(this List<T> extends, int totalLength, T item)
|
||||
public static int BinarySearch<T>([NotNull] this IList<T> extends, T item, [NotNull] IComparer<T> comparer)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (totalLength < 0)
|
||||
throw new ArgumentOutOfRangeException("totalLength", "totalLength must be greater or equal to 0");
|
||||
if (comparer == null)
|
||||
throw new ArgumentNullException("comparer");
|
||||
|
||||
int pad = totalLength - extends.Count;
|
||||
if (pad <= 0)
|
||||
return;
|
||||
// Array
|
||||
T[] array = extends as T[];
|
||||
if (array != null)
|
||||
return Array.BinarySearch(array, 0, array.Length, item, comparer);
|
||||
|
||||
extends.AddRange(Enumerable.Repeat(item, pad));
|
||||
// List
|
||||
List<T> list = extends as List<T>;
|
||||
if (list != null)
|
||||
return list.BinarySearch(item, comparer);
|
||||
|
||||
// IList
|
||||
int lo = 0;
|
||||
int hi = extends.Count - 1;
|
||||
|
||||
while (lo <= hi)
|
||||
{
|
||||
int i = lo + ((hi - lo) >> 1);
|
||||
int order = comparer.Compare(extends[i], item);
|
||||
|
||||
if (order == 0)
|
||||
return i;
|
||||
|
||||
if (order < 0)
|
||||
lo = i + 1;
|
||||
else
|
||||
hi = i - 1;
|
||||
}
|
||||
|
||||
return ~lo;
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using ICD.Common.Properties;
|
||||
#if SIMPLSHARP
|
||||
using Crestron.SimplSharp.Reflection;
|
||||
#else
|
||||
@@ -15,8 +16,11 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="method">The Method</param>
|
||||
/// <returns>Method signature</returns>
|
||||
public static string GetSignature(this MethodInfo method)
|
||||
public static string GetSignature([NotNull]this MethodInfo method)
|
||||
{
|
||||
if (method == null)
|
||||
throw new ArgumentNullException("method");
|
||||
|
||||
return method.GetSignature(false);
|
||||
}
|
||||
|
||||
@@ -26,8 +30,11 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="method">The Method</param>
|
||||
/// <param name="callable">Return as a callable string(public void a(string b) would return a(b))</param>
|
||||
/// <returns>Method signature</returns>
|
||||
public static string GetSignature(this MethodInfo method, bool callable)
|
||||
public static string GetSignature([NotNull]this MethodInfo method, bool callable)
|
||||
{
|
||||
if (method == null)
|
||||
throw new ArgumentNullException("method");
|
||||
|
||||
bool firstParam = true;
|
||||
StringBuilder sigBuilder = new StringBuilder();
|
||||
|
||||
@@ -123,5 +130,22 @@ namespace ICD.Common.Utils.Extensions
|
||||
|
||||
return sigBuilder.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cross-platform shim for getting MethodInfo for the delegate.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static MethodInfo GetMethodInfo([NotNull]this Delegate extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
#if SIMPLSHARP
|
||||
return extends.GetMethod();
|
||||
#else
|
||||
return extends.Method;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using ICD.Common.Properties;
|
||||
#if SIMPLSHARP
|
||||
using Crestron.SimplSharp.Reflection;
|
||||
#else
|
||||
@@ -14,7 +15,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static bool GetIsOut(this ParameterInfo extends)
|
||||
public static bool GetIsOut([NotNull] this ParameterInfo extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using ICD.Common.Properties;
|
||||
|
||||
namespace ICD.Common.Utils.Extensions
|
||||
{
|
||||
@@ -11,7 +12,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="items"></param>
|
||||
public static void EnqueueRange<T>(this Queue<T> extends, IEnumerable<T> items)
|
||||
public static void EnqueueRange<T>([NotNull] this Queue<T> extends, [NotNull] IEnumerable<T> items)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -30,7 +31,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
public static bool Dequeue<T>(this Queue<T> extends, out T item)
|
||||
public static bool Dequeue<T>([NotNull] this Queue<T> extends, out T item)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -51,7 +52,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
public static bool Peek<T>(this Queue<T> extends, out T item)
|
||||
public static bool Peek<T>([NotNull] this Queue<T> extends, out T item)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using ICD.Common.Properties;
|
||||
#if SIMPLSHARP
|
||||
using Crestron.SimplSharp.Reflection;
|
||||
#else
|
||||
@@ -20,7 +21,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<T> GetCustomAttributes<T>(this ICustomAttributeProvider extends)
|
||||
public static IEnumerable<T> GetCustomAttributes<T>([NotNull] this ICustomAttributeProvider extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -35,7 +36,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="inherits"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<T> GetCustomAttributes<T>(this ICustomAttributeProvider extends, bool inherits)
|
||||
public static IEnumerable<T> GetCustomAttributes<T>([NotNull] this ICustomAttributeProvider extends,
|
||||
bool inherits)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -57,7 +59,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static T GetCustomAttribute<T>(this ICustomAttributeProvider extends)
|
||||
public static T GetCustomAttribute<T>([NotNull] this ICustomAttributeProvider extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -72,7 +74,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="inherits"></param>
|
||||
/// <returns></returns>
|
||||
public static T GetCustomAttribute<T>(this ICustomAttributeProvider extends, bool inherits)
|
||||
public static T GetCustomAttribute<T>([NotNull] this ICustomAttributeProvider extends, bool inherits)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -86,16 +88,18 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="inherits"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<T> GetCustomAttributesIncludingBaseInterfaces<T>(this Type extends)
|
||||
public static IEnumerable<T> GetCustomAttributesIncludingBaseInterfaces<T>([NotNull] this Type extends)
|
||||
where T : Attribute
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
return extends.GetCustomAttributes<T>(true)
|
||||
.Union(extends.GetInterfaces()
|
||||
.SelectMany(interfaceType => interfaceType
|
||||
.GetCustomAttributes<T>(true)))
|
||||
.Distinct();
|
||||
.Union(extends.GetInterfaces()
|
||||
.SelectMany(interfaceType => interfaceType
|
||||
.GetCustomAttributes<T>(true)))
|
||||
.Distinct();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -103,23 +107,25 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="inherits"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<T> GetCustomAttributesIncludingBaseInterfaces<T>(this MemberInfo extends)
|
||||
public static IEnumerable<T> GetCustomAttributesIncludingBaseInterfaces<T>([NotNull] this MemberInfo extends)
|
||||
where T : Attribute
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
return extends.GetCustomAttributes<T>(true)
|
||||
.Union(extends.DeclaringType?
|
||||
.GetInterfaces()
|
||||
.SelectMany(interfaceType => interfaceType
|
||||
.GetMember(
|
||||
extends.Name,
|
||||
extends.MemberType,
|
||||
BindingFlags.Instance)
|
||||
.FirstOrDefault()?
|
||||
.GetCustomAttributes<T>(true) ?? Enumerable.Empty<T>())?
|
||||
.Except(null) ?? Enumerable.Empty<T>())
|
||||
.Distinct();
|
||||
.Union(extends.DeclaringType?
|
||||
.GetInterfaces()
|
||||
.SelectMany(interfaceType => interfaceType
|
||||
.GetMember(extends.Name,
|
||||
extends.MemberType,
|
||||
BindingFlags.Instance)
|
||||
.FirstOrDefault()?
|
||||
.GetCustomAttributes<T>(true) ??
|
||||
Enumerable.Empty<T>())?
|
||||
.Except(null) ?? Enumerable.Empty<T>())
|
||||
.Distinct();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using ICD.Common.Properties;
|
||||
|
||||
namespace ICD.Common.Utils.Extensions
|
||||
{
|
||||
@@ -9,7 +10,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// Empties the StringBuilder.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
public static void Clear(this StringBuilder extends)
|
||||
public static void Clear([NotNull] this StringBuilder extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -23,7 +24,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static string Pop(this StringBuilder extends)
|
||||
public static string Pop([NotNull] this StringBuilder extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="first"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static int IndexOf(this string extends, IEnumerable<string> items, out string first)
|
||||
public static int IndexOf([NotNull] this string extends, [NotNull] IEnumerable<string> items, out string first)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -52,7 +52,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="character"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool StartsWith(this string extends, char character)
|
||||
public static bool StartsWith([NotNull] this string extends, char character)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -67,7 +67,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="character"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool EndsWith(this string extends, char character)
|
||||
public static bool EndsWith([NotNull] this string extends, char character)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -83,7 +83,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="delimeter"></param>
|
||||
/// <param name="count"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<string> Split(this string extends, char delimeter, int count)
|
||||
[NotNull]
|
||||
public static IEnumerable<string> Split([NotNull] this string extends, char delimeter, int count)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -102,7 +103,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="delimeter"></param>
|
||||
/// <param name="count"></param>
|
||||
/// <returns></returns>
|
||||
private static IEnumerable<string> SplitIterator(string value, char delimeter, int count)
|
||||
[NotNull]
|
||||
private static IEnumerable<string> SplitIterator([NotNull] string value, char delimeter, int count)
|
||||
{
|
||||
while (count > 1)
|
||||
{
|
||||
@@ -125,7 +127,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="chunkSize"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static IEnumerable<string> Split(this string extends, int chunkSize)
|
||||
[NotNull]
|
||||
public static IEnumerable<string> Split([NotNull] this string extends, int chunkSize)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -134,7 +137,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
throw new InvalidOperationException("chunkSize must be greater than 0");
|
||||
|
||||
return Enumerable.Range(0, (int)Math.Ceiling(extends.Length / (double)chunkSize))
|
||||
.Select(i => extends.Substring(i * chunkSize, Math.Min(chunkSize, extends.Length - (i * chunkSize))));
|
||||
.Select(i => extends.Substring(i * chunkSize,
|
||||
Math.Min(chunkSize, extends.Length - (i * chunkSize))));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -143,7 +147,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static string RemoveWhitespace(this string extends)
|
||||
[NotNull]
|
||||
public static string RemoveWhitespace([NotNull] this string extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -152,12 +157,13 @@ namespace ICD.Common.Utils.Extensions
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes all occurances of the given string.
|
||||
/// Removes all occurrences of the given string.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="other"></param>
|
||||
/// <returns></returns>
|
||||
public static string Remove(this string extends, string other)
|
||||
[NotNull]
|
||||
public static string Remove([NotNull] this string extends, [NotNull] string other)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -176,19 +182,21 @@ namespace ICD.Common.Utils.Extensions
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes all occurances the given characters from the string.
|
||||
/// Removes all occurrences the given characters from the string.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="characters"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static string Remove(this string extends, IEnumerable<char> characters)
|
||||
[NotNull]
|
||||
public static string Remove([NotNull] this string extends, IEnumerable<char> characters)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
if (characters == null)
|
||||
throw new ArgumentNullException("characters");
|
||||
|
||||
var cSet = characters.ToIcdHashSet();
|
||||
|
||||
return new string(extends.Where(c => !cSet.Contains(c)).ToArray());
|
||||
@@ -200,7 +208,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
[PublicAPI]
|
||||
public static bool IsNumeric(this string extends)
|
||||
public static bool IsNumeric([NotNull] this string extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -214,7 +222,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="character"></param>
|
||||
/// <returns></returns>
|
||||
public static bool Contains(this string extends, char character)
|
||||
public static bool Contains([NotNull] this string extends, char character)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -227,8 +235,11 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static int GetStableHashCode(this string extends)
|
||||
public static int GetStableHashCode([NotNull] this string extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
unchecked
|
||||
{
|
||||
int hash1 = 5381;
|
||||
|
||||
@@ -25,5 +25,48 @@ namespace ICD.Common.Utils.Extensions
|
||||
|
||||
return builder.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the given number of hours to the time, wrapping every 24 hours without modifying the day.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="hours"></param>
|
||||
/// <returns></returns>
|
||||
public static TimeSpan AddHoursAndWrap(this TimeSpan extends, int hours)
|
||||
{
|
||||
hours = MathUtils.Modulus(hours + extends.Hours, 24);
|
||||
return new TimeSpan(extends.Days, hours, extends.Minutes, extends.Seconds, extends.Milliseconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the given number of hours to the time, wrapping within the current 12 hour span, without modifying the day.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="hours"></param>
|
||||
/// <returns></returns>
|
||||
public static TimeSpan AddHoursAndWrap12Hour(this TimeSpan extends, int hours)
|
||||
{
|
||||
int currentHour = extends.Hours;
|
||||
bool am = extends.Hours < 12;
|
||||
|
||||
int current12Hour = MathUtils.Modulus(currentHour, 12);
|
||||
int new12Hour = MathUtils.Modulus(current12Hour + hours, 12);
|
||||
|
||||
return am
|
||||
? new TimeSpan(extends.Days, new12Hour, extends.Minutes, extends.Seconds, extends.Milliseconds)
|
||||
: new TimeSpan(extends.Days, new12Hour + 12, extends.Minutes, extends.Seconds, extends.Milliseconds);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the given number of minutes to the time, wrapping every 60 minutes without modifying the hour.
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <param name="minutes"></param>
|
||||
/// <returns></returns>
|
||||
public static TimeSpan AddMinutesAndWrap(this TimeSpan extends, int minutes)
|
||||
{
|
||||
minutes = MathUtils.Modulus(minutes + extends.Minutes, 60);
|
||||
return new TimeSpan(extends.Days, extends.Hours, minutes, extends.Seconds, extends.Milliseconds);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using ICD.Common.Properties;
|
||||
using ICD.Common.Utils.Collections;
|
||||
#if SIMPLSHARP
|
||||
using Crestron.SimplSharp.Reflection;
|
||||
@@ -83,7 +84,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static bool CanBeNull(this Type extends)
|
||||
public static bool CanBeNull([NotNull]this Type extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentException("extends");
|
||||
@@ -96,7 +97,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsNumeric(this Type extends)
|
||||
public static bool IsNumeric([NotNull]this Type extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentException("extends");
|
||||
@@ -109,7 +110,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsSignedNumeric(this Type extends)
|
||||
public static bool IsSignedNumeric([NotNull]this Type extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentException("extends");
|
||||
@@ -122,7 +123,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsDecimalNumeric(this Type extends)
|
||||
public static bool IsDecimalNumeric([NotNull]this Type extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentException("extends");
|
||||
@@ -135,7 +136,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsIntegerNumeric(this Type extends)
|
||||
public static bool IsIntegerNumeric([NotNull]this Type extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentException("extends");
|
||||
@@ -148,7 +149,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static Assembly GetAssembly(this Type extends)
|
||||
[NotNull]
|
||||
public static Assembly GetAssembly([NotNull]this Type extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -167,7 +169,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="from"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsAssignableTo<T>(this Type from)
|
||||
public static bool IsAssignableTo<T>([NotNull]this Type from)
|
||||
{
|
||||
if (from == null)
|
||||
throw new ArgumentNullException("from");
|
||||
@@ -181,7 +183,7 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// <param name="from"></param>
|
||||
/// <param name="to"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsAssignableTo(this Type from, Type to)
|
||||
public static bool IsAssignableTo([NotNull]this Type from, [NotNull]Type to)
|
||||
{
|
||||
if (from == null)
|
||||
throw new ArgumentNullException("from");
|
||||
@@ -197,7 +199,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Type> GetAllTypes(this Type extends)
|
||||
[NotNull]
|
||||
public static IEnumerable<Type> GetAllTypes([NotNull]this Type extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -221,7 +224,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Type> GetBaseTypes(this Type extends)
|
||||
[NotNull]
|
||||
public static IEnumerable<Type> GetBaseTypes([NotNull]this Type extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -236,7 +240,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
return types;
|
||||
}
|
||||
|
||||
private static IEnumerable<Type> GetBaseTypesIterator(Type type)
|
||||
[NotNull]
|
||||
private static IEnumerable<Type> GetBaseTypesIterator([NotNull] Type type)
|
||||
{
|
||||
do
|
||||
{
|
||||
@@ -257,7 +262,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Type> GetImmediateInterfaces(this Type extends)
|
||||
[NotNull]
|
||||
public static IEnumerable<Type> GetImmediateInterfaces([NotNull]this Type extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -286,7 +292,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Type> GetMinimalInterfaces(this Type extends)
|
||||
[NotNull]
|
||||
public static IEnumerable<Type> GetMinimalInterfaces([NotNull]this Type extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -314,7 +321,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetNameWithoutGenericArity(this Type extends)
|
||||
[NotNull]
|
||||
public static string GetNameWithoutGenericArity([NotNull]this Type extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -330,7 +338,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetMinimalName(this Type extends)
|
||||
[NotNull]
|
||||
public static string GetMinimalName([NotNull]this Type extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -380,7 +389,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetNameWithoutAssemblyDetails(this Type extends)
|
||||
[NotNull]
|
||||
public static string GetNameWithoutAssemblyDetails([NotNull]this Type extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -401,7 +411,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="fullyQualifiedTypeName"></param>
|
||||
/// <returns></returns>
|
||||
private static string RemoveAssemblyDetails(string fullyQualifiedTypeName)
|
||||
[NotNull]
|
||||
private static string RemoveAssemblyDetails([NotNull] string fullyQualifiedTypeName)
|
||||
{
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
@@ -450,7 +461,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends">Type. May be generic or nullable</param>
|
||||
/// <returns>Full type name, fully qualified namespaces</returns>
|
||||
public static string GetSyntaxName(this Type extends)
|
||||
[NotNull]
|
||||
public static string GetSyntaxName([NotNull]this Type extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using ICD.Common.Properties;
|
||||
|
||||
namespace ICD.Common.Utils.Extensions
|
||||
{
|
||||
@@ -10,7 +11,8 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetUserName(this Uri extends)
|
||||
[NotNull]
|
||||
public static string GetUserName([NotNull] this Uri extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
@@ -23,12 +25,26 @@ namespace ICD.Common.Utils.Extensions
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetPassword(this Uri extends)
|
||||
[NotNull]
|
||||
public static string GetPassword([NotNull] this Uri extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
return extends.UserInfo.Split(':').Skip(1).FirstOrDefault(string.Empty);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the URI matches the default http://localhost/
|
||||
/// </summary>
|
||||
/// <param name="extends"></param>
|
||||
/// <returns></returns>
|
||||
public static bool GetIsDefault([NotNull] this Uri extends)
|
||||
{
|
||||
if (extends == null)
|
||||
throw new ArgumentNullException("extends");
|
||||
|
||||
return extends.ToString() == "http://localhost/";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -204,16 +204,17 @@ namespace ICD.Common.Utils.Globalization
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_IsResident)
|
||||
return base.DateTimeFormat;
|
||||
|
||||
ThrowIfNeutralCulture(this);
|
||||
|
||||
if (m_DatetimeFormat == null)
|
||||
{
|
||||
if (m_IsResident)
|
||||
return base.DateTimeFormat;
|
||||
|
||||
ThrowIfNeutralCulture(this);
|
||||
|
||||
DateTimeFormatInfo dateTimeFormatInfo = GetDateTimeFormat(m_DatetimeFormatId);
|
||||
if (IsReadOnly)
|
||||
dateTimeFormatInfo = DateTimeFormatInfo.ReadOnly(dateTimeFormatInfo);
|
||||
|
||||
m_DatetimeFormat = dateTimeFormatInfo;
|
||||
}
|
||||
return m_DatetimeFormat;
|
||||
@@ -221,13 +222,13 @@ namespace ICD.Common.Utils.Globalization
|
||||
set
|
||||
{
|
||||
if (m_IsResident)
|
||||
{
|
||||
base.DateTimeFormat = value;
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowIfReadOnly();
|
||||
|
||||
if (value == null)
|
||||
throw new ArgumentException("value");
|
||||
|
||||
m_DatetimeFormat = value;
|
||||
}
|
||||
}
|
||||
@@ -246,16 +247,17 @@ namespace ICD.Common.Utils.Globalization
|
||||
{
|
||||
get
|
||||
{
|
||||
if (m_IsResident)
|
||||
return base.NumberFormat;
|
||||
|
||||
ThrowIfNeutralCulture(this);
|
||||
|
||||
if (m_NumberFormat == null)
|
||||
{
|
||||
if (m_IsResident)
|
||||
return base.NumberFormat;
|
||||
|
||||
ThrowIfNeutralCulture(this);
|
||||
|
||||
NumberFormatInfo numberFormatInfo = GetNumberFormat(m_NumberFormatId);
|
||||
if (IsReadOnly)
|
||||
numberFormatInfo = NumberFormatInfo.ReadOnly(numberFormatInfo);
|
||||
|
||||
m_NumberFormat = numberFormatInfo;
|
||||
}
|
||||
return m_NumberFormat;
|
||||
@@ -263,13 +265,13 @@ namespace ICD.Common.Utils.Globalization
|
||||
set
|
||||
{
|
||||
if (m_IsResident)
|
||||
{
|
||||
base.NumberFormat = value;
|
||||
return;
|
||||
}
|
||||
|
||||
ThrowIfReadOnly();
|
||||
|
||||
if (value == null)
|
||||
throw new ArgumentException("value");
|
||||
|
||||
m_NumberFormat = value;
|
||||
}
|
||||
}
|
||||
@@ -851,24 +853,25 @@ namespace ICD.Common.Utils.Globalization
|
||||
try
|
||||
{
|
||||
m_CompareInfo = ci.CompareInfo;
|
||||
goto IL_CE;
|
||||
}
|
||||
catch (PlatformNotSupportedException)
|
||||
{
|
||||
m_CompareInfo = CultureInfo.InvariantCulture.CompareInfo;
|
||||
goto IL_CE;
|
||||
}
|
||||
}
|
||||
m_Calendar = icdCultureInfo.m_Calendar;
|
||||
m_CalendarType = icdCultureInfo.m_CalendarType;
|
||||
m_GregorianCalendarType = icdCultureInfo.m_GregorianCalendarType;
|
||||
m_OptionalCalendars = icdCultureInfo.m_OptionalCalendars;
|
||||
m_OptionalCalendarTypes = icdCultureInfo.m_OptionalCalendarTypes;
|
||||
m_OptionalGregorianCalendarTypes = icdCultureInfo.m_OptionalGregorianCalendarTypes;
|
||||
m_Parent = icdCultureInfo.m_Parent;
|
||||
m_TextInfo = icdCultureInfo.m_TextInfo;
|
||||
m_CompareInfo = icdCultureInfo.m_CompareInfo;
|
||||
IL_CE:
|
||||
else
|
||||
{
|
||||
m_Calendar = icdCultureInfo.m_Calendar;
|
||||
m_CalendarType = icdCultureInfo.m_CalendarType;
|
||||
m_GregorianCalendarType = icdCultureInfo.m_GregorianCalendarType;
|
||||
m_OptionalCalendars = icdCultureInfo.m_OptionalCalendars;
|
||||
m_OptionalCalendarTypes = icdCultureInfo.m_OptionalCalendarTypes;
|
||||
m_OptionalGregorianCalendarTypes = icdCultureInfo.m_OptionalGregorianCalendarTypes;
|
||||
m_Parent = icdCultureInfo.m_Parent;
|
||||
m_TextInfo = icdCultureInfo.m_TextInfo;
|
||||
m_CompareInfo = icdCultureInfo.m_CompareInfo;
|
||||
}
|
||||
|
||||
m_EnglishName = ci.EnglishName;
|
||||
m_IsNeutralCulture = ci.IsNeutralCulture;
|
||||
m_Lcid = ci.LCID;
|
||||
@@ -877,19 +880,21 @@ namespace ICD.Common.Utils.Globalization
|
||||
m_ThreeLetterIsoLanguageName = ci.ThreeLetterISOLanguageName;
|
||||
m_ThreeLetterWindowsLanguageName = ci.ThreeLetterWindowsLanguageName;
|
||||
m_TwoLetterIsoLanguageName = ci.TwoLetterISOLanguageName;
|
||||
if (!m_IsNeutralCulture)
|
||||
|
||||
if (m_IsNeutralCulture)
|
||||
return;
|
||||
|
||||
if (icdCultureInfo == null)
|
||||
{
|
||||
if (icdCultureInfo == null)
|
||||
{
|
||||
m_DatetimeFormat = ci.DateTimeFormat;
|
||||
m_NumberFormat = ci.NumberFormat;
|
||||
return;
|
||||
}
|
||||
m_DatetimeFormatId = icdCultureInfo.m_DatetimeFormatId;
|
||||
m_DatetimeFormat = icdCultureInfo.m_DatetimeFormat;
|
||||
m_NumberFormatId = icdCultureInfo.m_NumberFormatId;
|
||||
m_NumberFormat = icdCultureInfo.m_NumberFormat;
|
||||
m_DatetimeFormat = ci.DateTimeFormat.Clone() as DateTimeFormatInfo;
|
||||
m_NumberFormat = ci.NumberFormat.Clone() as NumberFormatInfo;
|
||||
return;
|
||||
}
|
||||
|
||||
m_DatetimeFormatId = icdCultureInfo.m_DatetimeFormatId;
|
||||
m_DatetimeFormat = icdCultureInfo.m_DatetimeFormat.Clone() as DateTimeFormatInfo;
|
||||
m_NumberFormatId = icdCultureInfo.m_NumberFormatId;
|
||||
m_NumberFormat = icdCultureInfo.m_NumberFormat.Clone() as NumberFormatInfo;
|
||||
}
|
||||
|
||||
#region Methods
|
||||
|
||||
@@ -86,6 +86,7 @@
|
||||
<Compile Include="Comparers\PredicateComparer.cs" />
|
||||
<Compile Include="Comparers\SequenceComparer.cs" />
|
||||
<Compile Include="ConsoleColor.cs" />
|
||||
<Compile Include="DateTimeUtils.cs" />
|
||||
<Compile Include="Email\EmailClient.cs" />
|
||||
<Compile Include="Email\eMailErrorCode.cs" />
|
||||
<Compile Include="Email\EmailStringCollection.cs" />
|
||||
@@ -109,6 +110,7 @@
|
||||
<None Include="ObfuscationSettings.cs" />
|
||||
<Compile Include="Extensions\BoolExtensions.cs" />
|
||||
<Compile Include="Extensions\ByteExtensions.cs" />
|
||||
<Compile Include="Extensions\CultureInfoExtensions.cs" />
|
||||
<Compile Include="Extensions\DayOfWeekExtensions.cs" />
|
||||
<Compile Include="Extensions\JsonSerializerExtensions.cs" />
|
||||
<Compile Include="Extensions\JsonTokenExtensions.cs" />
|
||||
@@ -127,6 +129,7 @@
|
||||
<Compile Include="IO\eSeekOrigin.cs" />
|
||||
<Compile Include="IO\IcdStreamWriter.cs" />
|
||||
<Compile Include="Json\ToStringJsonConverter.cs" />
|
||||
<Compile Include="NullObject.cs" />
|
||||
<Compile Include="ProcessorUtils.SimplSharp.cs" />
|
||||
<Compile Include="ProcessorUtils.Standard.cs" />
|
||||
<Compile Include="ProgramUtils.SimplSharp.cs" />
|
||||
|
||||
@@ -3,11 +3,19 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using Crestron.SimplSharp;
|
||||
using ICD.Common.Properties;
|
||||
using ICD.Common.Utils.Extensions;
|
||||
|
||||
namespace ICD.Common.Utils
|
||||
{
|
||||
public static partial class IcdEnvironment
|
||||
{
|
||||
/// <summary>
|
||||
/// For some reason crestron returns "Invalid Value" for ethernet parameters sometimes :/
|
||||
/// </summary>
|
||||
private const string INVALID_VALUE = "Invalid Value";
|
||||
|
||||
#region Properties
|
||||
|
||||
public static string NewLine { get { return CrestronEnvironment.NewLine; } }
|
||||
|
||||
public static eRuntimeEnvironment RuntimeEnvironment
|
||||
@@ -30,36 +38,27 @@ namespace ICD.Common.Utils
|
||||
{
|
||||
const CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET param =
|
||||
CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_CURRENT_IP_ADDRESS;
|
||||
const EthernetAdapterType primaryType = EthernetAdapterType.EthernetLANAdapter;
|
||||
const EthernetAdapterType secondaryType = EthernetAdapterType.EthernetLAN2Adapter;
|
||||
|
||||
string address1 = null;
|
||||
|
||||
try
|
||||
foreach (EthernetAdapterType type in EnumUtils.GetValuesExceptNone<EthernetAdapterType>())
|
||||
{
|
||||
short id = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(primaryType);
|
||||
address1 = CrestronEthernetHelper.GetEthernetParameter(param, id);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(address1))
|
||||
yield return address1;
|
||||
string address;
|
||||
|
||||
string address2 = null;
|
||||
try
|
||||
{
|
||||
short id = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(type);
|
||||
if (id >= InitialParametersClass.NumberOfEthernetInterfaces)
|
||||
continue;
|
||||
|
||||
try
|
||||
{
|
||||
short adapter2Type = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(secondaryType);
|
||||
address2 = CrestronEthernetHelper.GetEthernetParameter(param, adapter2Type);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
}
|
||||
address = CrestronEthernetHelper.GetEthernetParameter(param, id);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(address2))
|
||||
yield return address2;
|
||||
if (!string.IsNullOrEmpty(address) && !address.Equals(INVALID_VALUE))
|
||||
yield return address;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,36 +72,27 @@ namespace ICD.Common.Utils
|
||||
{
|
||||
const CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET param =
|
||||
CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_MAC_ADDRESS;
|
||||
const EthernetAdapterType primaryType = EthernetAdapterType.EthernetLANAdapter;
|
||||
const EthernetAdapterType secondaryType = EthernetAdapterType.EthernetLAN2Adapter;
|
||||
|
||||
string macAddress1 = null;
|
||||
|
||||
try
|
||||
foreach (EthernetAdapterType type in EnumUtils.GetValuesExceptNone<EthernetAdapterType>())
|
||||
{
|
||||
short id = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(primaryType);
|
||||
macAddress1 = CrestronEthernetHelper.GetEthernetParameter(param, id);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
}
|
||||
string macAddress;
|
||||
|
||||
if (!string.IsNullOrEmpty(macAddress1))
|
||||
yield return macAddress1;
|
||||
try
|
||||
{
|
||||
short id = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(type);
|
||||
if (id >= InitialParametersClass.NumberOfEthernetInterfaces)
|
||||
continue;
|
||||
|
||||
string macAddress2 = null;
|
||||
macAddress = CrestronEthernetHelper.GetEthernetParameter(param, id);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
short id = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(secondaryType);
|
||||
macAddress2 = CrestronEthernetHelper.GetEthernetParameter(param, id);
|
||||
if (!string.IsNullOrEmpty(macAddress) && !macAddress.Equals(INVALID_VALUE))
|
||||
yield return macAddress;
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(macAddress2))
|
||||
yield return macAddress2;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,7 +111,15 @@ namespace ICD.Common.Utils
|
||||
try
|
||||
{
|
||||
short id = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(type);
|
||||
return CrestronEthernetHelper.GetEthernetParameter(param, id);
|
||||
if (id >= InitialParametersClass.NumberOfEthernetInterfaces)
|
||||
return null;
|
||||
|
||||
string status = CrestronEthernetHelper.GetEthernetParameter(param, id);
|
||||
|
||||
if (!string.IsNullOrEmpty(status) && !status.Equals(INVALID_VALUE))
|
||||
return status;
|
||||
|
||||
return null;
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
@@ -140,40 +138,32 @@ namespace ICD.Common.Utils
|
||||
{
|
||||
const CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET param =
|
||||
CrestronEthernetHelper.ETHERNET_PARAMETER_TO_GET.GET_HOSTNAME;
|
||||
const EthernetAdapterType primaryType = EthernetAdapterType.EthernetLANAdapter;
|
||||
const EthernetAdapterType secondaryType = EthernetAdapterType.EthernetLAN2Adapter;
|
||||
|
||||
string address1 = null;
|
||||
|
||||
try
|
||||
foreach (EthernetAdapterType type in EnumUtils.GetValuesExceptNone<EthernetAdapterType>())
|
||||
{
|
||||
short id = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(primaryType);
|
||||
address1 = CrestronEthernetHelper.GetEthernetParameter(param, id);
|
||||
string hostname;
|
||||
|
||||
try
|
||||
{
|
||||
short id = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(type);
|
||||
if (id >= InitialParametersClass.NumberOfEthernetInterfaces)
|
||||
continue;
|
||||
|
||||
hostname = CrestronEthernetHelper.GetEthernetParameter(param, id);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(hostname) && !hostname.Equals(INVALID_VALUE))
|
||||
yield return hostname;
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(address1))
|
||||
yield return address1;
|
||||
|
||||
string address2 = null;
|
||||
|
||||
try
|
||||
{
|
||||
|
||||
short adapter2Type = CrestronEthernetHelper.GetAdapterdIdForSpecifiedAdapterType(secondaryType);
|
||||
address2 = CrestronEthernetHelper.GetEthernetParameter(param, adapter2Type);
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(address2))
|
||||
yield return address2;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Static constructor.
|
||||
/// </summary>
|
||||
@@ -190,6 +180,18 @@ namespace ICD.Common.Utils
|
||||
return CrestronEnvironment.GetLocalTime();
|
||||
}
|
||||
|
||||
public static void SetLocalTime(DateTime localTime)
|
||||
{
|
||||
CrestronEnvironment.SetTimeAndDate((ushort)localTime.Hour,
|
||||
(ushort)localTime.Minute,
|
||||
(ushort)localTime.Second,
|
||||
(ushort)localTime.Month,
|
||||
(ushort)localTime.Day,
|
||||
(ushort)localTime.Year);
|
||||
|
||||
OnSystemDateTimeChanged.Raise(null);
|
||||
}
|
||||
|
||||
public static eEthernetEventType GetEthernetEventType(Crestron.SimplSharp.eEthernetEventType type)
|
||||
{
|
||||
switch (type)
|
||||
|
||||
@@ -79,6 +79,16 @@ namespace ICD.Common.Utils
|
||||
return DateTime.Now;
|
||||
}
|
||||
|
||||
public static void SetLocalTime(DateTime localTime)
|
||||
{
|
||||
#if DEBUG
|
||||
IcdConsole.PrintLine(eConsoleColor.Magenta, "Debug Build - Skipped setting local time to {0}",
|
||||
localTime.ToString("s"));
|
||||
#else
|
||||
throw new NotSupportedException();
|
||||
#endif
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts 12 digit address to XX:XX:XX... format
|
||||
/// </summary>
|
||||
|
||||
@@ -62,6 +62,11 @@ namespace ICD.Common.Utils
|
||||
/// </summary>
|
||||
public static event EventHandler OnProgramInitializationComplete;
|
||||
|
||||
/// <summary>
|
||||
/// Raised when the system date/time has been set.
|
||||
/// </summary>
|
||||
public static event EventHandler OnSystemDateTimeChanged;
|
||||
|
||||
private static readonly SafeCriticalSection s_ProgramInitializationSection = new SafeCriticalSection();
|
||||
private static bool s_ProgramInitializationComplete;
|
||||
|
||||
@@ -92,5 +97,15 @@ namespace ICD.Common.Utils
|
||||
|
||||
OnProgramInitializationComplete.Raise(null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets UTC time
|
||||
/// Uses GetLocalTime so Crestron Env will have ms percision
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static DateTime GetUtcTime()
|
||||
{
|
||||
return GetLocalTime().ToUniversalTime();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,5 +209,31 @@ namespace ICD.Common.Utils
|
||||
|
||||
return nearest.Aggregate((x, y) => Math.Abs(x - number) < Math.Abs(y - number) ? x : y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the modulus of the given number.
|
||||
/// </summary>
|
||||
/// <param name="number"></param>
|
||||
/// <param name="mod"></param>
|
||||
/// <returns></returns>
|
||||
/// <remarks>Method name can't be "Mod", due to S+ compatibility issues</remarks>
|
||||
public static int Modulus(int number, int mod)
|
||||
{
|
||||
int remainder = number % mod;
|
||||
return remainder < 0 ? remainder + mod : remainder;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Calculates the modulus of the given number.
|
||||
/// </summary>
|
||||
/// <param name="number"></param>
|
||||
/// <param name="mod"></param>
|
||||
/// <returns></returns>
|
||||
/// <remarks>Method name can't be "Mod", due to S+ compatibility issues</remarks>
|
||||
public static long Modulus(long number, long mod)
|
||||
{
|
||||
long remainder = number % mod;
|
||||
return remainder < 0 ? remainder + mod : remainder;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
121
ICD.Common.Utils/NullObject.cs
Normal file
121
ICD.Common.Utils/NullObject.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace ICD.Common.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Convenience wrapper for supporting null keys in hash tables.
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
public struct NullObject<T> : IEquatable<NullObject<T>>, IComparable<NullObject<T>>
|
||||
{
|
||||
#region Properties
|
||||
|
||||
public T Item { get; private set; }
|
||||
|
||||
public bool IsNull { get; private set; }
|
||||
|
||||
public static NullObject<T> Null { get { return new NullObject<T>(); } }
|
||||
|
||||
#endregion
|
||||
|
||||
#region Constructors
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
public NullObject(T item)
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
: this(item, item == null)
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructor.
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <param name="isnull"></param>
|
||||
private NullObject(T item, bool isnull)
|
||||
: this()
|
||||
{
|
||||
IsNull = isnull;
|
||||
Item = item;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
// ReSharper disable CompareNonConstrainedGenericWithNull
|
||||
return Item == null ? "NULL" : Item.ToString();
|
||||
// ReSharper restore CompareNonConstrainedGenericWithNull
|
||||
}
|
||||
|
||||
#region Casting
|
||||
|
||||
public static implicit operator T(NullObject<T> nullObject)
|
||||
{
|
||||
return nullObject.Item;
|
||||
}
|
||||
|
||||
public static implicit operator NullObject<T>(T item)
|
||||
{
|
||||
return new NullObject<T>(item);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Equality
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
if (obj == null)
|
||||
return IsNull;
|
||||
|
||||
if (!(obj is NullObject<T>))
|
||||
return false;
|
||||
|
||||
return Equals((NullObject<T>)obj);
|
||||
}
|
||||
|
||||
public bool Equals(NullObject<T> other)
|
||||
{
|
||||
if (IsNull)
|
||||
return other.IsNull;
|
||||
|
||||
return !other.IsNull && Item.Equals(other.Item);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
if (IsNull)
|
||||
return 0;
|
||||
|
||||
var result = Item.GetHashCode();
|
||||
|
||||
if (result >= 0)
|
||||
result++;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Comparable
|
||||
|
||||
public int CompareTo(NullObject<T> other)
|
||||
{
|
||||
if (IsNull && other.IsNull)
|
||||
return 0;
|
||||
|
||||
if (IsNull)
|
||||
return -1;
|
||||
|
||||
return Comparer<T>.Default.Compare(Item, other.Item);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -48,7 +48,7 @@ namespace ICD.Common.Utils
|
||||
#elif LINUX
|
||||
return Join(RootPath, "opt", "ICD.Connect");
|
||||
#else
|
||||
return Join(RootPath, "ProgramData", "ICD.Connect");
|
||||
return Join(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "ICD.Connect");
|
||||
#endif
|
||||
}
|
||||
}
|
||||
@@ -84,6 +84,37 @@ namespace ICD.Common.Utils
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the absolute path to the program data directory.
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
[PublicAPI]
|
||||
public static string ProgramDataPath { get { return Join(RootConfigPath, ProgramDataDirectory); } }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the name of the program data directory.
|
||||
/// This directory contains runtime program data that should be retained through deployments.
|
||||
/// </summary>
|
||||
public static string ProgramDataDirectory
|
||||
{
|
||||
get
|
||||
{
|
||||
switch (IcdEnvironment.RuntimeEnvironment)
|
||||
{
|
||||
case IcdEnvironment.eRuntimeEnvironment.SimplSharp:
|
||||
case IcdEnvironment.eRuntimeEnvironment.SimplSharpPro:
|
||||
case IcdEnvironment.eRuntimeEnvironment.Standard:
|
||||
return string.Format("Program{0:D2}Data", ProgramUtils.ProgramNumber);
|
||||
|
||||
case IcdEnvironment.eRuntimeEnvironment.SimplSharpProMono:
|
||||
return "ProgramData";
|
||||
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the absolute path to the common configuration directory.
|
||||
/// </summary>
|
||||
@@ -247,6 +278,37 @@ namespace ICD.Common.Utils
|
||||
return Join(ProgramConfigPath, local);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Appends the local path to the program data path.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetProgramDataPath(params string[] localPath)
|
||||
{
|
||||
string local = Join(localPath);
|
||||
return Join(ProgramDataPath, local);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///Appends the local path to the room data path.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetRoomDataPath(int roomId, params string[] localPath)
|
||||
{
|
||||
string local = Join(localPath);
|
||||
string roomDataDirectory = GetRoomDataDirectory(roomId);
|
||||
return Join(ProgramDataPath, roomDataDirectory, local);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the directory name of the room data directory for the room with the given id.
|
||||
/// </summary>
|
||||
/// <param name="roomId"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetRoomDataDirectory(int roomId)
|
||||
{
|
||||
return string.Format("Room{0}Data", roomId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if the given path exists.
|
||||
/// </summary>
|
||||
|
||||
@@ -3,5 +3,5 @@ using System.Reflection;
|
||||
[assembly: AssemblyTitle("ICD.Common.Utils")]
|
||||
[assembly: AssemblyCompany("ICD Systems")]
|
||||
[assembly: AssemblyProduct("ICD.Common.Utils")]
|
||||
[assembly: AssemblyCopyright("Copyright © ICD Systems 2019")]
|
||||
[assembly: AssemblyVersion("9.8.0.0")]
|
||||
[assembly: AssemblyCopyright("Copyright © ICD Systems 2020")]
|
||||
[assembly: AssemblyVersion("10.3.0.0")]
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using ICD.Common.Properties;
|
||||
using ICD.Common.Utils.Extensions;
|
||||
using ICD.Common.Utils.IO;
|
||||
using MethodInfoExtensions = ICD.Common.Utils.Extensions.MethodInfoExtensions;
|
||||
#if SIMPLSHARP
|
||||
using Crestron.SimplSharp.CrestronIO;
|
||||
using Crestron.SimplSharp.Reflection;
|
||||
@@ -50,13 +51,7 @@ namespace ICD.Common.Utils
|
||||
if (parameters == null)
|
||||
throw new ArgumentNullException("parameters");
|
||||
|
||||
#if SIMPLSHARP
|
||||
IEnumerable<CType>
|
||||
#else
|
||||
IEnumerable<Type>
|
||||
#endif
|
||||
parameterTypes = method.GetParameters().Select(p => p.ParameterType);
|
||||
|
||||
IEnumerable<Type> parameterTypes = method.GetParameters().Select(p => (Type)p.ParameterType);
|
||||
return ParametersMatchTypes(parameterTypes, parameters);
|
||||
}
|
||||
|
||||
@@ -80,13 +75,7 @@ namespace ICD.Common.Utils
|
||||
/// <param name="types"></param>
|
||||
/// <param name="parameters"></param>
|
||||
/// <returns></returns>
|
||||
private static bool ParametersMatchTypes(
|
||||
#if SIMPLSHARP
|
||||
IEnumerable<CType>
|
||||
#else
|
||||
IEnumerable<Type>
|
||||
#endif
|
||||
types, IEnumerable<object> parameters)
|
||||
private static bool ParametersMatchTypes(IEnumerable<Type> types, IEnumerable<object> parameters)
|
||||
{
|
||||
if (types == null)
|
||||
throw new ArgumentNullException("types");
|
||||
@@ -94,27 +83,11 @@ namespace ICD.Common.Utils
|
||||
if (parameters == null)
|
||||
throw new ArgumentNullException("parameters");
|
||||
|
||||
return types
|
||||
#if SIMPLSHARP
|
||||
IList<CType>
|
||||
#else
|
||||
IList<Type>
|
||||
.Cast<object>()
|
||||
#endif
|
||||
typesArray = types as
|
||||
#if SIMPLSHARP
|
||||
IList<CType>
|
||||
#else
|
||||
IList<Type>
|
||||
#endif
|
||||
?? types.ToArray();
|
||||
|
||||
IList<object> parametersArray = parameters as IList<object> ?? parameters.ToArray();
|
||||
|
||||
if (parametersArray.Count != typesArray.Count)
|
||||
return false;
|
||||
|
||||
// Compares each pair of items in the two arrays.
|
||||
return !parametersArray.Where((t, index) => !ParameterMatchesType(typesArray[index], t))
|
||||
.Any();
|
||||
.SequenceEqual(parameters, (a, b) => ParameterMatchesType((Type)a, b));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -163,7 +136,7 @@ namespace ICD.Common.Utils
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Platform independant delegate instantiation.
|
||||
/// Platform independent delegate instantiation.
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="firstArgument"></param>
|
||||
@@ -222,13 +195,10 @@ namespace ICD.Common.Utils
|
||||
throw new ArgumentNullException("parameters");
|
||||
try
|
||||
{
|
||||
|
||||
if (parameters.Length == 0)
|
||||
return Activator.CreateInstance(type);
|
||||
|
||||
ConstructorInfo constructor = GetConstructor(type, parameters);
|
||||
|
||||
|
||||
return constructor.Invoke(parameters);
|
||||
}
|
||||
catch (TypeLoadException e)
|
||||
@@ -376,7 +346,7 @@ namespace ICD.Common.Utils
|
||||
|
||||
object handler = eventHandler.Target;
|
||||
// ReSharper disable InvokeAsExtensionMethod
|
||||
MethodInfo callback = EventHandlerExtensions.GetMethodInfo(eventHandler);
|
||||
MethodInfo callback = MethodInfoExtensions.GetMethodInfo(eventHandler);
|
||||
// ReSharper restore InvokeAsExtensionMethod
|
||||
|
||||
return SubscribeEvent(instance, eventInfo, handler, callback);
|
||||
@@ -400,7 +370,7 @@ namespace ICD.Common.Utils
|
||||
|
||||
object handler = eventHandler.Target;
|
||||
// ReSharper disable InvokeAsExtensionMethod
|
||||
MethodInfo callback = EventHandlerExtensions.GetMethodInfo(eventHandler);
|
||||
MethodInfo callback = MethodInfoExtensions.GetMethodInfo(eventHandler);
|
||||
// ReSharper restore InvokeAsExtensionMethod
|
||||
|
||||
return SubscribeEvent(instance, eventInfo, handler, callback);
|
||||
|
||||
@@ -13,9 +13,10 @@ namespace ICD.Common.Utils
|
||||
if (callback == null)
|
||||
throw new ArgumentNullException("callback");
|
||||
|
||||
Enter();
|
||||
|
||||
try
|
||||
{
|
||||
Enter();
|
||||
callback();
|
||||
}
|
||||
finally
|
||||
@@ -35,9 +36,10 @@ namespace ICD.Common.Utils
|
||||
if (callback == null)
|
||||
throw new ArgumentNullException("callback");
|
||||
|
||||
Enter();
|
||||
|
||||
try
|
||||
{
|
||||
Enter();
|
||||
return callback();
|
||||
}
|
||||
finally
|
||||
|
||||
@@ -39,7 +39,7 @@ namespace ICD.Common.Utils.Services.Scheduler
|
||||
try
|
||||
{
|
||||
Subscribe(action);
|
||||
m_Actions.AddSorted(action, a => a.NextRunTime);
|
||||
m_Actions.InsertSorted(action, a => a.NextRunTime);
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -195,7 +195,7 @@ namespace ICD.Common.Utils.Services.Scheduler
|
||||
try
|
||||
{
|
||||
m_Actions.Remove(action);
|
||||
m_Actions.AddSorted(action, a => a.NextRunTime);
|
||||
m_Actions.InsertSorted(action, a => a.NextRunTime);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
@@ -32,6 +32,9 @@ namespace ICD.Common.Utils.Sqlite
|
||||
/// <param name="path"></param>
|
||||
public static void CreateFile(string path)
|
||||
{
|
||||
string directory = IcdPath.GetDirectoryName(path);
|
||||
IcdDirectory.CreateDirectory(directory);
|
||||
|
||||
using (IcdFileStream fs = IcdFile.Create(path))
|
||||
fs.Close();
|
||||
}
|
||||
|
||||
@@ -178,7 +178,7 @@ namespace ICD.Common.Utils.Timers
|
||||
long duration = stopwatch.ElapsedTicks;
|
||||
stopwatch.Stop();
|
||||
|
||||
orderedMs.AddSorted(duration);
|
||||
orderedMs.InsertSorted(duration);
|
||||
totalTicks += duration;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,10 @@ namespace ICD.Common.Utils.Xml
|
||||
/// Creates a new instance of T.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
protected abstract T Instantiate();
|
||||
protected virtual T Instantiate()
|
||||
{
|
||||
return ReflectionUtils.CreateInstance<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes the XML representation of the object.
|
||||
|
||||
Reference in New Issue
Block a user