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

com.g2forge.gearbox.browser.StatefulBrowser Maven / Gradle / Ivy

package com.g2forge.gearbox.browser;

import com.g2forge.alexandria.java.function.IConsumer1;
import com.g2forge.alexandria.java.function.IFunction1;

import lombok.AccessLevel;
import lombok.Getter;

@Getter(AccessLevel.PROTECTED)
public class StatefulBrowser {
	protected volatile transient boolean open = true;

	protected final IBrowser browser;

	public StatefulBrowser(IBrowser browser) {
		this.browser = browser;
	}

	public  T back(IConsumer1 consumer, T value, IFunction1 accessor) {
		if (!isOpen()) throw new IllegalStateException();
		final IBrowser browser = getBrowser();
		consumer.accept(browser);

		this.open = false;
		accessor.apply(value).open = true;
		return value;
	}

	public  T change(IConsumer1 consumer, IFunction1 constructor) {
		if (!isOpen()) throw new IllegalStateException();
		final IBrowser browser = getBrowser();
		consumer.accept(browser);

		this.open = false;
		final StatefulBrowser newState = new StatefulBrowser(browser);
		return constructor.apply(newState);
	}

	public  T get(IFunction1 function) {
		if (!isOpen()) throw new IllegalStateException();
		return function.apply(getBrowser());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy