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

org.javabits.yar.guice.RegistryAnnotatedBindingBuilderImpl Maven / Gradle / Ivy

There is a newer version: 3.0.1.RELEASE
Show newest version
package org.javabits.yar.guice;

import com.google.inject.*;
import com.google.inject.Provider;
import com.google.inject.Scope;
import com.google.inject.binder.AnnotatedBindingBuilder;
import com.google.inject.binder.LinkedBindingBuilder;
import com.google.inject.binder.ScopedBindingBuilder;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;

import static java.util.Objects.requireNonNull;

/**
 * TODO comment
 * Date: 2/8/13
 * Time: 10:25 PM
 *
 * @author Romain Gilles
 * @since 1.0
 */
abstract class RegistryAnnotatedBindingBuilderImpl implements RegistryAnnotatedBindingBuilder {


    private final Binder binder;
    private Key key;
    private LinkedBindingBuilder linkedBindingBuilder;
    //Lax type binding represent qualified versus non qualified types
    private boolean laxTypeBinding = false;
    private AnnotatedBindingBuilder annotatedBindingBuilder;


    RegistryAnnotatedBindingBuilderImpl(Binder binder, Key key, LinkedBindingBuilder bindingBuilder) {
        this.binder = requireNonNull(binder, "binder");
        this.key = requireNonNull(key, "key");
        laxTypeBinding = false;
        annotatedBindingBuilder = null;
        linkedBindingBuilder = bindingBuilder;
    }

    RegistryAnnotatedBindingBuilderImpl(Binder binder, TypeLiteral typeLiteral, AnnotatedBindingBuilder bindingBuilder) {
        this.binder = requireNonNull(binder, "binder");
        key = Key.get(typeLiteral);
        laxTypeBinding = true;
        linkedBindingBuilder = annotatedBindingBuilder = bindingBuilder;
    }

    RegistryAnnotatedBindingBuilderImpl(Binder binder, Class clazz, AnnotatedBindingBuilder bindingBuilder) {
        this.binder = requireNonNull(binder, "binder");
        key = Key.get(clazz);
        laxTypeBinding = true;
        linkedBindingBuilder = annotatedBindingBuilder = bindingBuilder;
    }

    Key key() {
        return key;
    }

    @Override
    public RegistryLinkedBindingBuilder annotatedWith(Class annotationType) {
        key = Key.get(key.getTypeLiteral(), annotationType);
        laxTypeBinding = false;
        linkedBindingBuilder = annotatedBindingBuilder.annotatedWith(annotationType);
        return this;
    }

    @Override
    public RegistryLinkedBindingBuilder annotatedWith(Annotation annotation) {
        key = Key.get(key.getTypeLiteral(), annotation);
        laxTypeBinding = false;
        linkedBindingBuilder = annotatedBindingBuilder.annotatedWith(annotation);
        return this;
    }

    @Override
    public RegistryBindingBuilder toRegistry() {
        Iterable> registryProviders = doToRegistry();
        return new RegistryBindingBuilderImpl<>(registryProviders);
    }

    abstract Iterable> doToRegistry();

    @Override
    public ScopedBindingBuilder to(Class implementation) {
        linkedBindingBuilder().to(implementation);
        return this;
    }

    @Override
    public ScopedBindingBuilder to(TypeLiteral implementation) {
        linkedBindingBuilder().to(implementation);
        return this;
    }

    @Override
    public ScopedBindingBuilder to(Key targetKey) {
        linkedBindingBuilder().to(targetKey);
        return this;
    }

    @Override
    public void toInstance(T instance) {
        linkedBindingBuilder().toInstance(instance);
    }

    @Override
    public ScopedBindingBuilder toProvider(Provider provider) {
        linkedBindingBuilder().toProvider(provider);
        return this;
    }

    @Override
    public ScopedBindingBuilder toProvider(javax.inject.Provider provider) {
        linkedBindingBuilder().toProvider(provider);
        return this;
    }

    @Override
    public ScopedBindingBuilder toProvider(Class> providerType) {
        linkedBindingBuilder().toProvider(providerType);
        return this;
    }

    @Override
    public ScopedBindingBuilder toProvider(TypeLiteral> providerType) {
        linkedBindingBuilder().toProvider(providerType);
        return this;
    }

    @Override
    public ScopedBindingBuilder toProvider(Key> providerKey) {
        linkedBindingBuilder().toProvider(providerKey);
        return this;
    }

    @Override
    public  ScopedBindingBuilder toConstructor(Constructor constructor) {
        linkedBindingBuilder().toConstructor(constructor);
        return this;
    }

    @Override
    public  ScopedBindingBuilder toConstructor(Constructor constructor, TypeLiteral type) {
        linkedBindingBuilder().toConstructor(constructor, type);
        return this;
    }

    @Override
    public void in(Class scopeAnnotation) {
        linkedBindingBuilder().in(scopeAnnotation);
    }

    @Override
    public void in(Scope scope) {
        linkedBindingBuilder().in(scope);
    }

    @Override
    public void asEagerSingleton() {
        linkedBindingBuilder().asEagerSingleton();
    }

    LinkedBindingBuilder linkedBindingBuilder() {
        return requireNonNull(linkedBindingBuilder, "linkedBindingBuilder");
    }

    Binder binder() {
        return binder;
    }


    boolean isLaxTypeBinding() {
        return laxTypeBinding;
    }

}