BSPTreePrinter.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.geometry.core.partitioning.bsp;

  18. import org.apache.commons.geometry.core.Point;
  19. import org.apache.commons.geometry.core.partitioning.bsp.BSPTree.Node;

  20. /** Internal class for creating simple string representations of BSP trees.
  21.  * @param <P> Point implementation type
  22.  * @param <N> Node implementation type
  23.  */
  24. final class BSPTreePrinter<P extends Point<P>, N extends Node<P, N>>
  25.     implements BSPTreeVisitor<P, N> {

  26.     /** Line indent string. */
  27.     private static final String INDENT = "    ";

  28.     /** New line character. */
  29.     private static final String NEW_LINE = "\n";

  30.     /** Entry prefix for nodes on the minus side of their parent. */
  31.     private static final String MINUS_CHILD = "[-] ";

  32.     /** Entry prefix for nodes on the plus side of their parent. */
  33.     private static final String PLUS_CHILD = "[+] ";

  34.     /** Ellipsis for truncated representations. */
  35.     private static final String ELLIPSIS = "...";

  36.     /** Maximum depth of nodes that will be printed. */
  37.     private final int maxDepth;

  38.     /** Contains the string output. */
  39.     private final StringBuilder output = new StringBuilder();

  40.     /** Simple constructor.
  41.      * @param maxDepth maximum depth of nodes to be printed
  42.      */
  43.     BSPTreePrinter(final int maxDepth) {
  44.         this.maxDepth = maxDepth;
  45.     }

  46.     /** {@inheritDoc} */
  47.     @Override
  48.     public Result visit(final N node) {
  49.         final int depth = node.depth();

  50.         if (depth <= maxDepth) {
  51.             startLine(node);
  52.             writeNode(node);
  53.         } else if (depth == maxDepth + 1 && node.isPlus()) {
  54.             startLine(node);
  55.             write(ELLIPSIS);
  56.         }

  57.         return Result.CONTINUE;
  58.     }

  59.     /** {@inheritDoc} */
  60.     @Override
  61.     public Order visitOrder(final N node) {
  62.         if (node.depth() > maxDepth + 1) {
  63.             return Order.NONE;
  64.         }
  65.         return Order.NODE_MINUS_PLUS;
  66.     }

  67.     /** Start a line for the given node.
  68.      * @param node the node to begin a line for
  69.      */
  70.     private void startLine(final N node) {
  71.         if (node.getParent() != null) {
  72.             write(NEW_LINE);
  73.         }

  74.         final int depth = node.depth();
  75.         for (int i = 0; i < depth; ++i) {
  76.             write(INDENT);
  77.         }
  78.     }

  79.     /** Writes the given node to the output.
  80.      * @param node the node to write
  81.      */
  82.     private void writeNode(final N node) {
  83.         if (node.getParent() != null) {
  84.             if (node.isMinus()) {
  85.                 write(MINUS_CHILD);
  86.             } else {
  87.                 write(PLUS_CHILD);
  88.             }
  89.         }

  90.         write(node.toString());
  91.     }

  92.     /** Add the given string to the output.
  93.      * @param str the string to add
  94.      */
  95.     private void write(final String str) {
  96.         output.append(str);
  97.     }

  98.     /** Return the string representation of the visited tree. */
  99.     @Override
  100.     public String toString() {
  101.         return output.toString();
  102.     }
  103. }