LCOV - code coverage report
Current view: top level - server/util - TreeFormatter.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 0 39 0.0 %
Date: 2022-11-19 15:00:39 Functions: 0 6 0.0 %

          Line data    Source code
       1             : // Copyright (C) 2011 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.util;
      16             : 
      17             : import java.io.PrintWriter;
      18             : import java.util.NavigableSet;
      19             : 
      20             : public class TreeFormatter {
      21             : 
      22             :   public interface TreeNode {
      23             :     String getDisplayName();
      24             : 
      25             :     boolean isVisible();
      26             : 
      27             :     NavigableSet<? extends TreeNode> getChildren();
      28             :   }
      29             : 
      30             :   public static final String NOT_VISIBLE_NODE = "(x)";
      31             : 
      32             :   private static final String NODE_PREFIX = "|-- ";
      33             :   private static final String LAST_NODE_PREFIX = "`-- ";
      34             :   private static final String DEFAULT_TAB_SEPARATOR = "|";
      35             : 
      36             :   private final PrintWriter stdout;
      37           0 :   private String currentTabSeparator = " ";
      38             : 
      39           0 :   public TreeFormatter(PrintWriter stdout) {
      40           0 :     this.stdout = stdout;
      41           0 :   }
      42             : 
      43             :   public void printTree(NavigableSet<? extends TreeNode> rootNodes) {
      44           0 :     if (rootNodes.isEmpty()) {
      45           0 :       return;
      46             :     }
      47           0 :     if (rootNodes.size() == 1) {
      48           0 :       printTree(rootNodes.first());
      49             :     } else {
      50           0 :       currentTabSeparator = DEFAULT_TAB_SEPARATOR;
      51           0 :       int i = 0;
      52           0 :       final int size = rootNodes.size();
      53           0 :       for (TreeNode rootNode : rootNodes) {
      54           0 :         final boolean isLastRoot = ++i == size;
      55           0 :         if (isLastRoot) {
      56           0 :           currentTabSeparator = " ";
      57             :         }
      58           0 :         printTree(rootNode);
      59           0 :       }
      60             :     }
      61           0 :   }
      62             : 
      63             :   public void printTree(TreeNode rootNode) {
      64           0 :     printTree(rootNode, 0, true);
      65           0 :   }
      66             : 
      67             :   private void printTree(TreeNode node, int level, boolean isLast) {
      68           0 :     printNode(node, level, isLast);
      69           0 :     final NavigableSet<? extends TreeNode> childNodes = node.getChildren();
      70           0 :     int i = 0;
      71           0 :     final int size = childNodes.size();
      72           0 :     for (TreeNode childNode : childNodes) {
      73           0 :       final boolean isLastChild = ++i == size;
      74           0 :       printTree(childNode, level + 1, isLastChild);
      75           0 :     }
      76           0 :   }
      77             : 
      78             :   private void printIndention(int level) {
      79           0 :     if (level > 0) {
      80           0 :       stdout.print(String.format("%-" + 4 * level + "s", currentTabSeparator));
      81             :     }
      82           0 :   }
      83             : 
      84             :   private void printNode(TreeNode node, int level, boolean isLast) {
      85           0 :     printIndention(level);
      86           0 :     stdout.print(isLast ? LAST_NODE_PREFIX : NODE_PREFIX);
      87           0 :     if (node.isVisible()) {
      88           0 :       stdout.print(node.getDisplayName());
      89             :     } else {
      90           0 :       stdout.print(NOT_VISIBLE_NODE);
      91             :     }
      92           0 :     stdout.print("\n");
      93           0 :   }
      94             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750