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

org.openl.util.print.NicePrinter Maven / Gradle / Ivy

The newest version!
/*
 * Created on Apr 1, 2004
 *
 *
 * Developed by OpenRules, Inc. 2003, 2004
 *
 */
package org.openl.util.print;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author snshor
 */
public class NicePrinter {

    private static final int DEF_IDENT_STEP = 2;
    private final StringBuilder buffer;
    private int ident = 0;
    private final HashMap printedObjects = new HashMap<>();
    private int printedID = 0;

    public static String getTypeName(Object obj) {
        return obj.getClass().getName();
    }

    public static String print(Object obj) {
        StringBuilder buf = new StringBuilder(100);
        print(obj, buf);
        return buf.toString();
    }

    public static void print(Object obj, StringBuilder buf) {
        NicePrinter np = new NicePrinter(buf);
        np.print(obj, new NicePrinterAdaptor());
    }

    public NicePrinter() {
        this(new StringBuilder(100));
    }

    public NicePrinter(StringBuilder buf) {
        buffer = buf;
    }

    public void decIdent() {
        --ident;
    }

    /**
     * @return Returns the buffer.
     */
    public StringBuilder getBuffer() {
        return buffer;
    }

    public void incIdent() {
        ++ident;
    }

    @SuppressWarnings("unchecked")
    public void print(Object obj, NicePrinterAdaptor adaptor) {
        if (obj == null) {
            adaptor.printNull(this);
            return;
        }

        if (adaptor.isPrimitive(obj)) {
            adaptor.printPrimitive(obj, this);
            return;
        }

        Integer existingID = printedObjects.get(obj);
        if (existingID != null) {
            adaptor.printReference(obj, existingID, this);
            return;
        }

        int newID = printedID++;

        printedObjects.put(obj, newID);

        if (obj instanceof Enum) {
            adaptor.printPrimitive(obj, this);
            return;
        }

        if (obj instanceof Map) {
            adaptor.printMap((Map) obj, null, this);
            return;
        }

        if (obj instanceof Collection) {
            adaptor.printCollection((Collection) obj, newID, this);
            return;
        }

        if (obj.getClass().isArray()) {
            adaptor.printArray(obj, newID, this);
            return;
        }

        adaptor.printObject(obj, newID, this);

    }

    public void startNewLine() {
        buffer.append('\n');
        for (int i = 0; i < ident; ++i) {
            for (int j = 0; j < DEF_IDENT_STEP; j++) {
                buffer.append(' ');
            }
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy