All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.search.engine.environment.bean.BeanReference Maven / Gradle / Ivy

There is a newer version: 8.0.0.Alpha1
Show newest version
/*
 * SPDX-License-Identifier: Apache-2.0
 * Copyright Red Hat Inc. and Hibernate Authors
 */
package org.hibernate.search.engine.environment.bean;

import org.hibernate.search.util.common.impl.StringHelper;

/**
 * A reference to a bean, allowing the retrieval of that bean
 * when {@link #resolve(BeanResolver) passed} a {@link BeanResolver}.
 *
 * @param  The type of the referenced bean.
 */
public interface BeanReference {

	/**
	 * Resolve this reference into a bean using the given resolver.
	 *
	 * @param beanResolver A resolver to resolve this reference with.
	 * @return The bean instance.
	 */
	BeanHolder resolve(BeanResolver beanResolver);

	/**
	 * Cast this reference into a reference whose {@link #resolve(BeanResolver)} method is is guaranteed to
	 * either fail or return an instance of the given type.
	 *
	 * @param expectedType The expected bean type.
	 * @param  The expected bean type.
	 * @return A bean reference.
	 * @throws ClassCastException If this reference is certain to never return an instance of the given type.
	 */
	default  BeanReference asSubTypeOf(Class expectedType) {
		return new CastingBeanReference<>( this, expectedType );
	}

	/**
	 * Create a {@link BeanReference} referencing a bean by its type only.
	 *
	 * @param type The bean type. Must not be null.
	 * @param  The bean type.
	 * @return The corresponding {@link BeanReference}.
	 */
	static  BeanReference of(Class type) {
		return of( type, BeanRetrieval.ANY );
	}

	/**
	 * Create a {@link BeanReference} referencing a bean by its type only.
	 *
	 * @param type The bean type. Must not be null.
	 * @param retrieval How to retrieve the bean. See {@link BeanRetrieval}.
	 * @param  The bean type.
	 * @return The corresponding {@link BeanReference}.
	 */
	static  BeanReference of(Class type, BeanRetrieval retrieval) {
		return new TypeBeanReference<>( type, retrieval );
	}

	/**
	 * Create a {@link BeanReference} referencing a bean by type and name.
	 *
	 * @param type The bean type. Must not be null.
	 * @param name The bean name. May be null or empty.
	 * @param  The bean type.
	 * @return The corresponding {@link BeanReference}.
	 */
	static  BeanReference of(Class type, String name) {
		return of( type, name, BeanRetrieval.ANY );
	}

	/**
	 * Create a {@link BeanReference} referencing a bean by type and name.
	 *
	 * @param type The bean type. Must not be null.
	 * @param name The bean name. May be null or empty.
	 * @param retrieval How to retrieve the bean. See {@link BeanRetrieval}.
	 * @param  The bean type.
	 * @return The corresponding {@link BeanReference}.
	 */
	static  BeanReference of(Class type, String name, BeanRetrieval retrieval) {
		if ( StringHelper.isNotEmpty( name ) ) {
			return new TypeAndNameBeanReference<>( type, name, retrieval );
		}
		else {
			return new TypeBeanReference<>( type, retrieval );
		}
	}

	/**
	 * Create a {@link BeanReference} referencing a bean instance directly.
	 *
	 * @param instance The bean instance. Must not be null.
	 * @param  The bean type.
	 * @return The corresponding {@link BeanReference}.
	 */
	static  BeanReference ofInstance(T instance) {
		return new InstanceBeanReference<>( instance );
	}

	// This method conforms to the MicroProfile Config specification. Do not change its signature.
	static BeanReference parse(String value) {
		return BeanReference.parse( Object.class, value );
	}

	static  BeanReference parse(Class expectedType, String value) {
		return BeanReferences.parse( expectedType, value );
	}

}