Java tutorial
/** * Copyright 2012 NetDigital Sweden AB * * Licensed 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. */ package com.nginious.http.xsp.expr; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; /** * A math function which returns the lowest of two value argumrnyd. Below is an example which * evaluates to 4. * * <pre> * min(4, 6) * </pre> * * @author Bojan Pisler, NetDigital Sweden AB * */ public class MinFunction extends Function { private Value value1; private Value value2; /** * Constructs a new min function which returns the smallest of the two specified * values. * * @param value1 the first value * @param value2 the second value */ public MinFunction(Value value1, Value value2) { super(); this.value1 = value1; this.value2 = value2; } /** * Returns the default type produced by this function when evaluated. The default type * depends on the argument value types to this function. If both argument values are * integers then this function returns an integer, otherwise it returns a double. * * @return the type returned by this function */ protected Type getType() { return resolveType(this.value1, this.value2); } /** * Evaluates this function which returns the lowest of the two argument values. * * @return the result of evaluating this function */ @SuppressWarnings("incomplete-switch") protected Value evaluate() { Type type = resolveType(this.value1, this.value2); Value returnValue = null; switch (type) { case DOUBLE: returnValue = new DoubleValue(Math.min(value1.getDoubleValue(), value2.getDoubleValue())); break; case INT: returnValue = new IntegerValue(Math.min(value1.getIntValue(), value2.getIntValue())); break; } return returnValue; } /** * Evaluates this function and returns the lowest value as an integer. * * @return the integer result of evaluating this function */ protected int getIntValue() { return Math.min(value1.getIntValue(), value2.getIntValue()); } /** * Evaluates this function and returns the lowest value as a double. * * @return the double result of evaluating this function */ protected double getDoubleValue() { return Math.min(value1.getDoubleValue(), value2.getDoubleValue()); } /** * Evaluates this function and returns the lowest value converted to a string. * * @return the string result of evaluating this function */ @SuppressWarnings("incomplete-switch") protected String getStringValue() { Type type = resolveType(this.value1, this.value2); String value = null; switch (type) { case DOUBLE: value = Double.toString(Math.min(value1.getDoubleValue(), value2.getDoubleValue())); break; case INT: value = Integer.toString(Math.min(value1.getIntValue(), value2.getIntValue())); break; } return value; } /** * Evaluates this function and returns the lowest value converted to a boolean. * * @return <code>true</code> if the lowest value is different from 0.0d, <code>false</code> * otherwise */ protected boolean getBooleanValue() { return getDoubleValue() != 0.0d; } /** * Creates bytecode for evaluating this function. The bytecode is generated using the * specified method visitor. The generated bytecode produces a result of the specified * type. * * @param visitor the method visitor * @param type the type */ void compile(MethodVisitor visitor, Type type) { type = resolveType(this.value1, this.value2); value1.compile(visitor, type); value2.compile(visitor, type); visitor.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/Math", "min", type == Type.DOUBLE ? "(DD)D" : "(II)I"); } }