- return Collections.unmodifiableSet(fLabels);
- }
-
- /*
- * Maps integer range -> string. A simple list for now, but feel free to
- * optimize it. Babeltrace suggests an interval tree.
- */
- private class EnumTable {
-
- private final List<LabelAndRange> ranges = new LinkedList<>();
-
- public EnumTable() {
- }
-
- public synchronized boolean add(@Nullable String label) {
- LabelAndRange lastAdded = ranges.isEmpty() ? new LabelAndRange(-1, -1, "") : ranges.get(ranges.size() - 1); //$NON-NLS-1$
- return add(lastAdded.low + 1, lastAdded.high + 1, label);
- }
-
- public synchronized boolean add(long low, long high, @Nullable String label) {
- LabelAndRange newRange = new LabelAndRange(low, high, label);
-
- for (LabelAndRange r : ranges) {
- if (r.intersects(newRange)) {
- return false;
- }
- }
-
- ranges.add(newRange);
-
- return true;
- }
-
- /**
- * Return the first label that matches a value
- *
- * @param value
- * the value to query
- * @return the label corresponding to that value
- */
- public synchronized @Nullable String query(long value) {
- for (LabelAndRange r : ranges) {
- if (r.intersects(value)) {
- return r.getLabel();
- }
- }
- return null;
- }
-
- @Override
- public synchronized int hashCode() {
- final int prime = 31;
- int result = 1;
- for (LabelAndRange range : ranges) {
- result = prime * result + range.hashCode();
- }
- return result;
- }
-
- @Override
- public synchronized boolean equals(@Nullable Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- EnumTable other = (EnumTable) obj;
- if (ranges.size() != other.ranges.size()) {
- return false;
- }
- for (int i = 0; i < ranges.size(); i++) {
- if (!ranges.get(i).equals(other.ranges.get(i))) {
- return false;
- }
- }
- return true;
- }
-
- }
-
- private static class LabelAndRange {
-
- private final long low, high;
- private final @Nullable String fLabel;
-
- /**
- * Get the label
- *
- * @return the label
- */
- public @Nullable String getLabel() {
- return fLabel;
- }
-
- public LabelAndRange(long low, long high, @Nullable String str) {
- this.low = low;
- this.high = high;
- this.fLabel = str;
- }
-
- public boolean intersects(long i) {
- return (i >= this.low) && (i <= this.high);
- }
-
- public boolean intersects(LabelAndRange other) {
- return this.intersects(other.low)
- || this.intersects(other.high);
- }
-
- @Override
- public int hashCode() {
- final int prime = 31;
- int result = 1;
- final String label = fLabel;
- result = prime * result + ((label == null) ? 0 : label.hashCode());
- result = prime * result + (int) (high ^ (high >>> 32));
- result = prime * result + (int) (low ^ (low >>> 32));
- return result;
- }
-
- @Override
- public boolean equals(@Nullable Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- LabelAndRange other = (LabelAndRange) obj;
- final String label = fLabel;
- if (label == null) {
- if (other.fLabel != null) {
- return false;
- }
- } else if (!label.equals(other.fLabel)) {
- return false;
- }
- if (high != other.high) {
- return false;
- }
- if (low != other.low) {
- return false;
- }
- return true;
- }