Apache Lucene open-source search software
LUCENE-10158: Add a new interface Unwrappable to the utils package to ease migration to new MMAPDirectory and its testing (#369)
Uwe Schindlerc94aca7e5dd6
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 1ab534e75fb..6ade60bc035 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -157,6 +157,10 @@ API Changes* LUCENE-9325: Sort is now final, and the `setSort()` method has been removed (Alan Woodward)+* LUCENE-10158: Add a new interface Unwrappable to the utils package to allow code to+ unwrap wrappers/delegators that are added by Lucene's testing framework. This will allow+ testing new MMapDirectory implementation based on JDK Project Panama. (Uwe Schindler)+Improvements* LUCENE-10139: ExternalRefSorter returns a covariant with a subtype of BytesRefIteratordiff --git a/lucene/core/src/java/org/apache/lucene/util/Unwrappable.java b/lucene/core/src/java/org/apache/lucene/util/Unwrappable.javanew file mode 100644index 00000000000..4c91e38438b--- /dev/null+++ b/lucene/core/src/java/org/apache/lucene/util/Unwrappable.java@@ -0,0 +1,38 @@+/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package org.apache.lucene.util;++/**+ * An object with this interface is a wrapper around another object (e.g., a filter with a+ * delegate). The method {@link #unwrap()} can be called to get the wrapped object+ *+ * @lucene.internal+ */+public interface Unwrappable<T> {++ /** Unwraps this instance */+ T unwrap();++ /** Unwraps all {@code Unwrappable}s around the given object. */+ @SuppressWarnings("unchecked")+ public static <T> T unwrapAll(T o) {+ while (o instanceof Unwrappable) {+ o = ((Unwrappable<T>) o).unwrap();+ }+ return o;+ }+}diff --git a/lucene/test-framework/src/java/org/apache/lucene/mockfile/FilterPath.java b/lucene/test-framework/src/java/org/apache/lucene/mockfile/FilterPath.javaindex 9323536cf62..73f496fcb91 100644--- a/lucene/test-framework/src/java/org/apache/lucene/mockfile/FilterPath.java+++ b/lucene/test-framework/src/java/org/apache/lucene/mockfile/FilterPath.java@@ -29,12 +29,13 @@ import java.nio.file.WatchKey;import java.nio.file.WatchService;import java.util.Iterator;import org.apache.lucene.util.SuppressForbidden;+import org.apache.lucene.util.Unwrappable;/*** A {@code FilterPath} contains another {@code Path}, which it uses as its basic source of data,* possibly transforming the data along the way or providing additional functionality.*/-public class FilterPath implements Path {+public class FilterPath implements Path, Unwrappable<Path> {/** The underlying {@code Path} instance. */protected final Path delegate;@@ -63,6 +64,11 @@ public class FilterPath implements Path {return delegate;}+ @Override+ public Path unwrap() {+ return delegate;+ }+@Overridepublic FileSystem getFileSystem() {return fileSystem;@@ -261,10 +267,7 @@ public class FilterPath implements Path {* @return innermost Path instance*/public static Path unwrap(Path path) {- while (path instanceof FilterPath) {- path = ((FilterPath) path).delegate;- }- return path;+ return Unwrappable.unwrapAll(path);}/** Override this to customize the return wrapped path from various operations */