From c8bba37510f01f116fea99204e934d35863a9d2b Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Sun, 26 Jun 2022 19:34:14 +0700 Subject: Add store/selector API --- crypto/src/util/collections/CollectionUtilities.cs | 38 +++++++++++++++++++--- crypto/src/util/collections/ISelector.cs | 16 +++++++++ crypto/src/util/collections/IStore.cs | 15 +++++++++ crypto/src/util/collections/StoreImpl.cs | 25 ++++++++++++++ 4 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 crypto/src/util/collections/ISelector.cs create mode 100644 crypto/src/util/collections/IStore.cs create mode 100644 crypto/src/util/collections/StoreImpl.cs (limited to 'crypto/src') diff --git a/crypto/src/util/collections/CollectionUtilities.cs b/crypto/src/util/collections/CollectionUtilities.cs index 37551e5b3..426700903 100644 --- a/crypto/src/util/collections/CollectionUtilities.cs +++ b/crypto/src/util/collections/CollectionUtilities.cs @@ -15,6 +15,36 @@ namespace Org.BouncyCastle.Utilities.Collections } } + public static void CollectMatches(ICollection matches, ISelector selector, params IStore[] stores) + { + CollectMatches(matches, selector, stores); + } + + public static void CollectMatches(ICollection matches, ISelector selector, + IEnumerable> stores) + { + if (matches == null) + throw new ArgumentNullException(nameof(matches)); + if (stores == null) + return; + + foreach (var store in stores) + { + if (store == null) + continue; + + foreach (T match in store.EnumerateMatches(selector)) + { + matches.Add(match); + } + } + } + + public static IStore CreateStore(IEnumerable contents) + { + return new StoreImpl(contents); + } + public static IEnumerable Proxy(IEnumerable e) { return new EnumerableProxy(e); @@ -48,18 +78,18 @@ namespace Org.BouncyCastle.Utilities.Collections return e.Current; } - public static string ToString(IEnumerable c) + public static string ToString(IEnumerable c) { - IEnumerator e = c.GetEnumerator(); + IEnumerator e = c.GetEnumerator(); if (!e.MoveNext()) return "[]"; StringBuilder sb = new StringBuilder("["); - sb.Append(e.Current.ToString()); + sb.Append(e.Current); while (e.MoveNext()) { sb.Append(", "); - sb.Append(e.Current.ToString()); + sb.Append(e.Current); } sb.Append(']'); return sb.ToString(); diff --git a/crypto/src/util/collections/ISelector.cs b/crypto/src/util/collections/ISelector.cs new file mode 100644 index 000000000..186b922d3 --- /dev/null +++ b/crypto/src/util/collections/ISelector.cs @@ -0,0 +1,16 @@ +using System; + +namespace Org.BouncyCastle.Utilities.Collections +{ + /// Interface for matching objects in an . + /// The contravariant type of selectable objects. + public interface ISelector + : ICloneable + { + /// Match the passed in object, returning true if it would be selected by this selector, false + /// otherwise. + /// The object to be matched. + /// true if the objects is matched by this selector, false otherwise. + bool Match(T candidate); + } +} diff --git a/crypto/src/util/collections/IStore.cs b/crypto/src/util/collections/IStore.cs new file mode 100644 index 000000000..12c19aaf4 --- /dev/null +++ b/crypto/src/util/collections/IStore.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; + +namespace Org.BouncyCastle.Utilities.Collections +{ + /// A generic interface describing a simple store of objects. + /// The covariant type of stored objects. + public interface IStore + { + /// Enumerate the (possibly empty) collection of objects matched by the given selector. + /// The used to select matching objects. + /// An of the matching objects. + IEnumerable EnumerateMatches(ISelector selector); + } +} diff --git a/crypto/src/util/collections/StoreImpl.cs b/crypto/src/util/collections/StoreImpl.cs new file mode 100644 index 000000000..3a7135007 --- /dev/null +++ b/crypto/src/util/collections/StoreImpl.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; + +namespace Org.BouncyCastle.Utilities.Collections +{ + internal sealed class StoreImpl + : IStore + { + private readonly List m_contents; + + internal StoreImpl(IEnumerable e) + { + m_contents = new List(e); + } + + IEnumerable IStore.EnumerateMatches(ISelector selector) + { + foreach (T candidate in m_contents) + { + if (selector == null || selector.Match(candidate)) + yield return candidate; + } + } + } +} -- cgit 1.4.1