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

org.nasdanika.html.model.app.util.LabelBuilder Maven / Gradle / Ivy

There is a newer version: 2024.10.0
Show newest version
package org.nasdanika.html.model.app.util;

import java.util.Collection;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import org.nasdanika.common.Composable;
import org.nasdanika.common.CompoundExecutionParticipant;
import org.nasdanika.common.ExecutionParticipant;
import org.nasdanika.common.FilterExecutionParticipant;
import org.nasdanika.common.ProgressMonitor;
import org.nasdanika.html.model.app.Label;
import org.nasdanika.html.model.app.util.LabelProvider.Resolver;

/**
 * Adapter interface for creating {@link Label}'s and Label subclasses.
 * @author Pavel
 *
 */
public interface LabelBuilder extends ExecutionParticipant, Composable {
		
	Label build(Label label, Consumer resolverCollector, ProgressMonitor progressMonitor);
		
	default Label splitAndBuild(Label label, Consumer resolverCollector, ProgressMonitor progressMonitor) {
		try (ProgressMonitor subMonitor = split(progressMonitor, "Building" + name())) {
			return build(label, resolverCollector, subMonitor);
		}
	}	

	@Override
	default LabelBuilder compose(LabelBuilder other) {
		if (other == null) {
			return this;
		}
		
		List labelBuilders = List.of(this, other);
		
		class CompoundLabelBuilder extends CompoundExecutionParticipant implements LabelBuilder {

			protected CompoundLabelBuilder(String name, boolean reverseCommitClose) {
				super(name, reverseCommitClose);
			}

			@Override
			public Label build(Label label, Consumer resolverCollector, ProgressMonitor progressMonitor) {
				for (LabelBuilder element: getElements()) {
					if (element != null) {
						label = element.splitAndBuild(label, resolverCollector, progressMonitor);
					}
				}
				return label;
			}

			@Override
			protected Collection getElements() {
				return labelBuilders;
			}
			
		}
		
		return new CompoundLabelBuilder("Composite label builder", false);
	}
	
	/**
	 * Wraps this builder into a provider
	 * @param identity Initial label, can be null
	 * @return {@link LabelProvider} delegating to this builder
	 */
	default LabelProvider asProvider(Label identity, Iterable registryKeys) {
		class DelegatingLabelProvider extends FilterExecutionParticipant implements LabelProvider {

			public DelegatingLabelProvider() {
				super(LabelBuilder.this);
			}

			@Override
			public Label create(BiConsumer registry, Consumer resolverCollector, ProgressMonitor progressMonitor) {
				Label label = target.build(identity, resolverCollector, progressMonitor);
				if (registry != null && label != null && registryKeys != null) {
					for (Object registryKey: registryKeys) {
						registry.accept(registryKey, label);
					}
				}
				return label;
			}
			
		}
		return new DelegatingLabelProvider();
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy