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

org.hibernate.event.service.spi.EventListenerGroup Maven / Gradle / Ivy

/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or .
 */
package org.hibernate.event.service.spi;

import java.io.Serializable;
import java.util.Map;
import java.util.concurrent.CompletionStage;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;

import org.hibernate.Incubating;
import org.hibernate.event.spi.EventType;

/**
 * Contract for a groups of events listeners for a particular event type.
 *
 * @author Steve Ebersole
 */
public interface EventListenerGroup extends Serializable {

	/**
	 * Retrieve the event type associated with this groups of listeners.
	 *
	 * @return The event type.
	 */
	public EventType getEventType();

	/**
	 * Are there no listeners registered?
	 *
	 * @return {@literal true} if no listeners are registered; {@literal false} otherwise.
	 */
	public boolean isEmpty();

	public int count();

	/**
	 * @deprecated this is not the most efficient way for iterating the event listeners.
	 * See {@link #fireEventOnEachListener(Object, BiConsumer)} and its overloaded variants for better alternatives.
	 * @return The Iterable.
	 */
	@Deprecated
	public Iterable listeners();

	/**
	 * Mechanism to more finely control the notion of duplicates.
	 * 

* For example, say you are registering listeners for an extension library. This extension library * could define a "marker interface" which indicates listeners related to it and register a strategy * that checks against that marker interface. * * @param strategy The duplication strategy */ public void addDuplicationStrategy(DuplicationStrategy strategy); public void appendListener(T listener); public void appendListeners(T... listeners); public void prependListener(T listener); public void prependListeners(T... listeners); /** * Clears both the list of event listeners and all DuplicationStrategy, * including the default duplication strategy. * @deprecated likely want to use {@link #clearListeners()} instead, which doesn't * also reset the registered DuplicationStrategy(ies). */ @Deprecated public void clear(); /** * Removes all registered listeners */ public void clearListeners(); /** * Fires an event on each registered event listener of this group. * * Implementation note (performance): * the first argument is a supplier so that events can avoid being created when no listener is registered. * the second argument is specifically designed to avoid needing a capturing lambda. * * @param eventSupplier * @param actionOnEvent * @param the kind of event */ @Incubating void fireLazyEventOnEachListener(final Supplier eventSupplier, final BiConsumer actionOnEvent); /** * Similar as {@link #fireLazyEventOnEachListener(Supplier, BiConsumer)} except it doesn't use a {{@link Supplier}}: * useful when there is no need to lazily initialize the event. * @param event * @param actionOnEvent * @param the kind of event */ @Incubating void fireEventOnEachListener(final U event, final BiConsumer actionOnEvent); /** * Similar to {@link #fireEventOnEachListener(Object, BiConsumer)}, but allows passing a third parameter * to the consumer; our code based occasionally needs a third parameter: having this additional variant * allows using the optimal iteration more extensively and reduce allocations. * @param event * @param param * @param actionOnEvent * @param * @param */ @Incubating void fireEventOnEachListener(final U event, X param, final EventActionWithParameter actionOnEvent); /** * Similar to {@link #fireEventOnEachListener(Object, Function)}, but Reactive friendly: it chains * processing of the same event on each Reactive Listener, and returns a {@link CompletionStage} of type R. * The various generic types allow using this for each concrete event type and flexible return types. *

Used by Hibernate Reactive

* @param event The event being fired * @param fun The function combining each event listener with the event * @param the return type of the returned CompletionStage * @param the type of the event being fired on each listener * @param the type of ReactiveListener: each listener of type T will be casted to it. * @return the composite completion stage of invoking fun(event) on each listener. */ @Incubating CompletionStage fireEventOnEachListener(final U event, final Function>> fun); /** * Similar to {@link #fireEventOnEachListener(Object, Object, Function)}, but Reactive friendly: it chains * processing of the same event on each Reactive Listener, and returns a {@link CompletionStage} of type R. * The various generic types allow using this for each concrete event type and flexible return types. *

Used by Hibernate Reactive

* @param event The event being fired * @param fun The function combining each event listener with the event * @param the return type of the returned CompletionStage * @param the type of the event being fired on each listener * @param the type of ReactiveListener: each listener of type T will be casted to it. * @param an additional parameter to be passed to the function fun * @return the composite completion stage of invoking fun(event) on each listener. */ @Incubating public CompletionStage fireEventOnEachListener(U event, X param, Function>> fun); /** * Similar to {@link #fireLazyEventOnEachListener(Supplier, BiConsumer)}, but Reactive friendly: it chains * processing of the same event on each Reactive Listener, and returns a {@link CompletionStage} of type R. * The various generic types allow using this for each concrete event type and flexible return types. *

This variant expects a Supplier of the event, rather than the event directly; this is useful for the * event types which are commonly configured with no listeners at all, so to allow skipping creating the * event; use only for event types which are known to be expensive while the listeners are commonly empty.

*

Used by Hibernate Reactive

* @param eventSupplier A supplier able to produce the actual event * @param fun The function combining each event listener with the event * @param the return type of the returned CompletionStage * @param the type of the event being fired on each listener * @param the type of ReactiveListener: each listener of type T will be casted to it. * @return the composite completion stage of invoking fun(event) on each listener. */ @Incubating CompletionStage fireLazyEventOnEachListener(final Supplier eventSupplier, final Function>> fun); }