view artifacts/src/main/java/org/dive4elements/river/artifacts/common/AbstractResultType.java @ 9425:3f49835a00c3

Extended CrossSectionFacet so it may fetch different data from within the artifact result. Also allows to have acces to the potentially already computed artifact result via its normal computation cache.
author gernotbelger
date Fri, 17 Aug 2018 15:31:02 +0200
parents 1614cb14308f
children d32b11d585cd
line wrap: on
line source
/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
 * Software engineering by
 *  Björnsen Beratende Ingenieure GmbH
 *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */
package org.dive4elements.river.artifacts.common;

import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.dive4elements.artifacts.CallContext;
import org.dive4elements.artifacts.CallMeta;
import org.dive4elements.river.artifacts.resources.Resources;

/**
 * @author Domenico Nardi Tironi
 */
public abstract class AbstractResultType implements IResultType {

    /* Cache for formatters because Formatter will always create new formats (which is very expensive) */
    private final Map<Locale, NumberFormat> formatters = new HashMap<>();

    private final String unit;

    private final String csvHeader;

    private final String pdfHeader;

    protected AbstractResultType(final String unit, final String csvHeader, final String pdfHeader) {
        this.unit = unit;
        this.csvHeader = csvHeader;
        this.pdfHeader = pdfHeader;

    }

    @Override
    public abstract String exportValue(final CallContext context, final Object value);

    protected final String exportStringValue(final Object value) {

        if (value == null)
            return StringUtils.EMPTY;

        if (!(value instanceof String))
            throw new IllegalStateException();

        return (String) value;
    }

    @Override
    public final double asDouble(final Object value) {
        if (value == null)
            return Double.NaN;

        if (!(value instanceof Number))
            throw new IllegalStateException();

        final Number number = (Number) value;
        return number.doubleValue();
    }

    protected final String exportDoubleValue(final CallContext context, final double value) {
        if (Double.isNaN(value))
            return StringUtils.EMPTY;

        final NumberFormat formatter = getFormatter(context);
        return formatter.format(value);
    }

    private NumberFormat getFormatter(final CallContext context) {
        final CallMeta meta = context.getMeta();
        final Locale locale = Resources.getLocale(meta);

        if (!this.formatters.containsKey(locale))
            this.formatters.put(locale, createFormatter(context));

        return this.formatters.get(locale);
    }

    protected abstract NumberFormat createFormatter(CallContext context);

    protected final String exportDateValue(final CallContext context, final Date value) {
        final Locale locale = Resources.getLocale(context.getMeta());
        final DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
        return df.format(value);
    }

    @Override
    public final String getCsvHeader() {
        return this.csvHeader;
    }

    public final String getCsvHeader(final CallMeta meta) {
        return Resources.getMsg(meta, this.csvHeader, this.csvHeader);
    }

    @Override
    public final String getPdfHeader(final CallMeta meta) {
        return Resources.getMsg(meta, this.pdfHeader, this.pdfHeader);
    }

    @Override
    public final String getUnit() {
        return this.unit;
    }
}

http://dive4elements.wald.intevation.org