Skip to content

Commit f243bb0

Browse files
committed
Added new internal predicate types
1 parent 6fe63a6 commit f243bb0

File tree

6 files changed

+205
-0
lines changed

6 files changed

+205
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace Microsoft.Toolkit.Uwp.UI.Extensions.Tree
6+
{
7+
/// <summary>
8+
/// An interface representing a predicate for items of a given type.
9+
/// </summary>
10+
/// <typeparam name="T">The type of items to match.</typeparam>
11+
internal interface IPredicate<T>
12+
{
13+
/// <summary>
14+
/// Performs a match with the current predicate over a target <typeparamref name="T"/> instance.
15+
/// </summary>
16+
/// <param name="element">The input element to match.</param>
17+
/// <returns>Whether the match evaluation was successful.</returns>
18+
bool Match(T element);
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Runtime.CompilerServices;
6+
7+
namespace Microsoft.Toolkit.Uwp.UI.Extensions.Tree
8+
{
9+
/// <summary>
10+
/// An <see cref="IPredicate{T}"/> type matching all instances of a given type.
11+
/// </summary>
12+
/// <typeparam name="T">The type of items to match.</typeparam>
13+
internal readonly struct PredicateByAny<T> : IPredicate<T>
14+
{
15+
/// <inheritdoc/>
16+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
17+
public bool Match(T element)
18+
{
19+
return true;
20+
}
21+
}
22+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Runtime.CompilerServices;
7+
8+
namespace Microsoft.Toolkit.Uwp.UI.Extensions.Tree
9+
{
10+
/// <summary>
11+
/// An <see cref="IPredicate{T}"/> type matching items of a given type.
12+
/// </summary>
13+
/// <typeparam name="T">The type of items to match.</typeparam>
14+
/// <typeparam name="TState">The type of state to use when matching items.</typeparam>
15+
internal readonly struct PredicateByFunc<T, TState> : IPredicate<T>
16+
{
17+
/// <summary>
18+
/// The state to give as input to <see name="predicate"/>.
19+
/// </summary>
20+
private readonly TState state;
21+
22+
/// <summary>
23+
/// The predicatee to use to match items.
24+
/// </summary>
25+
private readonly Func<T, TState, bool> predicate;
26+
27+
/// <summary>
28+
/// Initializes a new instance of the <see cref="PredicateByFunc{T, TState}"/> struct.
29+
/// </summary>
30+
/// <param name="state">The state to give as input to <paramref name="predicate"/>.</param>
31+
/// <param name="predicate">The predicatee to use to match items.</param>
32+
public PredicateByFunc(TState state, Func<T, TState, bool> predicate)
33+
{
34+
this.state = state;
35+
this.predicate = predicate;
36+
}
37+
38+
/// <inheritdoc/>
39+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
40+
public bool Match(T element)
41+
{
42+
return this.predicate(element, state);
43+
}
44+
}
45+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Runtime.CompilerServices;
7+
8+
namespace Microsoft.Toolkit.Uwp.UI.Extensions.Tree
9+
{
10+
/// <summary>
11+
/// An <see cref="IPredicate{T}"/> type matching items of a given type.
12+
/// </summary>
13+
/// <typeparam name="T">The type of items to match.</typeparam>
14+
internal readonly struct PredicateByFunc<T> : IPredicate<T>
15+
{
16+
/// <summary>
17+
/// The predicatee to use to match items.
18+
/// </summary>
19+
private readonly Func<T, bool> predicate;
20+
21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="PredicateByFunc{T}"/> struct.
23+
/// </summary>
24+
/// <param name="predicate">The predicatee to use to match items.</param>
25+
public PredicateByFunc(Func<T, bool> predicate)
26+
{
27+
this.predicate = predicate;
28+
}
29+
30+
/// <inheritdoc/>
31+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
32+
public bool Match(T element)
33+
{
34+
return this.predicate(element);
35+
}
36+
}
37+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Runtime.CompilerServices;
7+
using Windows.UI.Xaml;
8+
9+
namespace Microsoft.Toolkit.Uwp.UI.Extensions.Tree
10+
{
11+
/// <summary>
12+
/// An <see cref="IPredicate{T}"/> type matching <see cref="FrameworkElement"/> instances by name.
13+
/// </summary>
14+
internal readonly struct PredicateByName : IPredicate<FrameworkElement>
15+
{
16+
/// <summary>
17+
/// The name of the element to look for.
18+
/// </summary>
19+
private readonly string name;
20+
21+
/// <summary>
22+
/// The comparison type to use to match <see name="name"/>.
23+
/// </summary>
24+
private readonly StringComparison comparisonType;
25+
26+
/// <summary>
27+
/// Initializes a new instance of the <see cref="PredicateByName"/> struct.
28+
/// </summary>
29+
/// <param name="name">The name of the element to look for.</param>
30+
/// <param name="comparisonType">The comparison type to use to match <paramref name="name"/>.</param>
31+
public PredicateByName(string name, StringComparison comparisonType)
32+
{
33+
this.name = name;
34+
this.comparisonType = comparisonType;
35+
}
36+
37+
/// <inheritdoc/>
38+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
39+
public bool Match(FrameworkElement element)
40+
{
41+
return element.Name.Equals(this.name, this.comparisonType);
42+
}
43+
}
44+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Runtime.CompilerServices;
7+
8+
namespace Microsoft.Toolkit.Uwp.UI.Extensions.Tree
9+
{
10+
/// <summary>
11+
/// An <see cref="IPredicate{T}"/> type matching items of a given type.
12+
/// </summary>
13+
/// <typeparam name="T">The type of items to match.</typeparam>
14+
internal readonly struct PredicateByType<T> : IPredicate<T>
15+
{
16+
/// <summary>
17+
/// The type of element to match.
18+
/// </summary>
19+
private readonly Type type;
20+
21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="PredicateByType{T}"/> struct.
23+
/// </summary>
24+
/// <param name="type">The type of element to match.</param>
25+
public PredicateByType(Type type)
26+
{
27+
this.type = type;
28+
}
29+
30+
/// <inheritdoc/>
31+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
32+
public bool Match(T element)
33+
{
34+
return element.GetType() == this.type;
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)