private List<SpanNode> getSpanNodes() { ArrayList<SpanNode> nodes = new ArrayList<SpanNode>(); nodes.add(root); Iterator<SpanNode> it = root.childIteratorRecursive(); while (it.hasNext()) { nodes.add(it.next()); } return nodes; }
@Override public int compareTo(SpanTree spanTree) { int comp = name.compareTo(spanTree.name); if (comp != 0) { comp = root.compareTo(spanTree.root); } return comp; }
void checkValidity(SpanNode node, List<SpanNode> childrenToCheck) { if (!node.isValid()) { throw new IllegalStateException("Cannot reuse SpanNode instance " + node + ", is INVALID."); } if (node.getParent() != null) { if (node.getParent() != this) { throw new IllegalStateException(node + " is already a child of " + node.getParent() + ", cannot be added to " + this); } else if (node.getParent() == this && childrenToCheck.contains(node)) { throw new IllegalStateException(node + " is already a child of " + this + ", cannot be added twice to the same parent node."); } } }
/** * WARNING! Only to be used by deserializers! Sets the root of this SpanTree instance. * * @param root the root to set for this SpanTree instance. */ public void setRoot(SpanNode root) { if (!root.isValid()) { throw new IllegalStateException("Cannot use invalid node " + root + " as root node."); } if (root.getParent() != null) { if (root.getParent() != this) { throw new IllegalStateException(root + " is already a child of " + root.getParent() + ", cannot be root of " + this); } } this.root = root; root.setParent(this); }
private void calculateTo() { int greatestTo = Integer.MIN_VALUE; for (SpanNode n : children()) { greatestTo = Math.max(n.getTo(), greatestTo); } cachedTo = greatestTo; }
private void calculateFrom() { int smallestFrom = Integer.MAX_VALUE; for (SpanNode n : children()) { final int thisFrom = n.getFrom(); if (thisFrom != -1) { smallestFrom = Math.min(thisFrom, smallestFrom); } } if (smallestFrom == Integer.MAX_VALUE) { //all children were empty SpanLists which returned -1 smallestFrom = -1; } cachedFrom = smallestFrom; }
/** * Convenience method for adding an annotation to this span, same as * <code>getSpanTree().{@link SpanTree#annotate(SpanNode,AnnotationType,FieldValue) spanTree.annotate(this,type,value)}</code> * * @param type the type of the annotation to add * @param value the value of the annotation to add * @return this for chaining * @throws NullPointerException if this span is not attached to a tree */ public SpanNode annotate(AnnotationType type,FieldValue value) { getNonNullSpanTree().annotate(this,type,value); return this; }
@Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof SpanTree)) return false; SpanTree tree = (SpanTree) o; if (!annotationsEquals(tree)) return false; if (!name.equals(tree.name)) return false; if (!root.equals(tree.root)) return false; return true; }
int spanNodeId = annotation.getSpanNode().getScratchId(); if (spanNodeId >= 0) { buf.putInt1_2_4Bytes(spanNodeId);
/** Returns the SpanTree this belongs to and throws a nice NullPointerException if none */ private SpanTree getNonNullSpanTree() { SpanTree spanTree=getSpanTree(); if (spanTree==null) throw new NullPointerException(this + " is not attached to a SpanTree through its parent yet"); return spanTree; }
@Override public boolean hasNext() { if (stack.isEmpty()) { return false; } PeekableListIterator<SpanNode> iterator = stack.peek(); if (!iterator.hasNext()) { stack.pop(); return hasNext(); } SpanNode node = iterator.peek(); if (!iterator.traversed) { //we set the traversed flag on our way down iterator.traversed = true; stack.push(new PeekableListIterator<SpanNode>(node.childIterator())); return hasNext(); } return true; }
/** * Convenience method for adding an annotation to this span, same as * <code>getSpanTree().{@link SpanTree#annotate(SpanNode,Annotation) spanTree.annotate(this,annotation)}</code> * * @param annotation the annotation to add * @return this for chaining * @throws NullPointerException if this span is not attached to a tree */ public SpanNode annotate(Annotation annotation) { getNonNullSpanTree().annotate(this, annotation); return this; }
@Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof Annotation)) return false; Annotation that = (Annotation) o; if (!type.equals(that.type)) return false; if (spanNode != null ? !spanNode.equals(that.spanNode) : that.spanNode != null) return false; if (value != null ? !value.equals(that.value) : that.value != null) return false; return true; }
int spanNodeId = annotation.getSpanNode().getScratchId(); if (spanNodeId >= 0) { buf.putInt1_2_4Bytes(spanNodeId);
/** * Convenience method for adding an annotation with no value to this span, same as * <code>getSpanTree().{@link SpanTree#annotate(SpanNode,AnnotationType) spanTree.annotate(this,type)}</code> * * @param type the type of the annotation to add * @return this for chaining * @throws NullPointerException if this span is not attached to a tree */ public SpanNode annotate(AnnotationType type) { getNonNullSpanTree().annotate(this,type); return this; }
@Override Iterator<Annotation> iteratorRecursive(SpanNode node) { IdentityHashMap<SpanNode, SpanNode> nodes = new IdentityHashMap<SpanNode, SpanNode>(); nodes.put(node, node); { Iterator<SpanNode> childrenIt = node.childIteratorRecursive(); while (childrenIt.hasNext()) { SpanNode child = childrenIt.next(); nodes.put(child, child); } } return iterator(nodes); }
@Override public boolean equals(Object o) { if (this == o) return true; if (!(o instanceof SpanList)) return false; if (!super.equals(o)) return false; SpanList spanList = (SpanList) o; if (children() != null ? !children().equals(spanList.children()) : spanList.children() != null) return false; return true; }
@Override public int compareTo(Annotation annotation) { int comp; if (spanNode == null) { comp = (annotation.spanNode == null) ? 0 : -1; } else { comp = (annotation.spanNode == null) ? 1 : spanNode.compareTo(annotation.spanNode); } if (comp != 0) { return comp; } comp = type.compareTo(annotation.type); if (comp != 0) { return comp; } //types are equal, too, compare values if (value == null) { comp = (annotation.value == null) ? 0 : -1; } else { comp = (annotation.value == null) ? 1 : value.compareTo(annotation.value); } return comp; } }