java/sql-dk/src/info/globalcode/sql/dk/formatting/BarChartFormatter.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 06 Sep 2015 21:48:54 +0200
branchv_0
changeset 226 b40153eb7716
parent 224 36db9fd27436
child 228 3787c999d12c
permissions -rw-r--r--
BarChartFormatter: format null as an empty/missing bar + better logging
     1 /**
     2  * SQL-DK
     3  * Copyright © 2015 František Kučera (frantovo.cz)
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License, or
     8  * (at your option) any later version.
     9  *
    10  * This program is distributed in the hope that it will be useful,
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  * GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License
    16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 package info.globalcode.sql.dk.formatting;
    19 
    20 import info.globalcode.sql.dk.Functions;
    21 import info.globalcode.sql.dk.configuration.PropertyDeclaration;
    22 import info.globalcode.sql.dk.logging.LoggerProducer;
    23 import java.math.BigDecimal;
    24 import java.math.MathContext;
    25 import java.math.RoundingMode;
    26 import java.util.List;
    27 import java.util.logging.Level;
    28 import java.util.logging.Logger;
    29 
    30 /**
    31  *
    32  * @author Ing. František Kučera (frantovo.cz)
    33  */
    34 @PropertyDeclaration(name = BarChartFormatter.PROPERTY_PRECISION, type = Integer.class, defaultValue = BarChartFormatter.PROPERTY_PRECISION_DEFAULT, description = "number of characters representing 100 % in the bar chart")
    35 public class BarChartFormatter extends TabularPrefetchingFormatter {
    36 
    37 	public static final String NAME = "barchart"; // bash-completion:formatter
    38 	public static final String PROPERTY_PRECISION = "precision";
    39 	protected static final String PROPERTY_PRECISION_DEFAULT = "100";
    40 	private static final MathContext mathContext = MathContext.DECIMAL128;
    41 	public static final Logger log = LoggerProducer.getLogger();
    42 	private final BigDecimal chartPrecision;
    43 	private final char chartFull;
    44 	private final char chartEmpty;
    45 
    46 	public BarChartFormatter(FormatterContext formatterContext) {
    47 		super(formatterContext);
    48 		chartPrecision = BigDecimal.valueOf(formatterContext.getProperties().getInteger(PROPERTY_PRECISION, Integer.parseInt(PROPERTY_PRECISION_DEFAULT)));
    49 		chartFull = isAsciiNostalgia() ? '#' : '█';
    50 		chartEmpty = isAsciiNostalgia() ? '~' : '░';
    51 		// TODO: consider using partial blocks for more precision: https://en.wikipedia.org/wiki/Block_Elements
    52 	}
    53 
    54 	@Override
    55 	protected void postprocessPrefetchedResultSet(ColumnsHeader currentHeader, List<Object[]> currentResultSet) {
    56 		super.postprocessPrefetchedResultSet(currentHeader, currentResultSet);
    57 
    58 		updateColumnWidth(currentHeader.getColumnCount(), chartPrecision.intValue());
    59 
    60 		BigDecimal maximum = BigDecimal.ZERO;
    61 		BigDecimal minimum = BigDecimal.ZERO;
    62 		int lastIndex = currentHeader.getColumnCount() - 1;
    63 
    64 		Object valueObject = null;
    65 		try {
    66 			for (Object[] row : currentResultSet) {
    67 				valueObject = row[lastIndex];
    68 				if (valueObject != null) {
    69 					BigDecimal value = new BigDecimal(valueObject.toString());
    70 					maximum = maximum.max(value);
    71 					minimum = minimum.min(value);
    72 				}
    73 			}
    74 
    75 			BigDecimal range = maximum.subtract(minimum);
    76 
    77 			for (Object[] row : currentResultSet) {
    78 				valueObject = row[lastIndex];
    79 				if (valueObject == null) {
    80 					row[lastIndex] = "";
    81 				} else {
    82 					BigDecimal value = new BigDecimal(valueObject.toString());
    83 					BigDecimal valueFromMinimum = value.subtract(minimum);
    84 
    85 					BigDecimal points = chartPrecision.divide(range, mathContext).multiply(valueFromMinimum, mathContext);
    86 					int pointsRounded = points.setScale(0, RoundingMode.HALF_UP).intValue();
    87 					row[lastIndex] = Functions.repeat(chartFull, pointsRounded) + Functions.repeat(chartEmpty, chartPrecision.intValue() - pointsRounded);
    88 				}
    89 			}
    90 
    91 		} catch (NumberFormatException e) {
    92 			// https://en.wiktionary.org/wiki/parsable
    93 			log.log(Level.SEVERE, "Last column must be number or an object with toString() value parsable to a number. But was „{0}“", valueObject);
    94 			// FIXME: throw FormatterException
    95 			throw e;
    96 		}
    97 	}
    98 
    99 }