/*
This file is part of the software framework JStatCom,
a library for building applications for data based analysis.
Copyright (C) 2000-2005 Markus Kraetzig
This library is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation; either version 2.1 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA
*/
package com.jstatcom.component;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
/**
* The NumberFormatTypes class contains formatters that are used by JStatCom components
* to guarantee a consistent formatting behaviour. This class can be extended to provide
* special number formatters in certain contexts. It contains an enumeration of
* common formatters.
*
* The defined operators override getInstance to initialize an instance
* of the respective number format with a certain behaviour.
*
* The class implements the typesave enum pattern (Joshua Bloch, "Effective Java", Item 21).
* If you compare NumberFormatTypes, always use the == operator. This is
* save, because they are Singletons (Gamma et al., 1995).
*
* @author Markus Kraetzig
*/
public abstract class NumberFormatTypes {
/**
* The name of the NumberFormatTypes.
*/
private final String name;
/**
* The default NumberFormatType to be used for representing doubles.
* It displays numbers with Locale.ENGLISH, no grouping and a given precision,
* for example 2.345, 1.000.
*/
public static final NumberFormatTypes DEFAULT =
new NumberFormatTypes("DEFAULT") {
public NumberFormat getInstance() {
NumberFormat format = NumberFormat.getInstance(Locale.ENGLISH);
format.setMaximumFractionDigits(0);
format.setMinimumFractionDigits(0);
format.setGroupingUsed(false);
return format;
}
};
/**
* The scientific notation NumberFormatType. The applied pattern is
* "0.###E0. It displays numbers with Locale.ENGLISH,
* no grouping and a given precision, for example 1.234E-2, 1.000E3.
*/
public static final NumberFormatTypes SCIENTIFIC =
new NumberFormatTypes("SCIENTIFIC") {
public NumberFormat getInstance() {
NumberFormat format = NumberFormat.getInstance(Locale.ENGLISH);
((DecimalFormat) format).applyPattern("0.###E0");
format.setGroupingUsed(false);
return format;
}
};
/**
* NumberFormatTypes constructor with a descriptive name.
*/
protected NumberFormatTypes(String name) {
super();
this.name = name;
}
/**
* Override prevention method to enforce
* a.equals(b) if and only if a==b.
*
* @param o object to compare with
* @return true if equal, false otherwise
*/
public final boolean equals(Object o) {
return super.equals(o);
}
/**
* Returns an instance of the respective NumberFormat.
*
* @return a number formatter
*/
public abstract NumberFormat getInstance();
/**
* Gets the name of this type.
*
* @return a descriptive name
*/
public final String getName() {
return name;
}
/**
* Override prevention method invokes super method.
*
* @return hash code
*/
public final int hashCode() {
return super.hashCode();
}
/**
* Gets a string representing this type, do not use for comparisons.
*
* @return string representation
*/
public final String toString() {
return name;
}
}
Ads help maintain this website.