📄 mathtool.java
字号:
package org.apache.velocity.tools.generic;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import org.apache.commons.beanutils.PropertyUtils;
/**
* <p>Tool for performing math in Velocity.</p>
*
* <p>Some things should be noted here:</p>
* <ul>
* <li>This class does not have methods that take
* primitives. This is simply because Velocity
* wraps all primitives for us automagically.</li>
*
* <li>No null pointer, number format, or divide by zero
* exceptions are thrown here. This is because such exceptions
* thrown in template halt rendering. It should be sufficient
* debugging feedback that Velocity will render the reference
* literally. (e.g. $math.div(1, 0) renders as '$math.div(1, 0)')</li>
* </ul>
* <p><pre>
* Example toolbox.xml config (if you want to use this with VelocityView):
* <tool>
* <key>math</key>
* <scope>application</scope>
* <class>org.apache.velocity.tools.generic.MathTool</class>
* </tool>
* </pre></p>
*
* @author Nathan Bubna
* @author Leon Messerschmidt
* @version $Revision: 479724 $ $Date: 2006-11-27 10:49:37 -0800 (Mon, 27 Nov 2006) $
*/
public class MathTool
{
/**
* @param num1 the first number
* @param num2 the second number
* @return the sum of the numbers or
* <code>null</code> if they're invalid
* @see #toNumber
*/
public Number add(Object num1, Object num2)
{
Number n1 = toNumber(num1);
Number n2 = toNumber(num2);
if (n1 == null || n2 == null)
{
return null;
}
double value = n1.doubleValue() + n2.doubleValue();
return matchType(n1, n2, value);
}
/**
* @param num1 the first number
* @param num2 the second number
* @return the difference of the numbers or
* <code>null</code> if they're invalid
* @see #toNumber
*/
public Number sub(Object num1, Object num2)
{
Number n1 = toNumber(num1);
Number n2 = toNumber(num2);
if (n1 == null || n2 == null)
{
return null;
}
double value = n1.doubleValue() - n2.doubleValue();
return matchType(n1, n2, value);
}
/**
* @param num1 the first number
* @param num2 the second number
* @return the product of the numbers or
* <code>null</code> if they're invalid
* @see #toNumber
*/
public Number mul(Object num1, Object num2)
{
Number n1 = toNumber(num1);
Number n2 = toNumber(num2);
if (n1 == null || n2 == null)
{
return null;
}
double value = n1.doubleValue() * n2.doubleValue();
return matchType(n1, n2, value);
}
/**
* @param num1 the first number
* @param num2 the second number
* @return the quotient of the numbers or
* <code>null</code> if they're invalid
* @see #toNumber
*/
public Number div(Object num1, Object num2)
{
Number n1 = toNumber(num1);
Number n2 = toNumber(num2);
if (n1 == null || n2 == null || n2.doubleValue() == 0.0)
{
return null;
}
double value = n1.doubleValue() / n2.doubleValue();
return matchType(n1, n2, value);
}
/**
* @param num1 the first number
* @param num2 the second number
* @return the first number raised to the power of the
* second or <code>null</code> if they're invalid
* @see #toNumber
*/
public Number pow(Object num1, Object num2)
{
Number n1 = toNumber(num1);
Number n2 = toNumber(num2);
if (n1 == null || n2 == null)
{
return null;
}
double value = Math.pow(n1.doubleValue(), n2.doubleValue());
return matchType(n1, n2, value);
}
/**
* Does integer division on the int values of the specified numbers.
*
* <p>So, $math.idiv('5.1',3) will return '1',
* and $math.idiv(6,'3.9') will return '2'.</p>
*
* @param num1 the first number
* @param num2 the second number
* @return the result of performing integer division
* on the operands.
* @see #toInteger
*/
public Integer idiv(Object num1, Object num2)
{
Number n1 = toNumber(num1);
Number n2 = toNumber(num2);
if (n1 == null || n2 == null || n2.intValue() == 0)
{
return null;
}
int value = n1.intValue() / n2.intValue();
return new Integer(value);
}
/**
* Does integer modulus on the int values of the specified numbers.
*
* <p>So, $math.mod('5.1',3) will return '2',
* and $math.mod(6,'3.9') will return '0'.</p>
*
* @param num1 the first number
* @param num2 the second number
* @return the result of performing integer modulus
* on the operands.
* @see #toInteger
*/
public Integer mod(Object num1, Object num2)
{
Number n1 = toNumber(num1);
Number n2 = toNumber(num2);
if (n1 == null || n2 == null || n2.intValue() == 0)
{
return null;
}
int value = n1.intValue() % n2.intValue();
return new Integer(value);
}
/**
* @param num1 the first number
* @param num2 the second number
* @return the largest of the numbers or
* <code>null</code> if they're invalid
* @see #toNumber
*/
public Number max(Object num1, Object num2)
{
Number n1 = toNumber(num1);
Number n2 = toNumber(num2);
if (n1 == null || n2 == null)
{
return null;
}
double value = Math.max(n1.doubleValue(), n2.doubleValue());
return matchType(n1, n2, value);
}
/**
* @param num1 the first number
* @param num2 the second number
* @return the smallest of the numbers or
* <code>null</code> if they're invalid
* @see #toNumber
*/
public Number min(Object num1, Object num2)
{
Number n1 = toNumber(num1);
Number n2 = toNumber(num2);
if (n1 == null || n2 == null)
{
return null;
}
double value = Math.min(n1.doubleValue(), n2.doubleValue());
return matchType(n1, n2, value);
}
/**
* @param num the number
* @return the absolute value of the number or
* <code>null</code> if it's invalid
* @see #toDouble
*/
public Number abs(Object num)
{
Number n = toNumber(num);
if (n == null)
{
return null;
}
double value = Math.abs(n.doubleValue());
return matchType(n, value);
}
/**
* @param num the number
* @return the smallest integer that is not
* less than the given number
*/
public Integer ceil(Object num)
{
Number n = toNumber(num);
if (n == null)
{
return null;
}
return new Integer((int)Math.ceil(n.doubleValue()));
}
/**
* @param num the number
* @return the integer portion of the number
*/
public Integer floor(Object num)
{
Number n = toNumber(num);
if (n == null)
{
return null;
}
return new Integer((int)Math.floor(n.doubleValue()));
}
/**
* Rounds a number to the nearest whole Integer
*
* @param num the number to round
* @return the number rounded to the nearest whole Integer
* or <code>null</code> if it's invalid
* @see java.lang.Math#rint(double)
*/
public Integer round(Object num)
{
Number n = toNumber(num);
if (n == null)
{
return null;
}
return new Integer((int)Math.rint(n.doubleValue()));
}
/**
* Rounds a number to the specified number of decimal places.
* This is particulary useful for simple display formatting.
* If you want to round an number to the nearest integer, it
* is better to use {@link #round}, as that will return
* an {@link Integer} rather than a {@link Double}.
*
* @param decimals the number of decimal places
* @param num the number to round
* @return the value rounded to the specified number of
* decimal places or <code>null</code> if it's invalid
* @see #toNumber
*/
public Double roundTo(Object decimals, Object num)
{
Number i = toNumber(decimals);
Number d = toNumber(num);
if (i == null || d == null)
{
return null;
}
//ok, go ahead and do the rounding
int places = i.intValue();
double value = d.doubleValue();
if (places == 0)
{
value = (int)(value + .5);
}
else
{
double shift = Math.pow(10, places);
value = value * shift;
value = (int)(value + .5);
value = value / shift;
}
return new Double(value);
}
/**
* @return a pseudo-random {@link Double} greater
* than or equal to 0.0 and less than 1.0
* @see Math#random()
*/
public Double getRandom()
{
return new Double(Math.random());
}
/**
* This returns a random {@link Number} within the
* specified range. The returned value will be
* greater than or equal to the first number
* and less than the second number. If both arguments
* are whole numbers then the returned number will
* also be, otherwise a {@link Double} will
* be returned.
*
* @param num1 the first number
* @param num2 the second number
* @return a pseudo-random {@link Number} greater than
* or equal to the first number and less than
* the second
* @see Math#random()
*/
public Number random(Object num1, Object num2)
{
Number n1 = toNumber(num1);
Number n2 = toNumber(num2);
if (n1 == null || n2 == null)
{
return null;
}
double diff = n2.doubleValue() - n1.doubleValue();
// multiply the difference by a pseudo-random double from
// 0.0 to 1.0, round to the nearest int, and add the first
// value to the random int and return as an Integer
double random = (diff * Math.random()) + n1.doubleValue();
// check if either of the args were floating points
String in = n1.toString() + n2.toString();
if (in.indexOf('.') < 0)
{
// args were whole numbers, so return the same
return matchType(n1, n2, Math.floor(random));
}
// one of the args was a floating point,
// so don't floor the result
return new Double(random);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -