Node compress()

in spectator-reg-atlas/src/main/java/com/netflix/spectator/atlas/impl/PrefixTree.java [450:482]


    Node compress() {
      // Create list of compressed children, avoid allocating the list unless
      // there is a change.
      List<Node> cs = null;
      for (int i = 0; i < children.length; ++i) {
        Node child = children[i];
        Node c = child.compress();
        if (c != child || c.isEmpty()) {
          if (cs == null) {
            cs = new ArrayList<>(children.length);
            for (int j = 0; j < i; ++j) {
              cs.add(children[j]);
            }
          }
          if (!c.isEmpty()) {
            cs.add(c);
          }
        } else if (cs != null) {
          cs.add(child);
        }
      }

      // Return compressed node. Merge nodes if intermediates have no values.
      if (cs == null) {
        return this;
      } else if (inQueries.isEmpty() && otherQueries.isEmpty() && cs.size() == 1) {
        Node c = cs.get(0);
        String p = prefix + c.prefix;
        return new Node(p, c.children, c.inQueries, c.otherQueries);
      } else {
        return new Node(prefix, cs.toArray(EMPTY), inQueries, otherQueries);
      }
    }