Skip to content

Instantly share code, notes, and snippets.

@electrum
Created July 2, 2013 22:12
Show Gist options
  • Save electrum/5913708 to your computer and use it in GitHub Desktop.
Save electrum/5913708 to your computer and use it in GitHub Desktop.
ClassQualifier
import javax.inject.Qualifier;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@Target({FIELD, PARAMETER, METHOD})
@Retention(RUNTIME)
@Qualifier
public @interface ClassQualifier
{
Class<?> value();
}
import java.lang.annotation.Annotation;
import static java.util.Objects.requireNonNull;
@SuppressWarnings("ClassExplicitlyAnnotation")
public class ClassQualifierImpl
implements ClassQualifier
{
private final Class<?> value;
public static ClassQualifier classQualifier(Class<?> value)
{
return new ClassQualifierImpl(value);
}
private ClassQualifierImpl(Class<?> value)
{
this.value = requireNonNull(value, "value is null");
}
@Override
public Class<?> value()
{
return value;
}
@Override
public Class<? extends Annotation> annotationType()
{
return ClassQualifier.class;
}
@Override
public int hashCode()
{
// this is specified in java.lang.annotation.Annotation
return (127 * "value".hashCode()) ^ value.hashCode();
}
@Override
public boolean equals(Object o)
{
if (!(o instanceof ClassQualifier)) {
return false;
}
return value.equals(((ClassQualifier) o).value());
}
@Override
public String toString()
{
return "@" + ClassQualifier.class.getName() + "(value=" + value.getName() + ")";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment