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

com.mantledillusion.vaadin.cotton.component.builders.AbstractEntityBuilder Maven / Gradle / Ivy

Go to download

Cotton is a Vaadin extension destined for the ultimate of developer convenience.

There is a newer version: 2.3.4
Show newest version
package com.mantledillusion.vaadin.cotton.component.builders;

import com.mantledillusion.vaadin.cotton.component.ComponentBuilder;
import com.mantledillusion.vaadin.cotton.component.Configurer;
import com.mantledillusion.vaadin.cotton.component.EntityBuilder;

import java.util.*;

/**
 * Base implementation of {@link ComponentBuilder} that provides all of the base functionality.;
 *
 * @param 
 *            The entity type this builder builds.
 * @param 
 *            The final implementation type of this {@link AbstractEntityBuilder}. Necessary to allow builder methods of
 *            non-final implementations to return the builder instance in the correct type.
 */
abstract class AbstractEntityBuilder> implements EntityBuilder {

	private final AbstractEntityBuilder parent;
	private final Map, Object> context = new HashMap<>();
	private final List> configurators = new ArrayList<>();

	AbstractEntityBuilder() {
		this.parent = null;
	}

	AbstractEntityBuilder(AbstractEntityBuilder parent) {
		this.parent = parent;
	}

	@Override
	public  boolean contains(Class valueType) {
		return (this.parent != null && this.parent.contains(valueType)) || this.context.containsKey(valueType);
	}

	@Override
	public  V get(Class valueType) {
		return this.parent != null && this.parent.contains(valueType) ? this.parent.get (valueType) :
				(V) this.context.get(valueType);
	}

	@Override
	public  void set(Class valueType, V2 value) {
		if (value == null) {
			return;
		}
		if (this.context.containsKey(valueType)) {
			throw new IllegalStateException("Configuration tried to configure a second instance of the type " +
					value.getClass().getSimpleName() + " for the type " + valueType.getSimpleName() +
					", which would cause the configuration context to become inconsistent.");
		}
		this.context.put(valueType, value);
	}

	@Override
	public B configure(Configurer configurer, boolean prepend) {
		this.configurators.add(prepend ? 0 : this.configurators.size(), configurer);
		return getThis();
	}
	
	@Override
	public List> getConfigurers() {
		return Collections.unmodifiableList(this.configurators);
	}

	@Override
	@SuppressWarnings("unchecked")
	public B getThis() {
		return (B) this;
	}

	protected void apply(C entity) {
		this.context.clear();
		this.context.put(entity.getClass(), entity);
		this.configurators.forEach(configuration -> configuration.configure(entity));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy