Skip to content

Commit

Permalink
Array.Exist and Array.Find.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel committed Jul 7, 2023
1 parent 5ddf62d commit 99f84b9
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 11 deletions.
11 changes: 11 additions & 0 deletions specs/Qowaiv.Specs/Extensions/System.Array.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Runtime.CompilerServices;

namespace System;

internal static class QowaivArrayExtensions
{
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Exists<T>(this T[] array, Predicate<T> match)
=> Array.Exists(array, match);
}
11 changes: 6 additions & 5 deletions specs/Qowaiv.Specs/TestTools/SingleValueObjectSpecs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ public static IEnumerable<Type> JsonSerializable
});

private static bool IsJsonSerializable(Type type)
=> type.GetMethods(BindingFlags.Static | BindingFlags.Public)
.Any(m => m.Name == nameof(Date.FromJson)
&& m.ReturnType == type
&& m.GetParameters().Length == 1
&& m.GetParameters()[0].ParameterType == typeof(string));
=> type
.GetMethods(BindingFlags.Static | BindingFlags.Public)
.Exists(m => m.Name == nameof(Date.FromJson)
&& m.ReturnType == type
&& m.GetParameters().Length == 1
&& m.GetParameters()[0].ParameterType == typeof(string));
}
}
11 changes: 11 additions & 0 deletions src/Qowaiv.TestTools/Extensions/System.Array.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Runtime.CompilerServices;

namespace System;

internal static class QowaivArrayExtensions
{
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T? Find<T>(this T[] array, Predicate<T> match)
=> Array.Find(array, match);
}
2 changes: 1 addition & 1 deletion src/Qowaiv.TestTools/Globalization/TestTimeZones.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static class TestTimeZones
bool disableDaylightSavingTime)
{
var ctor = typeof(TimeZoneInfo).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)
.FirstOrDefault(ct => ct.GetParameters() is { Length: >= 7 } pars
.Find(ct => ct.GetParameters() is { Length: >= 7 } pars
&& pars[0].ParameterType == typeof(string)
&& pars[1].ParameterType == typeof(TimeSpan)
&& pars[6].ParameterType == typeof(bool))
Expand Down
6 changes: 2 additions & 4 deletions src/Qowaiv.TestTools/JsonTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static string ToString(object? val)
var parameterType = val?.GetType();
var fromJson = typeof(T)
.GetMethods(BindingFlags.Static | BindingFlags.Public)
.FirstOrDefault(m => FromJson<T>(m, parameterType));
.Find(m => FromJson<T>(m, parameterType));

if (fromJson is null)
{
Expand All @@ -77,9 +77,7 @@ static string ToString(object? val)
[Pure]
public static object? Write<T>(T val)
{
var toJson = typeof(T)
.GetMethods(BindingFlags.Instance | BindingFlags.Public)
.FirstOrDefault(ToJson);
var toJson = typeof(T).GetMethods(BindingFlags.Instance | BindingFlags.Public).Find(ToJson);

return toJson is null
? throw new InvalidOperationException($"Could not find {typeof(T).Name}.ToJson().")
Expand Down
15 changes: 15 additions & 0 deletions src/Qowaiv/Extensions/System.Array.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Runtime.CompilerServices;

namespace System;

internal static class QowaivArrayExtensions
{
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool Exists<T>(this T[] array, Predicate<T> match)
=> Array.Exists(array, match);

[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool None<T>(this T[] array) => array.Length == 0;
}
2 changes: 1 addition & 1 deletion src/Qowaiv/OpenApi/OpenApiDataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,5 +142,5 @@ private static Type AsDataType(Type type)

[Pure]
private static bool HasPublicParameterlessCtor(Type type)
=> type.GetConstructors().Any(ctor => !ctor.GetParameters().Any());
=> type.GetConstructors().Exists(ctor => ctor.GetParameters().None());
}

0 comments on commit 99f84b9

Please sign in to comment.