Printer.java

  1. /*
  2.  *  Copyright (c) 2001-2024, Jean Tessier
  3.  *  All rights reserved.
  4.  *  
  5.  *  Redistribution and use in source and binary forms, with or without
  6.  *  modification, are permitted provided that the following conditions
  7.  *  are met:
  8.  *  
  9.  *      * Redistributions of source code must retain the above copyright
  10.  *        notice, this list of conditions and the following disclaimer.
  11.  *  
  12.  *      * Redistributions in binary form must reproduce the above copyright
  13.  *        notice, this list of conditions and the following disclaimer in the
  14.  *        documentation and/or other materials provided with the distribution.
  15.  *  
  16.  *      * Neither the name of Jean Tessier nor the names of his contributors
  17.  *        may be used to endorse or promote products derived from this software
  18.  *        without specific prior written permission.
  19.  *  
  20.  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21.  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22.  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23.  *  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
  24.  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25.  *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26.  *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27.  *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28.  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29.  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30.  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  */

  32. package com.jeantessier.dependency;

  33. import java.io.*;

  34. public abstract class Printer extends VisitorBase {
  35.     public static final String DEFAULT_INDENT_TEXT = "    ";

  36.     private final PrintWriter out;

  37.     private String indentText = DEFAULT_INDENT_TEXT;
  38.     private int indentLevel = 0;
  39.     private boolean showInbounds = true;
  40.     private boolean showOutbounds = true;
  41.     private boolean showEmptyNodes = true;

  42.     public Printer(PrintWriter out) {
  43.         this(getDefaultStrategy(), out);
  44.     }

  45.     public Printer(TraversalStrategy strategy, PrintWriter out) {
  46.         super(new SortedTraversalStrategy(strategy));

  47.         this.out = out;
  48.     }

  49.     public String getIndentText() {
  50.         return indentText;
  51.     }

  52.     public void setIndentText(String indentText) {
  53.         this.indentText = indentText;
  54.     }

  55.     public boolean isShowInbounds() {
  56.         return showInbounds;
  57.     }

  58.     public void setShowInbounds(boolean showInbounds) {
  59.         this.showInbounds = showInbounds;
  60.     }
  61.    
  62.     public boolean isShowOutbounds() {
  63.         return showOutbounds;
  64.     }

  65.     public void setShowOutbounds(boolean showOutbounds) {
  66.         this.showOutbounds = showOutbounds;
  67.     }
  68.    
  69.     public boolean isShowEmptyNodes() {
  70.         return showEmptyNodes;
  71.     }

  72.     public void setShowEmptyNodes(boolean showEmptyNodes) {
  73.         this.showEmptyNodes = showEmptyNodes;
  74.     }

  75.     public void visitPackageNode(PackageNode node) {
  76.         if (shouldShowPackageNode(node)) {
  77.             super.visitPackageNode(node);
  78.         }
  79.     }

  80.     public void visitClassNode(ClassNode node) {
  81.         if (shouldShowClassNode(node)) {
  82.             super.visitClassNode(node);
  83.         }
  84.     }

  85.     public void visitFeatureNode(FeatureNode node) {
  86.         if (shouldShowFeatureNode(node)) {
  87.             super.visitFeatureNode(node);
  88.         }
  89.     }

  90.     protected Printer append(boolean b) {
  91.         out.print(b);
  92.         return this;
  93.     }

  94.     protected Printer append(char c) {
  95.         out.print(c);
  96.         return this;
  97.     }

  98.     protected Printer append(char[] s) {
  99.         out.print(s);
  100.         return this;
  101.     }

  102.     protected Printer append(double d) {
  103.         out.print(d);
  104.         return this;
  105.     }

  106.     protected Printer append(float f) {
  107.         out.print(f);
  108.         return this;
  109.     }

  110.     protected Printer append(int i) {
  111.         out.print(i);
  112.         return this;
  113.     }

  114.     protected Printer append(long l) {
  115.         out.print(l);
  116.         return this;
  117.     }

  118.     protected Printer append(Object obj) {
  119.         out.print(obj);
  120.         return this;
  121.     }

  122.     protected Printer append(String s) {
  123.         out.print(s);
  124.         return this;
  125.     }

  126.     protected Printer indent() {
  127.         append(getIndentText().repeat(indentLevel));
  128.         return this;
  129.     }

  130.     protected Printer eol() {
  131.         out.println();
  132.         return this;
  133.     }

  134.     protected final Printer printScopeNodeName(Node node) {
  135.         return printScopeNodeName(node, node.getName());
  136.     }

  137.     protected Printer printScopeNodeName(Node node, String name) {
  138.         return printNodeName(node, name);
  139.     }

  140.     protected final Printer printDependencyNodeName(Node node) {
  141.         return printDependencyNodeName(node, node.getName());
  142.     }

  143.     protected Printer printDependencyNodeName(Node node, String name) {
  144.         return printNodeName(node, name);
  145.     }

  146.     protected Printer printNodeName(Node node, String name) {
  147.         return append(name);
  148.     }
  149.    
  150.     protected void raiseIndent() {
  151.         indentLevel++;
  152.     }

  153.     protected void lowerIndent() {
  154.         indentLevel--;
  155.     }

  156.     protected boolean shouldShowPackageNode(PackageNode node) {
  157.         return shouldShowNode(node) || hasVisibleClasses(node);
  158.     }

  159.     protected boolean hasVisibleClasses(PackageNode node) {
  160.         return node.getClasses().parallelStream().anyMatch(this::shouldShowClassNode);
  161.     }

  162.     protected boolean shouldShowClassNode(ClassNode node) {
  163.         return shouldShowNode(node) || hasVisibleFeatures(node);
  164.     }

  165.     protected boolean hasVisibleFeatures(ClassNode node) {
  166.         return node.getFeatures().parallelStream().anyMatch(this::shouldShowFeatureNode);
  167.     }

  168.     protected boolean shouldShowFeatureNode(FeatureNode node) {
  169.         return shouldShowNode(node);
  170.     }
  171.    
  172.     protected boolean shouldShowNode(Node node) {
  173.         return isShowEmptyNodes() || (isShowOutbounds() && !node.getOutboundDependencies().isEmpty()) || (isShowInbounds() && !node.getInboundDependencies().isEmpty());
  174.     }
  175. }