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

com.github.nill14.utils.init.binding.impl.BindingBuilder Maven / Gradle / Ivy

The newest version!
package com.github.nill14.utils.init.binding.impl;

import java.lang.annotation.Annotation;

import javax.inject.Provider;

import com.github.nill14.utils.init.api.BindingKey;
import com.github.nill14.utils.init.binding.AnnotatedBindingBuilder;
import com.github.nill14.utils.init.binding.Binder;
import com.github.nill14.utils.init.binding.LinkedBindingBuilder;
import com.github.nill14.utils.init.binding.ScopedBindingBuilder;
import com.github.nill14.utils.init.binding.target.BeanInstanceBindingTarget;
import com.github.nill14.utils.init.binding.target.BeanTypeBindingTarget;
import com.github.nill14.utils.init.binding.target.ProviderInstanceBindingTarget;
import com.github.nill14.utils.init.binding.target.ProviderTypeBindingTarget;
import com.github.nill14.utils.init.meta.Annotations;
import com.github.nill14.utils.init.scope.IScopeStrategy;
import com.github.nill14.utils.init.scope.ScopeStrategies;
import com.github.nill14.utils.init.util.Element;
import com.google.common.reflect.TypeToken;

public final class BindingBuilder implements AnnotatedBindingBuilder {
	
	private final TypeToken keyToken;
	private Annotation qualifier;
	private BindingTarget target;
	private IScopeStrategy scope = ScopeStrategies.prototype();
	private final Object source;
	private final Element> element;
	private final Binder binder;

	
	public BindingBuilder(Binder binder, Element> element, Object source, TypeToken bindToken) {
		this.binder = binder;
		this.element = element;
		this.source = source;
		this.keyToken = bindToken;
		this.target = new BeanTypeBindingTarget<>(bindToken);
		
		//no qualifier at the moment of creation
		element.update(new Binding(BindingKey.of(bindToken), target, scope, source));
	}


	@Override
	public ScopedBindingBuilder to(Class implementation) {
		target = new BeanTypeBindingTarget<>(TypeToken.of(implementation));
		buildBinder();
		return this;
	}

	@Override
	public ScopedBindingBuilder to(TypeToken implementation) {
		target = new BeanTypeBindingTarget<>(implementation);
		buildBinder();
		return this;
	}

	@Override
	public void toInstance(T instance) {
		target = new BeanInstanceBindingTarget<>(instance);
		buildBinder();
	}

	@Override
	public ScopedBindingBuilder toProvider(Provider provider) {
		target = new ProviderInstanceBindingTarget<>(provider);
		buildBinder();
		return this;
	}

	@Override
	public ScopedBindingBuilder toProvider(Class> providerType) {
		target = new ProviderTypeBindingTarget<>(TypeToken.of(providerType));
		buildBinder();
		return this;
	}

	@Override
	public ScopedBindingBuilder toProvider(TypeToken> providerType) {
		target = new ProviderTypeBindingTarget<>(providerType);
		buildBinder();
		return this;
	}

	@Override
	public void in(Class scopeAnnotation) {
		scope = ScopeStrategies.of(scopeAnnotation);
		buildBinder();
	}

	@Override
	public LinkedBindingBuilder annotatedWith(Class annotationType) {
		this.qualifier = Annotations.annotation(annotationType);
		buildBinder();
		return this;
	}

	@Override
	public LinkedBindingBuilder annotatedWith(Annotation annotation) {
		this.qualifier = annotation;
		buildBinder();
		return this;
	}
	
	private void buildBinder() {
		BindingKey bindingKey = BindingKey.of(keyToken, qualifier); 
		
		element.update(new Binding(bindingKey, target, scope, source));
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy