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

com.rapiddweller.common.CompositeFormatter 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;

import com.rapiddweller.common.converter.FormatHolder;
import com.rapiddweller.common.converter.ToStringConverter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

/**
 * Formats a Composite as a String.
 * Created: 14.03.2008 22:47:57
 *
 * @author Volker Bergmann
 */
public class CompositeFormatter extends FormatHolder {

  private static final String INDENT_DELTA = "    ";

  private boolean flat;
  private final boolean renderNames;

  /**
   * Instantiates a new Composite formatter.
   */
  public CompositeFormatter() {
    this(true, true);
  }

  /**
   * Instantiates a new Composite formatter.
   *
   * @param flat        the flat
   * @param renderNames the render names
   */
  public CompositeFormatter(boolean flat, boolean renderNames) {
    super("[null]");
    this.flat = flat;
    this.renderNames = renderNames;
  }

  /**
   * Is flat boolean.
   *
   * @return the boolean
   */
  public boolean isFlat() {
    return flat;
  }

  /**
   * Sets flat.
   *
   * @param flat the flat
   */
  public void setFlat(boolean flat) {
    this.flat = flat;
  }

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

  /**
   * Render string.
   *
   * @param head      the head
   * @param composite the composite
   * @param tail      the tail
   * @return the string
   */
  public String render(String head, Composite composite, String tail) {
    if (flat) {
      return renderFlat(head, composite, tail);
    } else {
      return renderHierarchical(head, composite, tail);
    }
  }

  /**
   * Render hierarchical string.
   *
   * @param head      the head
   * @param composite the composite
   * @param tail      the tail
   * @param indent    the indent
   * @return the string
   */
  public String renderHierarchical(String head, Composite composite, String tail, String indent) {
    return head + renderComponentsHierarchical(composite, indent) + tail;
  }

  // private helpers -------------------------------------------------------------------------------------------------

  private String renderFlat(String head, Composite composite, String tail) {
    return head + renderComponentsFlat(composite) + tail;
  }

  private String renderHierarchical(String head, Composite composite, String tail) {
    return renderHierarchical(head, composite, tail, "");
  }

  private String renderComponentsFlat(Composite composite) {
    StringBuilder builder = new StringBuilder();
    Map components = composite.getComponents();
    Iterator> iterator = components.entrySet().iterator();
    if (iterator.hasNext()) {
      renderComponent(builder, "", iterator.next());
    }
    while (iterator.hasNext()) {
      builder.append(", ");
      renderComponent(builder, "", iterator.next());
    }
    return builder.toString();
  }

  private String renderComponentsHierarchical(Composite composite, String indent) {
    String lineSeparator = SystemInfo.getLineSeparator();
    StringBuilder builder = new StringBuilder();
    indent += INDENT_DELTA;
    Map components = composite.getComponents();
    for (Map.Entry stringObjectEntry : components.entrySet()) {
      builder.append(lineSeparator);
      renderComponent(builder, indent, stringObjectEntry);
    }
    if (builder.length() > 1) {
      builder.append(lineSeparator);
    }
    return builder.toString();
  }

  /**
   * Render component.
   *
   * @param builder   the builder
   * @param indent    the indent
   * @param component the component
   */
  void renderComponent(StringBuilder builder, String indent, Map.Entry component) {
    builder.append(indent);
    if (renderNames) {
      builder.append(component.getKey()).append('=');
    }
    Object value = component.getValue();
    if (value == null) {
      value = nullString;
    } else if (value instanceof Date) {
      Date date = (Date) value;
      if (TimeUtil.isMidnight(date)) {
        value = new SimpleDateFormat(datePattern).format((Date) value);
      } else {
        value = new SimpleDateFormat(dateTimePattern).format((Date) value);
      }
    } else if (value instanceof Composite) {
      value = render("[", (Composite) value, "]");
    } else if (value.getClass().isArray()) {
      value = "[" + ToStringConverter.convert(value, "null") + "]";
    } else {
      value = ToStringConverter.convert(value, "null");
    }
    builder.append(value);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy