mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Add unused FilterGroup for later
This commit is contained in:
41
SabreTools.Core/Filter/Enums.cs
Normal file
41
SabreTools.Core/Filter/Enums.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
namespace SabreTools.Core.Filter
|
||||
{
|
||||
/// <summary>
|
||||
/// Determines how a filter group should be applied
|
||||
/// </summary>
|
||||
public enum GroupType
|
||||
{
|
||||
/// <summary>
|
||||
/// Default value, does nothing
|
||||
/// </summary>
|
||||
NONE,
|
||||
|
||||
/// <summary>
|
||||
/// All must pass for the group to pass
|
||||
/// </summary>
|
||||
AND,
|
||||
|
||||
/// <summary>
|
||||
/// Any must pass for the group to pass
|
||||
/// </summary>
|
||||
OR,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines what operation is being done
|
||||
/// </summary>
|
||||
public enum Operation
|
||||
{
|
||||
/// <summary>
|
||||
/// Default value, does nothing
|
||||
/// </summary>
|
||||
NONE,
|
||||
|
||||
Equals,
|
||||
NotEquals,
|
||||
GreaterThan,
|
||||
GreaterThanOrEqual,
|
||||
LessThan,
|
||||
LessThanOrEqual,
|
||||
}
|
||||
}
|
||||
136
SabreTools.Core/Filter/FilterGroup.cs
Normal file
136
SabreTools.Core/Filter/FilterGroup.cs
Normal file
@@ -0,0 +1,136 @@
|
||||
using System.Collections.Generic;
|
||||
using SabreTools.Models.Metadata;
|
||||
|
||||
namespace SabreTools.Core.Filter
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a set of filters and groups
|
||||
/// </summary>
|
||||
public class FilterGroup
|
||||
{
|
||||
/// <summary>
|
||||
/// All standalone filters in the group
|
||||
/// </summary>
|
||||
public readonly List<FilterObject> Subfilters = [];
|
||||
|
||||
/// <summary>
|
||||
/// All filter groups contained in the group
|
||||
/// </summary>
|
||||
public readonly List<FilterGroup> Subgroups = [];
|
||||
|
||||
/// <summary>
|
||||
/// How to apply the group filters
|
||||
/// </summary>
|
||||
public readonly GroupType GroupType;
|
||||
|
||||
public FilterGroup(GroupType groupType)
|
||||
{
|
||||
GroupType = groupType;
|
||||
}
|
||||
|
||||
public FilterGroup(FilterObject[] filters, GroupType groupType)
|
||||
{
|
||||
Subfilters.AddRange(filters);
|
||||
GroupType = groupType;
|
||||
}
|
||||
|
||||
public FilterGroup(FilterGroup[] groups, GroupType groupType)
|
||||
{
|
||||
Subgroups.AddRange(groups);
|
||||
GroupType = groupType;
|
||||
}
|
||||
|
||||
public FilterGroup(FilterObject[] filters, FilterGroup[] groups, GroupType groupType)
|
||||
{
|
||||
Subfilters.AddRange(filters);
|
||||
Subgroups.AddRange(groups);
|
||||
GroupType = groupType;
|
||||
}
|
||||
|
||||
#region Matching
|
||||
|
||||
/// <summary>
|
||||
/// Determine if a DictionaryBase object matches the group
|
||||
/// </summary>
|
||||
public bool Matches(DictionaryBase dictionaryBase)
|
||||
{
|
||||
return GroupType switch
|
||||
{
|
||||
GroupType.AND => MatchesAnd(dictionaryBase),
|
||||
GroupType.OR => MatchesOr(dictionaryBase),
|
||||
_ => false,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if a value matches all filters
|
||||
/// </summary>
|
||||
private bool MatchesAnd(DictionaryBase dictionaryBase)
|
||||
{
|
||||
// Run standalone filters
|
||||
foreach (var filter in Subfilters)
|
||||
{
|
||||
// One failed match fails the group
|
||||
if (!filter.Matches(dictionaryBase))
|
||||
return false;
|
||||
}
|
||||
|
||||
// Run filter subgroups
|
||||
foreach (var group in Subgroups)
|
||||
{
|
||||
// One failed match fails the group
|
||||
if (!group.Matches(dictionaryBase))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines if a value matches any filters
|
||||
/// </summary>
|
||||
private bool MatchesOr(DictionaryBase dictionaryBase)
|
||||
{
|
||||
// Run standalone filters
|
||||
foreach (var filter in Subfilters)
|
||||
{
|
||||
// One successful match passes the group
|
||||
if (filter.Matches(dictionaryBase))
|
||||
return true;
|
||||
}
|
||||
|
||||
// Run filter subgroups
|
||||
foreach (var group in Subgroups)
|
||||
{
|
||||
// One successful match passes the group
|
||||
if (group.Matches(dictionaryBase))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Helpers
|
||||
|
||||
/// <summary>
|
||||
/// Derive a group type from the input string, if possible
|
||||
/// </summary>
|
||||
private static GroupType GetGroupType(string? groupType)
|
||||
{
|
||||
return groupType?.ToLowerInvariant() switch
|
||||
{
|
||||
"&" => GroupType.AND,
|
||||
"&&" => GroupType.AND,
|
||||
|
||||
"|" => GroupType.OR,
|
||||
"||" => GroupType.OR,
|
||||
|
||||
_ => GroupType.NONE,
|
||||
};
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -210,6 +210,5 @@ namespace SabreTools.Core.Filter
|
||||
string? constantMatch = Array.Find(constants, c => string.Equals(c, localFieldName, StringComparison.OrdinalIgnoreCase));
|
||||
return constantMatch;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
namespace SabreTools.Core.Filter
|
||||
{
|
||||
/// <summary>
|
||||
/// Determines what operation is being done
|
||||
/// </summary>
|
||||
public enum Operation
|
||||
{
|
||||
/// <summary>
|
||||
/// Default value, does nothing
|
||||
/// </summary>
|
||||
NONE,
|
||||
|
||||
Equals,
|
||||
NotEquals,
|
||||
GreaterThan,
|
||||
GreaterThanOrEqual,
|
||||
LessThan,
|
||||
LessThanOrEqual,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user