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

net.intelie.pipes.time.SpanAnnotations Maven / Gradle / Ivy

There is a newer version: 0.25.5
Show newest version
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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy