com.wavefront.data.AnnotationUtils Maven / Gradle / Ivy
package com.wavefront.data;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Multimap;
import com.google.common.collect.SetMultimap;
import wavefront.report.Annotation;
/**
* Utility methods for working with {@code List<Annotation>}.
*
* @author [email protected]
*/
public abstract class AnnotationUtils {
private AnnotationUtils() {
}
public static SetMultimap toMultimap(@Nullable Collection annotations) {
if (annotations == null) return ImmutableSetMultimap.of();
final ImmutableSetMultimap.Builder builder = ImmutableSetMultimap.builder();
for (Annotation annotation : annotations) {
builder.put(annotation.getKey(), annotation.getValue());
}
return builder.build();
}
public static List toAnnotationList(@Nullable Multimap tags) {
if (tags == null || tags.isEmpty()) return ImmutableList.of();
return tags.entries().stream().map(t -> new Annotation(t.getKey(), t.getValue())).
collect(Collectors.toList());
}
public static List toAnnotationList(@Nullable Map tags) {
if (tags == null || tags.isEmpty()) return ImmutableList.of();
return tags.entrySet().stream().map(t -> new Annotation(t.getKey(), t.getValue())).
collect(Collectors.toList());
}
@Nullable
public static String getValue(List annotations, String key) {
return annotations.stream().filter(x -> x.getKey().equals(key)).findFirst().
map(Annotation::getValue).orElse(null);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy