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.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 java.util.concurrent.TimeUnit;

import static java.util.Objects.requireNonNull;

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


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

    public RegistryAnnotatedBindingBuilderImpl(Binder binder, Key key) {
        this.key = requireNonNull(key, "key");
        linkedBindingBuilder = binder.bind(key);
        laxTypeBinding = false;
    }

    public RegistryAnnotatedBindingBuilderImpl(Binder binder, TypeLiteral typeLiteral) {
        key = Key.get(typeLiteral);
        linkedBindingBuilder = annotatedBindingBuilder = binder.bind(typeLiteral);
        laxTypeBinding = true;
    }

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

    Key key() {
        return key;
    }

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

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

    @Override
    public RegistryBindingBuilder toRegistry() {
        RegistryProvider registryProvider = newRegistryProvider();
        linkedBindingBuilder.toProvider(registryProvider);
        return new RegistryBindingBuilderImpl(registryProvider);
    }

    public void toRegistry(long timeout, TimeUnit unit) {
        linkedBindingBuilder.toProvider(newRegistryProvider(timeout, unit));
    }

    RegistryProvider newRegistryProvider(long timeout, TimeUnit unit) {
        return new RegistryProviderImpl<>(key(), timeout, unit);
    }

    RegistryProvider newRegistryProvider() {
        return new RegistryProviderImpl<>(key());
    }

    @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(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();
    }

    boolean isLaxTypeBinding() {
        return laxTypeBinding;
    }

}