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

jnr.ffi.util.Annotations Maven / Gradle / Ivy

There is a newer version: 2.2.16
Show newest version
package jnr.ffi.util;

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

/**
 * Utilities for collections of annotations
 */
public final class Annotations {
    public static final Collection EMPTY_ANNOTATIONS = Collections.emptyList();

    private Annotations() {}

    public static Collection sortedAnnotationCollection(Annotation[] annotations) {
        if (annotations.length > 1) {
            return sortedAnnotationCollection(Arrays.asList(annotations));

        } else if (annotations.length > 0) {
            return Collections.singletonList(annotations[0]);

        } else {
            return Collections.emptyList();
        }
    }

    public static Collection sortedAnnotationCollection(Collection annotations) {
        // If already sorted, or empty, or only one element, no need to sort again
        if (annotations.size() < 2 || (annotations instanceof SortedSet && ((SortedSet) annotations).comparator() instanceof AnnotationNameComparator)) {
            return annotations;
        }

        SortedSet sorted = new TreeSet(AnnotationNameComparator.getInstance());
        sorted.addAll(annotations);

        return Collections.unmodifiableSortedSet(sorted);
    }

    public static final Collection mergeAnnotations(Collection a, Collection b) {
        if (a.isEmpty() && b.isEmpty()) {
            return EMPTY_ANNOTATIONS;

        } else if (!a.isEmpty() && b.isEmpty()) {
            return a;

        } else if (a.isEmpty() && !b.isEmpty()) {
            return b;

        } else {
            List all = new ArrayList(a);
            all.addAll(b);
            return sortedAnnotationCollection(all);
        }
    }

    public static final Collection mergeAnnotations(Collection... collections) {
        int totalLength = 0;
        for (Collection c : collections) {
            totalLength += c.size();
        }

        List all = new ArrayList(totalLength);
        for (Collection c : collections) {
            all.addAll(c);
        }
        
        return sortedAnnotationCollection(all);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy