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

com.g2forge.alexandria.java.function.IConsumer1 Maven / Gradle / Ivy

There is a newer version: 0.0.18
Show newest version
package com.g2forge.alexandria.java.function;

import java.util.function.Consumer;

@FunctionalInterface
public interface IConsumer1 extends Consumer, IConsumer {
	public static  IConsumer1 create(IConsumer1 consumer) {
		return consumer;
	}

	@SafeVarargs
	public static  IConsumer1 fanOut(IConsumer1... consumers) {
		return input -> {
			for (IConsumer1 consumer : consumers) {
				consumer.accept(input);
			}
		};
	}

	public static  IConsumer1 once(IConsumer1 consumer) {
		return new IConsumer1() {
			protected boolean called = false;

			@Override
			public void accept(I input) {
				if (called) throw new IllegalStateException("Can only be called once!");
				try {
					consumer.accept(input);
				} finally {
					called = true;
				}
			}
		};
	}

	public default Runnable curry(I input) {
		return () -> this.accept(input);
	}
}