net.intelie.pipes.time.SpanAnnotations Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pipes-api Show documentation
Show all versions of pipes-api Show documentation
Intelie Pipes' API classes and interfaces
package net.intelie.pipes.time;
import net.intelie.pipes.util.Escapes;
import net.intelie.pipes.util.Iterables;
import java.io.Serializable;
import java.util.*;
public class SpanAnnotations implements Serializable {
private static final long serialVersionUID = 1L;
public static final String TRUE = "true";
private final Map values;
public SpanAnnotations() {
this(Collections.emptyMap());
}
public SpanAnnotations(Map values) {
this.values = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
this.values.putAll(values);
}
public boolean isTrue(String key) {
return TRUE.equals(get(key));
}
public String get(String key) {
return values.get(key);
}
public Set keys() {
return Collections.unmodifiableSet(values.keySet());
}
public int size() {
return values.size();
}
public static SpanAnnotations merge(Iterable annotations) {
Map values = new LinkedHashMap<>();
for (SpanAnnotations annotation : annotations)
if (annotation != null)
values.putAll(annotation.values);
return new SpanAnnotations(values);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SpanAnnotations)) return false;
SpanAnnotations that = (SpanAnnotations) o;
return Objects.equals(values, that.values);
}
@Override
public int hashCode() {
return Objects.hash(values);
}
@Override
public String toString() {
List list = new ArrayList<>();
for (Map.Entry entry : values.entrySet()) {
list.add("#" + formatString(entry.getKey()) +
(!TRUE.equals(entry.getValue()) ? "=" + formatString(entry.getValue()) : ""));
}
return Iterables.join(" ", list);
}
private String formatString(String value) {
if (Escapes.needsIdentifierFormatting(value))
return "'" + value + "'";
else
return value;
}
}