jnr.ffi.util.Annotations Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jnr-ffi Show documentation
Show all versions of jnr-ffi Show documentation
A library for invoking native functions from java
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);
}
}