📄 arithmeticengine.java
字号:
/*
* Copyright (c) 2003 The Visigoth Software Society. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowledgement:
* "This product includes software developed by the
* Visigoth Software Society (http://www.visigoths.org/)."
* Alternately, this acknowledgement may appear in the software itself,
* if and wherever such third-party acknowledgements normally appear.
*
* 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
* project contributors may be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact visigoths@visigoths.org.
*
* 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
* nor may "FreeMarker" or "Visigoth" appear in their names
* without prior written permission of the Visigoth Software Society.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Visigoth Software Society. For more
* information on the Visigoth Software Society, please see
* http://www.visigoths.org/
*/
package freemarker.core;
import java.math.*;
import java.util.HashMap;
import java.util.Map;
import freemarker.template.*;
import freemarker.template.utility.OptimizerUtil;
/**
* Class to perform arithmetic operations.
* @author <a href="mailto:jon@revusky.com">Jonathan Revusky</a>
* @author Attila Szegedi
*/
public abstract class ArithmeticEngine {
/**
* Arithmetic engine that converts all numbers to {@link BigDecimal} and
* then operates on them. This is FreeMarker's default arithmetic engine.
*/
public static final BigDecimalEngine BIGDECIMAL_ENGINE = new BigDecimalEngine();
/**
* Arithmetic engine that uses (more-or-less) the widening conversions of
* Java language to determine the type of result of operation, instead of
* converting everything to BigDecimal up front.
*/
public static final ConservativeEngine CONSERVATIVE_ENGINE = new ConservativeEngine();
public abstract int compareNumbers(Number first, Number second) throws TemplateException;
public abstract Number add(Number first, Number second) throws TemplateException;
public abstract Number subtract(Number first, Number second) throws TemplateException;
public abstract Number multiply(Number first, Number second) throws TemplateException;
public abstract Number divide(Number first, Number second) throws TemplateException;
public abstract Number modulus(Number first, Number second) throws TemplateException;
public abstract Number toNumber(String s);
protected int minScale = 12;
protected int maxScale = 12;
protected int roundingPolicy = BigDecimal.ROUND_HALF_UP;
/**
* Sets the minimal scale to use when dividing BigDecimal numbers. Default
* value is 12.
*/
public void setMinScale(int minScale) {
if(minScale < 0) {
throw new IllegalArgumentException("minScale < 0");
}
this.minScale = minScale;
}
/**
* Sets the maximal scale to use when multiplying BigDecimal numbers.
* Default value is 100.
*/
public void setMaxScale(int maxScale) {
if(maxScale < minScale) {
throw new IllegalArgumentException("maxScale < minScale");
}
this.maxScale = maxScale;
}
public void setRoundingPolicy(int roundingPolicy) {
if (roundingPolicy != BigDecimal.ROUND_CEILING
&& roundingPolicy != BigDecimal.ROUND_DOWN
&& roundingPolicy != BigDecimal.ROUND_FLOOR
&& roundingPolicy != BigDecimal.ROUND_HALF_DOWN
&& roundingPolicy != BigDecimal.ROUND_HALF_EVEN
&& roundingPolicy != BigDecimal.ROUND_HALF_UP
&& roundingPolicy != BigDecimal.ROUND_UNNECESSARY
&& roundingPolicy != BigDecimal.ROUND_UP)
{
throw new IllegalArgumentException("invalid rounding policy");
}
this.roundingPolicy = roundingPolicy;
}
/**
* This is the default arithmetic engine in FreeMarker. It converts every
* number it receives into {@link BigDecimal}, then operates on these
* converted {@link BigDecimal}s.
*/
public static class BigDecimalEngine
extends
ArithmeticEngine
{
public int compareNumbers(Number first, Number second) {
BigDecimal left = toBigDecimal(first);
BigDecimal right = toBigDecimal(second);
return left.compareTo(right);
}
public Number add(Number first, Number second) {
BigDecimal left = toBigDecimal(first);
BigDecimal right = toBigDecimal(second);
return left.add(right);
}
public Number subtract(Number first, Number second) {
BigDecimal left = toBigDecimal(first);
BigDecimal right = toBigDecimal(second);
return left.subtract(right);
}
public Number multiply(Number first, Number second) {
BigDecimal left = toBigDecimal(first);
BigDecimal right = toBigDecimal(second);
BigDecimal result = left.multiply(right);
if (result.scale() > maxScale) {
result = result.setScale(maxScale, roundingPolicy);
}
return result;
}
public Number divide(Number first, Number second) {
BigDecimal left = toBigDecimal(first);
BigDecimal right = toBigDecimal(second);
return divide(left, right);
}
public Number modulus(Number first, Number second) {
long left = first.longValue();
long right = second.longValue();
return new Long(left % right);
}
public Number toNumber(String s) {
return new BigDecimal(s);
}
private BigDecimal divide(BigDecimal left, BigDecimal right) {
int scale1 = left.scale();
int scale2 = right.scale();
int scale = Math.max(scale1, scale2);
scale = Math.max(minScale, scale);
return left.divide(right, scale, roundingPolicy);
}
}
/**
* An arithmetic engine that conservatively widens the operation arguments
* to extent that they can hold the result of the operation. Widening
* conversions occur in following situations:
* <ul>
* <li>byte and short are always widened to int (alike to Java language).</li>
* <li>To preserve magnitude: when operands are of different types, the
* result type is the type of the wider operand.</li>
* <li>to avoid overflows: if add, subtract, or multiply would overflow on
* integer types, the result is widened from int to long, or from long to
* BigInteger.</li>
* <li>to preserve fractional part: if a division of integer types would
* have a fractional part, int and long are converted to double, and
* BigInteger is converted to BigDecimal. An operation on a float and a
* long results in a double. An operation on a float or double and a
* BigInteger results in a BigDecimal.</li>
* </ul>
*/
public static class ConservativeEngine extends ArithmeticEngine {
private static final int INTEGER = 0;
private static final int LONG = 1;
private static final int FLOAT = 2;
private static final int DOUBLE = 3;
private static final int BIGINTEGER = 4;
private static final int BIGDECIMAL = 5;
private static final Map classCodes = createClassCodesMap();
public int compareNumbers(Number first, Number second) throws TemplateException {
switch(getCommonClassCode(first, second)) {
case INTEGER: {
int n1 = first.intValue();
int n2 = second.intValue();
return n1 < n2 ? -1 : (n1 == n2 ? 0 : 1);
}
case LONG: {
long n1 = first.longValue();
long n2 = second.longValue();
return n1 < n2 ? -1 : (n1 == n2 ? 0 : 1);
}
case FLOAT: {
float n1 = first.floatValue();
float n2 = second.floatValue();
return n1 < n2 ? -1 : (n1 == n2 ? 0 : 1);
}
case DOUBLE: {
double n1 = first.doubleValue();
double n2 = second.doubleValue();
return n1 < n2 ? -1 : (n1 == n2 ? 0 : 1);
}
case BIGINTEGER: {
BigInteger n1 = toBigInteger(first);
BigInteger n2 = toBigInteger(second);
return n1.compareTo(n2);
}
case BIGDECIMAL: {
BigDecimal n1 = toBigDecimal(first);
BigDecimal n2 = toBigDecimal(second);
return n1.compareTo(n2);
}
}
// Make the compiler happy. getCommonClassCode() is guaranteed to
// return only above codes, or throw an exception.
throw new Error();
}
public Number add(Number first, Number second) throws TemplateException {
switch(getCommonClassCode(first, second)) {
case INTEGER: {
int n1 = first.intValue();
int n2 = second.intValue();
int n = n1 + n2;
return
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -