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

net.yudichev.jiotty.common.inject.SpecifiedAnnotation Maven / Gradle / Ivy

package net.yudichev.jiotty.common.inject;

import com.google.inject.Key;
import com.google.inject.TypeLiteral;

import java.lang.annotation.Annotation;
import java.util.function.Function;

import static com.google.common.base.Preconditions.checkNotNull;

public final class SpecifiedAnnotation {
    private static final SpecifiedAnnotation NO_ANNOTATION = new SpecifiedAnnotation(Key::get);

    private final Function, Key> keyFactory;

    private SpecifiedAnnotation(Function, Key> keyFactory) {
        this.keyFactory = checkNotNull(keyFactory);
    }

    public static SpecifiedAnnotation forAnnotation(Annotation annotation) {
        return new SpecifiedAnnotation(typeLiteral -> Key.get(typeLiteral, annotation));
    }

    public static SpecifiedAnnotation forAnnotation(Class annotationClass) {
        return new SpecifiedAnnotation(typeLiteral -> Key.get(typeLiteral, annotationClass));
    }

    public static SpecifiedAnnotation forNoAnnotation() {
        return NO_ANNOTATION;
    }

    @SuppressWarnings("unchecked") // guaranteed by method signature
    public  Key specify(TypeLiteral typeLiteral) {
        return (Key) keyFactory.apply(typeLiteral);
    }

    public  Key specify(Class type) {
        return specify(TypeLiteral.get(type));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy