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

org.whitesource.utils.Prints.PrintUtils Maven / Gradle / Ivy

package org.whitesource.utils.Prints;

import org.whitesource.statistics.Statistics;
import org.whitesource.utils.Constants;

import java.util.Collection;

/**
 * @author chen.luigi
 */
public class PrintUtils {

    private static final String PASSWORD = "password";

    public static String getStatisticFirstLastSection(String stepName, int numOfLongSeparator, int numOfShortSeparator, String separator) {
        StringBuilder stringBuilder = new StringBuilder();
        String longDash = getSeparator(numOfLongSeparator, separator);
        String shortDash = getSeparator(numOfShortSeparator, separator);
        // calculate the count of dash by subtract the long dash (72), short dash (20) and 2 spaces
        String completeDash = getSeparator(numOfLongSeparator - stepName.length() - numOfShortSeparator - 2, separator);
        stringBuilder.append(Constants.DOWN_LINE);
        stringBuilder.append(longDash);
        stringBuilder.append(Constants.DOWN_LINE);
        stringBuilder.append(shortDash);
        stringBuilder.append(Constants.WHITESPACE);
        stringBuilder.append(stepName);
        stringBuilder.append(Constants.WHITESPACE);
        stringBuilder.append(completeDash);
        stringBuilder.append(Constants.DOWN_LINE);
        stringBuilder.append(longDash);
        return stringBuilder.toString();
    }

    // get dash string by count number
    public static String getSeparator(int separatorCount, String separator) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < separatorCount; i++) {
            stringBuilder.append(separator);
        }
        return stringBuilder.toString();
    }

    public static String printSpaces(int numOfSpaces) {
        StringBuilder stringBuilder1 = new StringBuilder();
        for (int i = 0; i < numOfSpaces; i++) {
            stringBuilder1.append(Constants.WHITESPACE);
        }
        return stringBuilder1.toString();
    }

    public static long calculateStatisticsTime(Statistics statistics) {
        if (statistics != null && !statistics.getSubStatistics().isEmpty()) {
            long elapsed = 0;
            Collection subStatistics = statistics.getSubStatistics();
            for (Statistics subStatistic : subStatistics) {
                elapsed += subStatistic.getElapsedTime();
            }
            return elapsed;
        }
        return 0;
    }

    public static String commandArgsToString(String[] args) {
        StringBuilder result = new StringBuilder(Constants.EMPTY_STRING);
        // don't print passwords
        boolean password = false;
        for (int i = 0; i < args.length; i++) {
            String arg = args[i];
            if (arg.toLowerCase().contains(PASSWORD)) {
                password = true;
            } else if (password){
                password = false;
                arg = "*******";
            }
            result.append(arg + Constants.WHITESPACE);
        }
        // delete last whitespace
        result.deleteCharAt(result.length() - 1);
        return result.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy