Skip to content

explore Publisher / Subscriber / Subscription characteristics as a replacement for instance check #3310

@OlegDokuka

Description

@OlegDokuka

Motivation

see #3309

Desired solution

We can use constant int characteristics (see https://docs.oracle.com/javase/8/docs/api/java/util/Spliterator.html#characteristics--) as a more straightforward solution to do faster type check and avoid expensive type check

public interface CorePublisher<T> extends Publisher<T> {

	/**
	 * An internal {@link Publisher#subscribe(Subscriber)} that will bypass
	 * {@link Hooks#onLastOperator(Function)} pointcut.
	 * <p>
	 * In addition to behave as expected by {@link Publisher#subscribe(Subscriber)}
	 * in a controlled manner, it supports direct subscribe-time {@link Context} passing.
	 *
	 * @param subscriber the {@link Subscriber} interested into the published sequence
	 * @see Publisher#subscribe(Subscriber)
	 */
	void subscribe(CoreSubscriber<? super T> subscriber);

	/**
	 * Better instanceof
	 *
	 * @return all characteristic of the given source
	 */
	int characteristics();

	interface Characteristics {

		int FUSEABLE             = 0b0000_0000_0000_0000_0000_0000_0000_0001;
		int SIZED                = 0b0000_0000_0000_0000_0000_0000_0000_0010;
		int CALLABLE             = 0b0000_0000_0000_0000_0000_0000_0000_0100;
		int SCALAR               = 0b0000_0000_0000_0000_0000_0000_0000_1000;
		int OPTIMIZABLE_OPERATOR = 0b0000_0000_0000_0000_0000_0000_0001_0000;

		static boolean isCallable(int characteristics) {
			return (characteristics & CALLABLE) == CALLABLE;
		}

		static boolean isFuseable(int characteristics) {
			return (characteristics & FUSEABLE) == FUSEABLE;
		}

		static boolean isScalar(int characteristics) {
			return (characteristics & SCALAR) == SCALAR;
		}

		static boolean isOptimizableOperator(int characteristics) {
			return (characteristics & OPTIMIZABLE_OPERATOR) == OPTIMIZABLE_OPERATOR;
		}
	}
}

Considered alternatives

Additional context

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions