This is an automated email from the ASF dual-hosted git repository. desruisseaux pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit a8ca48dad6d0ca350c603091650d1600f9d1bc1d Author: Martin Desruisseaux <martin.desruisse...@geomatys.com> AuthorDate: Tue Sep 26 10:22:19 2023 +0200 Combine `DataStoreFilter` with user-supplied filter. --- .../org/apache/sis/storage/DataStoreRegistry.java | 6 ++++- .../apache/sis/storage/image/DataStoreFilter.java | 29 +++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/DataStoreRegistry.java b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/DataStoreRegistry.java index 91f0c46788..69ec2b1ee7 100644 --- a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/DataStoreRegistry.java +++ b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/DataStoreRegistry.java @@ -204,8 +204,12 @@ final class DataStoreRegistry extends LazySet<DataStoreProvider> { StorageConnector connector; // Will be reset to `null` if it shall not be closed. if (storage instanceof StorageConnector) { connector = (StorageConnector) storage; + final var p = connector.getOption(InternalOptionKey.PREFERRED_PROVIDERS); if (preferred == null) { - preferred = connector.getOption(InternalOptionKey.PREFERRED_PROVIDERS); + preferred = p; + } else { + preferred = preferred.and(p); + connector.setOption(InternalOptionKey.PREFERRED_PROVIDERS, preferred); } } else { connector = new StorageConnector(storage); diff --git a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/image/DataStoreFilter.java b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/image/DataStoreFilter.java index c2d9929e89..3f88702377 100644 --- a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/image/DataStoreFilter.java +++ b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/image/DataStoreFilter.java @@ -49,6 +49,13 @@ public final class DataStoreFilter implements Predicate<DataStoreProvider> { */ private final boolean writer; + /** + * If there is another condition to apply, the other condition. Otherwise {@code null}. + * This is used for allowing {@code (foo instanceof DataStoreFilter)} to continue to work + * on the result of an {@code and} operation. + */ + private final Predicate<? super DataStoreProvider> other; + /** * Creates a new filter for the given data store name. * @@ -57,7 +64,17 @@ public final class DataStoreFilter implements Predicate<DataStoreProvider> { */ public DataStoreFilter(final String preferred, final boolean writer) { this.preferred = preferred; - this.writer = writer; + this.writer = writer; + this.other = null; + } + + /** + * Creates a new filter which is the result of a {@code AND} operation between the specified filters. + */ + private DataStoreFilter(final DataStoreFilter first, final Predicate<? super DataStoreProvider> other) { + this.preferred = first.preferred; + this.writer = first.writer; + this.other = other; } /** @@ -80,4 +97,14 @@ public final class DataStoreFilter implements Predicate<DataStoreProvider> { } return false; } + + /** + * Returns a filter which is the result of a AND operation between this filter and the other filter. + * The combined filter is still an instance of {@code DataStoreFilter}, so {@code instanceof} checks + * are still possible. + */ + @Override + public Predicate<DataStoreProvider> and(Predicate<? super DataStoreProvider> other) { + return new DataStoreFilter(this, other); + } }