at.spardat.enterprise.fmt.ABcdFmtRange Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
package at.spardat.enterprise.fmt;
import java.math.BigDecimal;
import java.util.Locale;
import at.spardat.enterprise.util.BigDecimalHelper;
/**
* Extends ABcdFmt to allow restricting the number range by providing an upper and lower limit.
*/
public class ABcdFmtRange extends ABcdFmtDefault {
// the least value this format accepts
private double minVal_;
// the largest possible value this format excepts
private double maxVal_;
/**
* Constructs the format object.
*
* @param maxBeforeC max number of digits before the comma or -1 if unrestricted. Must not be zero.
* @param maxAfterC max number of digits after the comma or -1 if unrestricted
* @param style may be DEFAULT, NO_THOUS_SEPS, THOUS_SEPS or NO_NEG. Either NO_THOUS_SEPS or THOUS_SEPS may be specified.
* @param minVal the smallest value accepted
* @param maxVal the largest value accepted
* @param l the Locale. Must not be null.
*/
public ABcdFmtRange (int maxBeforeC, int maxAfterC, int style, double minVal, double maxVal, Locale l) {
super (maxBeforeC, maxAfterC, style, l);
minVal_ = minVal;
maxVal_ = maxVal;
}
/**
* @see at.spardat.enterprise.fmt.IFmt#parse(String)
*/
public String parse (String external) throws AParseException {
String internal = super.parse(external);
// additionally check the constraints
if (internal != null && internal.length() > 0) checkLimits (internal);
return internal;
}
/**
* @see at.spardat.enterprise.fmt.IFmt#isLegalInternal(String)
*/
public boolean isLegalInternal (String internal) {
if (internal == null || internal.length() == 0) return true;
if (!super.isLegalInternal(internal)) return false;
// check the limits
try {
checkLimits (internal);
} catch (AParseException ex) {
return false;
}
return true;
}
/**
* Expects an internal string, converts it to a double and checks it for compliance
* in terms of the limits provided at construction time.
*/
private void checkLimits (String internal) {
double value;
try {
value = Double.parseDouble (internal);
} catch (NumberFormatException ex) {
// should not occur, probably a programming error;
throw new FmtParseException ("GENERIC");
}
if (value < minVal_) throw new FmtParseException ("ABcdLowerLimit", limitToString(minVal_));
if (value > maxVal_) throw new FmtParseException ("ABcdUpperLimit", limitToString(maxVal_));
}
/**
* Returns the limit, rounded to the allowed number after the comma, as String.
* This prevents problems with numbers which cannot be exactly represented in dual system like 0.01
* @author s2877
*/
private String limitToString(double limit) {
if(getMaxAfterC()>=0) {
return BigDecimalHelper.toPlainString(new BigDecimal(limit).setScale(getMaxAfterC(),BigDecimal.ROUND_HALF_UP));
} else {
return BigDecimalHelper.toPlainString(new BigDecimal(limit));
}
}
/**
* Returns the smallest value accepted.
*/
public double getMinValue () {
return minVal_;
}
/**
* Returns the largest value accepted.
*/
public double getMaxValue () {
return maxVal_;
}
}