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

org.apache.gobblin.cli.CliTablePrinter Maven / Gradle / Ivy

Go to download

A distributed data integration framework for streaming and batch data ecosystems.

There is a newer version: 0.17.0
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.gobblin.cli;

import java.util.ArrayList;
import java.util.List;

import com.google.common.base.Preconditions;
import com.google.common.primitives.Ints;


/**
 * A format helper for CLI output. Unfortunately it only supports strings, so
 * values need to be converted previous to passing in. This is done in order to
 * support table-like formatting.
 * 

* It's recommended that this class is built using the inner {@link Builder} class. * * @author [email protected] */ public class CliTablePrinter { /** * Labels for each columns */ private List labels; /** * A list of sprintf-style flag strings (corresponding to each column) */ private List flags; /** * Overall indentation of a table */ private int indentation; /** * Number of spaces to place between columns */ private int delimiterWidth; /** * Table of data to print */ private List> data; /** * The row format (generated by the constructor). */ private String rowFormat; public CliTablePrinter(List labels, List flags, int indentation, int delimiterWidth, List> data) { Preconditions.checkArgument(data.size() > 0); Preconditions.checkArgument(data.get(0).size() > 0); if (labels != null) { Preconditions.checkArgument(data.get(0).size() == labels.size()); } if (flags != null) { Preconditions.checkArgument(data.get(0).size() == flags.size()); } this.labels = labels; this.flags = flags; this.indentation = indentation; this.delimiterWidth = delimiterWidth; this.data = data; this.rowFormat = getRowFormat(getColumnMaxWidths()); } /** * Used to build a {@link CliTablePrinter} object. */ public static final class Builder { private List labels; private List flags; private int indentation; private int delimiterWidth; private List> data; public Builder() { // Set defaults this.delimiterWidth = 1; } public Builder labels(List labels) { this.labels = labels; return this; } public Builder data(List> data) { this.data = data; return this; } public Builder indentation(int indentation) { this.indentation = indentation; return this; } public Builder delimiterWidth(int delimiterWidth) { this.delimiterWidth = delimiterWidth; return this; } public Builder flags(List flags) { this.flags = flags; return this; } public CliTablePrinter build() { return new CliTablePrinter(this.labels, this.flags, this.indentation, this.delimiterWidth, this.data); } } /** * Prints the table of data */ public void printTable() { if (this.labels != null) { System.out.printf(this.rowFormat, this.labels.toArray()); } for (List row : this.data) { System.out.printf(this.rowFormat, row.toArray()); } } /** * A function for determining the max widths of columns, accounting for labels and data. * * @return An array of maximum widths for the strings in each column */ private List getColumnMaxWidths() { int numCols = data.get(0).size(); int[] widths = new int[numCols]; if (this.labels != null) { for (int i=0; i row : this.data) { for (int i=0;i widths) { StringBuilder rowFormat = new StringBuilder(spaces(this.indentation)); for (int i=0; i< widths.size(); i++) { rowFormat.append("%"); rowFormat.append(this.flags != null ? this.flags.get(i) : ""); rowFormat.append(widths.get(i).toString()); rowFormat.append("s"); rowFormat.append(spaces(this.delimiterWidth)); } rowFormat.append("\n"); return rowFormat.toString(); } private static String spaces(int numSpaces) { StringBuilder sb = new StringBuilder(); for (int i=0; i





© 2015 - 2025 Weber Informatics LLC | Privacy Policy