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

net.digitalid.utility.conversion.model.CustomAnnotation Maven / Gradle / Ivy

The newest version!
package net.digitalid.utility.conversion.model;

import java.lang.annotation.Annotation;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import net.digitalid.utility.annotations.method.Pure;
import net.digitalid.utility.immutable.ImmutableMap;
import net.digitalid.utility.validation.annotations.type.Immutable;

/**
 * This class stores information about the field annotations parsed during compile-time. Type-use annotations and field annotations are treated equally.
 */
@Immutable
public class CustomAnnotation {
    
    /* -------------------------------------------------- Annotation Type -------------------------------------------------- */
    
    private final @Nonnull Class annotationType;
    
    public @Nonnull Class getAnnotationType() {
        return annotationType;
    }
    
    /* -------------------------------------------------- Annotation Fields -------------------------------------------------- */
    
    private final @Nonnull ImmutableMap<@Nonnull String, @Nullable Object> annotationFields;
    
    @SuppressWarnings("unchecked")
    public  @Nullable T get(@Nonnull String fieldName, @Nonnull Class type) {
        return (T) annotationFields.get(fieldName);
    }
    
    /* -------------------------------------------------- Custom Annotation -------------------------------------------------- */
    
    CustomAnnotation(@Nonnull Class annotationType, @Nonnull ImmutableMap<@Nonnull String, @Nullable Object> annotationFields) {
        this.annotationType = annotationType;
        this.annotationFields = annotationFields;
    }
    
    @Pure
    public static @Nonnull CustomAnnotation with(@Nonnull Class annotationType, @Nonnull ImmutableMap<@Nonnull String, @Nullable Object> annotationFields) {
        return new CustomAnnotation(annotationType, annotationFields);
    }
    
    @Pure
    public static @Nonnull CustomAnnotation with(@Nonnull Class annotationType) {
        return new CustomAnnotation(annotationType, ImmutableMap.withNoEntries());
    }
    
    /* -------------------------------------------------- Equals -------------------------------------------------- */
    
    @Override
    public boolean equals(Object object) {
        if (object == null || !(object instanceof CustomAnnotation)) {
            return false;
        } else {
            return annotationType.equals(((CustomAnnotation) object).annotationType);
        }
    }
    
    @Override
    public int hashCode() {
        return annotationType.hashCode();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy