All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rapiddweller.common.tree.TreeLogger Maven / Gradle / Ivy

Go to download

'rapiddweller Common' is an open source Java library forked from Databene Commons by Volker Bergmann. It provides extensions to the Java core library by utility classes, abstract concepts and concrete implementations.

There is a newer version: 2.0.1-jdk-11
Show newest version
/*
 * Copyright (C) 2004-2015 Volker Bergmann ([email protected]).
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.rapiddweller.common.tree;

import com.rapiddweller.common.Filter;
import com.rapiddweller.common.TreeModel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * Logs the structure represented by a {@link com.rapiddweller.common.TreeModel} implementor.
 * Created: 10.11.2010 10:21:59
 *
 * @author Volker Bergmann
 * @since 0.5.4
 */
public class TreeLogger {

  private static final Logger LOGGER = LogManager.getLogger(TreeLogger.class);

  /**
   * The Indent.
   */
  String indent = "";

  // interface ---------------------------------------------------------------

  /**
   * Log.
   *
   * @param    the type parameter
   * @param model the model
   */
  public  void log(TreeModel model) {
    log(model.getRoot(), false, model, null);
  }

  /**
   * Log.
   *
   * @param     the type parameter
   * @param model  the model
   * @param filter the filter
   */
  public  void log(TreeModel model, Filter filter) {
    log(model.getRoot(), false, model, filter);
  }

  // private helper methods --------------------------------------------------

  private  void log(T node, boolean hasSiblings, TreeModel model, Filter filter) {
    if (filter != null && !filter.accept(node)) {
      return;
    }
    LOGGER.info(indent + node);
    if (!model.isLeaf(node)) {
      increaseIndent(hasSiblings);
      int n = model.getChildCount(node);
      for (int i = 0; i < n; i++) {
        log(model.getChild(node, i), i < n - 1, model, filter);
      }
      reduceIndent();
    }
  }

  private void increaseIndent(boolean hasSuccessors) {
    if (indent.length() == 0) {
      indent = "+-";
    } else if (hasSuccessors) {
      indent = indent.substring(0, indent.length() - 2) + "| " + indent.substring(indent.length() - 2);
    } else {
      indent = indent.substring(0, indent.length() - 2) + "  " + indent.substring(indent.length() - 2);
    }
  }

  private void reduceIndent() {
    if (indent.length() >= 4) {
      indent = indent.substring(0, indent.length() - 4) + indent.substring(indent.length() - 2);
    } else {
      indent = "";
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy