Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HSEARCH-2415 Unify the way we take adaptors into account when introsp…
…ecting bridges
- Loading branch information
Showing
10 changed files
with
154 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
engine/src/main/java/org/hibernate/search/bridge/util/impl/BridgeAdaptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Hibernate Search, full-text search for your domain model | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.search.bridge.util.impl; | ||
|
||
import org.hibernate.search.bridge.TwoWayStringBridge; | ||
import org.hibernate.search.bridge.spi.IgnoreAnalyzerBridge; | ||
|
||
/** | ||
* An interface for bridge adaptors, i.e. types exposing one bridge interface and using | ||
* another bridge instance under the hood. | ||
* | ||
* <p>Implementing this interface allows {@link BridgeAdaptorUtils} to detect | ||
* specific bridge interfaces ({@link TwoWayStringBridge}, {@link IgnoreAnalyzerBridge}, | ||
* ...) even when the adaptor does no re-implement the interface. | ||
* | ||
* @author Yoann Rodiere | ||
*/ | ||
public interface BridgeAdaptor { | ||
|
||
/** | ||
* @return The bridge that is directly wrapped by this adaptor. | ||
*/ | ||
Object unwrap(); | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
engine/src/main/java/org/hibernate/search/bridge/util/impl/BridgeAdaptorUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Hibernate Search, full-text search for your domain model | ||
* | ||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later | ||
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. | ||
*/ | ||
package org.hibernate.search.bridge.util.impl; | ||
|
||
import org.hibernate.search.bridge.ContainerBridge; | ||
import org.hibernate.search.bridge.TwoWayStringBridge; | ||
import org.hibernate.search.bridge.spi.IgnoreAnalyzerBridge; | ||
|
||
/** | ||
* Utilities allowing to take into account bridge adaptors when detecting implemented tagging | ||
* interface (e.g. {@link IgnoreAnalyzerBridge}) or when trying to make use of specific field | ||
* interfaces (e.g. {@link TwoWayStringBridge}). | ||
* | ||
* @author Yoann Rodiere | ||
*/ | ||
public final class BridgeAdaptorUtils { | ||
|
||
private BridgeAdaptorUtils() { | ||
// private constructor | ||
} | ||
|
||
/** | ||
* Return a bridge of the specified type extracted from the given bridge, | ||
* going through {@link BridgeAdaptor adaptors} and {@link ContainerBridge container bridges} | ||
* as necessary. | ||
* | ||
* <p>The returned object may be the adaptor itself, or its delegate, or a delegate of | ||
* its delegate, and so on. | ||
* <p>If neither the adaptor or one of its delegates is an instance of the specified type, | ||
* {@code null} is returned. | ||
* | ||
* @param bridge The bridge to use as a starting point. | ||
* @param clazz The expected bridge type. | ||
* @return A unwrapped bridge implementing the expected bridge type, or {@code null} | ||
* if none could be found. | ||
*/ | ||
public static <T> T unwrapAdaptorAndContainer(Object bridge, Class<T> clazz) { | ||
if ( clazz.isInstance( bridge ) ) { | ||
return clazz.cast( bridge ); | ||
} | ||
else if ( bridge instanceof BridgeAdaptor ) { | ||
Object delegate = ( (BridgeAdaptor) bridge ).unwrap(); | ||
return unwrapAdaptorAndContainer( delegate, clazz ); | ||
} | ||
else if ( bridge instanceof ContainerBridge ) { | ||
Object delegate = ( (ContainerBridge) bridge ).getElementBridge(); | ||
return unwrapAdaptorAndContainer( delegate, clazz ); | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Return a bridge of the specified type extracted from the given bridge, | ||
* going through {@link BridgeAdaptor adaptors} as necessary, never going | ||
* through {@link ContainerBridge container bridges}. | ||
* | ||
* <p>The returned object may be the adaptor itself, or its delegate, or a delegate of | ||
* its delegate, and so on. | ||
* <p>If neither the adaptor or one of its delegates is an instance of the specified type, | ||
* {@code null} is returned. | ||
* | ||
* @param bridge The bridge to use as a starting point. | ||
* @param clazz The expected bridge type. | ||
* @return A unwrapped bridge implementing the expected bridge type, or {@code null} | ||
* if none could be found. | ||
*/ | ||
public static <T> T unwrapAdaptorOnly(Object bridge, Class<T> clazz) { | ||
if ( clazz.isInstance( bridge ) ) { | ||
return clazz.cast( bridge ); | ||
} | ||
else if ( bridge instanceof BridgeAdaptor ) { | ||
Object delegate = ( (BridgeAdaptor) bridge ).unwrap(); | ||
return unwrapAdaptorOnly( delegate, clazz ); | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters