view flys-artifacts/src/main/java/de/intevation/flys/utils/Formatter.java @ 2211:5400631d2f96

Fixed problems with unformatted numbers in error message. flys-artifacts/trunk@3845 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Tue, 31 Jan 2012 16:26:30 +0000
parents bda04ae1154f
children ea173e4c07c7
line wrap: on
line source
package de.intevation.flys.utils;

import java.text.NumberFormat;
import java.util.Locale;

import de.intevation.artifacts.CallContext;

import de.intevation.flys.artifacts.resources.Resources;


public final class Formatter {

    // WATERLEVEL FORMATTER CONSTANTS
    public static final int WATERLEVEL_KM_MIN_DIGITS = 3;
    public static final int WATERLEVEL_KM_MAX_DIGITS = 3;
    public static final int WATERLEVEL_W_MIN_DIGITS  = 0;
    public static final int WATERLEVEL_W_MAX_DIGITS  = 2;
    public static final int WATERLEVEL_Q_MIN_DIGITS  = 0;
    public static final int WATERLEVEL_Q_MAX_DIGITS  = 2;


    // COMPUTED DISCHARGE CURVE FORMATTER CONSTANTS
    public static final int COMPUTED_DISCHARGE_W_MIN_DIGITS  = 2;
    public static final int COMPUTED_DISCHARGE_W_MAX_DIGITS  = 2;
    public static final int COMPUTED_DISCHARGE_Q_MIN_DIGITS  = 0;
    public static final int COMPUTED_DISCHARGE_Q_MAX_DIGITS  = 2;


    // DURATION CURVE FORMATTER CONSTANTS
    public static final int DURATION_W_MIN_DIGITS = 0;
    public static final int DURATION_W_MAX_DIGITS = 2;
    public static final int DURATION_Q_MIN_DIGITS = 0;
    public static final int DURATION_Q_MAX_DIGITS = 1;
    public static final int DURATION_D_MIN_DIGITS = 0;
    public static final int DURATION_D_MAX_DIGITS = 0;


    public static NumberFormat getFormatter(CallContext c, int min, int max){
        Locale       locale = Resources.getLocale(c.getMeta());
        NumberFormat nf     = NumberFormat.getInstance(locale);

        nf.setMaximumFractionDigits(max);
        nf.setMinimumFractionDigits(min);

        return nf;
    }


    /**
     * Returns a number formatter with no max or min digits set.
     *
     * @param c The CallContext.
     *
     * @return a number formatter.
     */
    public static NumberFormat getRawFormatter(CallContext c) {
        Locale locale = Resources.getLocale(c.getMeta());
        return NumberFormat.getInstance(locale);
    }


    /**
     * Returns the number formatter for kilometer values in waterlevel exports.
     *
     * @return the number formatter for kilometer values.
     */
    public static NumberFormat getWaterlevelKM(CallContext context) {
        return getFormatter(
            context,
            WATERLEVEL_KM_MIN_DIGITS,
            WATERLEVEL_KM_MAX_DIGITS);
    }


    /**
     * Returns the number formatter for W values in waterlevel exports.
     *
     * @return the number formatter for W values.
     */
    public static NumberFormat getWaterlevelW(CallContext context) {
        return getFormatter(
            context,
            WATERLEVEL_W_MIN_DIGITS,
            WATERLEVEL_W_MAX_DIGITS);
    }


    /**
     * Returns the number formatter for Q values in waterlevel exports.
     *
     * @return the number formatter for Q values.
     */
    public static NumberFormat getWaterlevelQ(CallContext context) {
        return getFormatter(
            context,
            WATERLEVEL_Q_MIN_DIGITS,
            WATERLEVEL_Q_MAX_DIGITS);
    }


    /**
     * Returns the number formatter for W values in exports of computed
     * discharge curves.
     *
     * @return the number formatter for W values.
     */
    public static NumberFormat getComputedDischargeW(CallContext context) {
        return getFormatter(
            context,
            COMPUTED_DISCHARGE_W_MIN_DIGITS,
            COMPUTED_DISCHARGE_W_MAX_DIGITS);
    }


    /**
     * Returns the number formatter for Q values in exports of computed
     * discharge curves.
     *
     * @return the number formatter for Q values.
     */
    public static NumberFormat getComputedDischargeQ(CallContext context) {
        return getFormatter(
            context,
            COMPUTED_DISCHARGE_Q_MIN_DIGITS,
            COMPUTED_DISCHARGE_Q_MAX_DIGITS);
    }


    /**
     * Returns the number formatter for W values in duration curve exports.
     *
     * @return the number formatter for W values.
     */
    public static NumberFormat getDurationW(CallContext context) {
        return getFormatter(
            context,
            DURATION_W_MIN_DIGITS,
            DURATION_W_MAX_DIGITS);
    }


    /**
     * Returns the number formatter for Q values in duration curve exports.
     *
     * @return the number formatter for W values.
     */
    public static NumberFormat getDurationQ(CallContext context) {
        return getFormatter(
            context,
            DURATION_Q_MIN_DIGITS,
            DURATION_Q_MAX_DIGITS);
    }


    /**
     * Returns the number formatter for D values in duration curve exports.
     *
     * @return the number formatter for W values.
     */
    public static NumberFormat getDurationD(CallContext context) {
        return getFormatter(
            context,
            DURATION_D_MIN_DIGITS,
            DURATION_D_MAX_DIGITS);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org