/** * {@inheritDoc} */ @Override public boolean hasChildren(Object element) { InvocationTreeElement ite = InvocationTreeUtil.lookupTreeElement(lookupMap, element); if (ite == null) { return false; } return ite.hasChildren(); }
/** * Starts resolving the tree elements. */ private void resolve() { if (tree.isSpan()) { resolveSpan(tree); } else { resolveInvocationSequence(tree); } }
/** * {@inheritDoc} */ @Override public Object getParent(Object element) { InvocationTreeElement ite = InvocationTreeUtil.lookupTreeElement(lookupMap, element); if ((ite == null) || (ite.getParent() == null)) { return null; } else { return ite.getParent().getDataElement(); } }
/** * Builds the {@link #lookupMap} map of the given tree. This allows direct access to the tree * elements via data objects. * * @param tree * the tree * @return the lookup map for the given tree */ public static Map<Object, InvocationTreeElement> buildLookupMap(InvocationTreeElement tree) { return InvocationTreeUtil.getRoot(tree).asStream().collect(Collectors.toMap(e -> calculateLookupKey(e.getDataElement()), Function.identity())); }
/** * {@inheritDoc} */ @Override public Object[] getChildren(Object inputElement) { InvocationTreeElement treeElement = InvocationTreeUtil.lookupTreeElement(lookupMap, inputElement); List<Object> objects = new ArrayList<>(); for (InvocationTreeElement its : treeElement.getChildren()) { objects.add(its.getDataElement()); } return objects.toArray(); }
@Test public void successful01() { Map<Object, InvocationTreeElement> lookupMap = InvocationTreeUtil.buildLookupMap(tree); InvocationTreeElement treeElement = InvocationTreeUtil.lookupTreeElement(lookupMap, _invoc02); assertThat(treeElement.getDataElement(), is(_invoc02)); }
/** * Builds the tree. * * @return The root {@link InvocationTreeElement} of the resulting tree. If no tree could be * built, e.g. when finding no root element <code>null</code> will be returned. */ public InvocationTreeElement build() { prepare(); tree = findRoot(); if (tree == null) { return null; } resolve(); sortChildren(tree); resolveSpanDetails(); return tree; }
/** * {@inheritDoc} */ @Override public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { InvocationTreeElement input = validateInput(newInput); if (input == null) { return; } lookupMap = InvocationTreeUtil.buildLookupMap(input); rootElement = InvocationTreeUtil.getRoot(input); }
@Test public void disabledCache() { InvocationSequenceData invoc01 = createSequence(null); builder.setMode(Mode.SINGLE).setInvocationSequence(invoc01); InvocationTreeElement treeOne = builder.build(); InvocationTreeElement treeTwo = builder.build(); assertThat(treeOne, is(not(sameInstance(treeTwo)))); }
/** * {@inheritDoc} */ @Override public Object[] getElements(Object inputElement) { InvocationTreeElement element = validateInput(inputElement); if (element == null) { return new Object[0]; } return new Object[] { element.getDataElement() }; }
@Test(expectedExceptions = IllegalStateException.class) public void noTraceId() { builder.setSpanService(spanService).setInvocationService(invocationService); builder.build(); }
/** * Returns the root element of the tree where the given element belongs to. * * @param element * the {@link InvocationTreeElement} to find the root for * @return the root {@link InvocationTreeElement} of the current tree */ public static InvocationTreeElement getRoot(InvocationTreeElement element) { if (element == null) { return null; } while (element.getParent() != null) { element = element.getParent(); } return element; }
/** * {@inheritDoc} */ @Override public Object[] getObjectsToSearch(Object treeInput) { return InvocationTreeUtil.getDataElements(spanTree, false).toArray(); }
/** * Creates a new {@link InvocationTreeElement} without a parent. * * @param dataElement * the data element of the created {@link InvocationTreeElement} * @return the newly created {@link InvocationTreeElement} */ private InvocationTreeElement createTreeElement(Object dataElement) { return createTreeElement(dataElement, null); }
/** * Adds a new child to this element and sets the child's parent. * * @param child * {@lin InvocationTreeElement} to add as a child */ void addChild(InvocationTreeElement child) { children.add(child); child.setParent(this); }
/** * The comparison is based on the elements' timestamps. * <p> * {@inheritDoc} * */ @Override public int compareTo(InvocationTreeElement o) { return ObjectUtils.compare(getTimeStamp(), o.getTimeStamp()); }
/** * Creates a string representation of the tree starting the given {@link InvocationTreeElement}. * * @param tree * starting element of the tree * @return string representation of the tree */ public static String stringify(InvocationTreeElement tree) { StringBuilder builder = new StringBuilder(); stringifyHelper(tree, builder, 0); return builder.toString(); }
@Test(expectedExceptions = IllegalArgumentException.class) public void useNullValue() { InvocationTreeUtil.isChildOfSpan(null); } }
ServerSpan createSdkSpan(Span parent) { ServerSpan span = createServerSpan(parent); span.setPropagationType(null); return span; }
@Test public void multipleCalls() { InvocationSequenceData invoc01 = createSequence(null); builder.setMode(Mode.SINGLE).setInvocationSequence(invoc01); InvocationTreeElement treeOne = builder.build(); InvocationTreeElement treeTwo = builder.build(); assertThat(treeOne, is(not(sameInstance(treeTwo)))); }