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

com.github.jsr330.spi.config.builder.BindingConditions Maven / Gradle / Ivy

package com.github.jsr330.spi.config.builder;

import java.lang.annotation.Annotation;
import java.util.Map;

import javax.inject.Named;

import com.github.jsr330.spi.ClassInjector;

public class BindingConditions {
    
    @SuppressWarnings("unchecked")
    public static  BindingCondition annotationIsPresent(Class type, Class annotation) {
        return anyAnnotationIsPresent(type, annotation);
    }
    
    public static  BindingCondition qualifierIs(Class type, final Class expectedQualifier) {
        return new BindingCondition() {
            
            @Override
            public boolean fulfilled(ClassInjector injector, Class type, Map[]> inheritanceTree, Annotation qualifier,
                    ClassLoader classLoader) {
                return qualifier != null && expectedQualifier.isAssignableFrom(qualifier.annotationType());
            }
            
        };
    }
    
    public static  BindingCondition isNamed(Class type, final CharSequence value) {
        return new BindingCondition() {
            
            @Override
            public boolean fulfilled(ClassInjector injector, Class type, Map[]> inheritanceTree, Annotation qualifier,
                    ClassLoader classLoader) {
                return qualifier instanceof Named && ((Named) qualifier).value().equals(value);
            }
            
        };
    }
    
    public static  BindingCondition isNamedIgnoringCase(Class type, final CharSequence value) {
        return new BindingCondition() {
            
            @Override
            public boolean fulfilled(ClassInjector injector, Class type, Map[]> inheritanceTree, Annotation qualifier,
                    ClassLoader classLoader) {
                return qualifier instanceof Named && ((Named) qualifier).value().equalsIgnoreCase(value.toString());
            }
            
        };
    }
    
    public static  BindingCondition allAnnotationsArePresent(Class type, final Class... annotations) {
        return new BindingCondition() {
            
            @Override
            public boolean fulfilled(ClassInjector injector, Class type, Map[]> inheritanceTree, Annotation qualifier,
                    ClassLoader classLoader) {
                boolean exists = false;
                
                if (type != null && annotations != null && annotations.length > 0) {
                    for (Class cmp : annotations) {
                        exists = false;
                        for (Annotation annotation : type.getAnnotations()) {
                            if (annotation.annotationType().equals(cmp)) {
                                exists = true;
                                break;
                            }
                        }
                        if (!exists) {
                            return false;
                        }
                        exists = true;
                    }
                    return true;
                }
                return false;
            }
            
        };
    }
    
    public static  BindingCondition anyAnnotationIsPresent(Class type, final Class... annotations) {
        return new BindingCondition() {
            
            @Override
            public boolean fulfilled(ClassInjector injector, Class type, Map[]> inheritanceTree, Annotation qualifier,
                    ClassLoader classLoader) {
                boolean exists = false;
                
                if (type != null && annotations != null && annotations.length > 0) {
                    for (Annotation annotation : type.getAnnotations()) {
                        exists = false;
                        for (Class cmp : annotations) {
                            if (annotation.annotationType().equals(cmp)) {
                                exists = true;
                                break;
                            }
                        }
                        if (!exists) {
                            return exists;
                        }
                        exists = true;
                    }
                }
                return exists;
            }
            
        };
    }
    
    public static  BindingCondition and(final BindingCondition... conditions) {
        return new BindingCondition() {
            
            @Override
            public boolean fulfilled(ClassInjector injector, Class type, Map[]> inheritanceTree, Annotation qualifier,
                    ClassLoader classLoader) {
                if (conditions != null && conditions.length > 1) {
                    for (BindingCondition condition : conditions) {
                        if (!condition.fulfilled(injector, type, inheritanceTree, qualifier, classLoader)) {
                            return false;
                        }
                    }
                } else if (conditions.length > 0) {
                    return conditions[0].fulfilled(injector, type, inheritanceTree, qualifier, classLoader);
                }
                return true;
            }
            
        };
    }
    
    public static  BindingCondition or(final BindingCondition... conditions) {
        return new BindingCondition() {
            
            @Override
            public boolean fulfilled(ClassInjector injector, Class type, Map[]> inheritanceTree, Annotation qualifier,
                    ClassLoader classLoader) {
                if (conditions != null && conditions.length > 1) {
                    for (BindingCondition condition : conditions) {
                        if (condition.fulfilled(injector, type, inheritanceTree, qualifier, classLoader)) {
                            return true;
                        }
                    }
                    return false;
                } else if (conditions.length > 0) {
                    return conditions[0].fulfilled(injector, type, inheritanceTree, qualifier, classLoader);
                }
                return true;
            }
            
        };
    }
    
    public static  BindingCondition xor(final BindingCondition condition1, final BindingCondition condition2) {
        return new BindingCondition() {
            
            @Override
            public boolean fulfilled(ClassInjector injector, Class type, Map[]> inheritanceTree, Annotation qualifier,
                    ClassLoader classLoader) {
                if (condition1 != null && condition2 != null) {
                    return condition1.fulfilled(injector, type, inheritanceTree, qualifier, classLoader) != condition2.fulfilled(injector, type,
                            inheritanceTree, qualifier, classLoader);
                }
                return true;
            }
            
        };
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy