Package org.apache.imperius.spl.parser.expressions.impl

Source Code of org.apache.imperius.spl.parser.expressions.impl.Min

/*
* 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. 
*/
//

/**
* @author Neeraj Joshi
*
*/
package org.apache.imperius.spl.parser.expressions.impl;

import java.util.Iterator;
import java.util.List;
import java.util.logging.Logger;

import org.apache.imperius.spl.core.Expression;
import org.apache.imperius.spl.core.TypeConstants;
import org.apache.imperius.spl.core.TypeInfo;
import org.apache.imperius.spl.parser.exceptions.SPLException;
import org.apache.imperius.spl.parser.expressions.MultipleArgumentExpression;
import org.apache.imperius.spl.parser.expressions.NumericExpression;
import org.apache.imperius.spl.parser.util.ExpressionUtility;
import org.apache.imperius.spl.parser.util.TypeInfoImpl;
import org.apache.imperius.spl.parser.util.TypeResolver;
import org.apache.imperius.util.Messages;
import org.apache.imperius.util.SPLLogger;


/**
*
* @author Xiping Change Log: 1. 3/9/07: Neeraj Joshi: Changed constructor to
*         take a list to facilitate reflection in the expression factory 2.
*         3/9/07: Neeraj Joshi: Changed the method in which the return type is
*         determined
*
*/
public class Min extends MultipleArgumentExpression implements
        NumericExpression
{
   
    public static final String className = Min.class.getName();
   
    private static Logger logger = SPLLogger.getSPLLogger().getLogger();
    private static final String sourceClass="Min";
   
   
   
    public Min(List exprList, boolean validateExpression)
            throws SPLException
    {
        super(exprList);
        logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "Min");

        if (validateExpression)
        {
            if (!validate())
            {
                logger.severe(Thread.currentThread().getName()+" "+"validation error: " + className
                        + " has wrong data type passed in.");
               
                throw new SPLException(Messages.getString(
              "SPL_VALIDATION_ERROR_MSG", new Object[] { className }));
            }
        }
        this._dataType.setIsArray(false);
        logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "Min");
       
    }
   
    public Object evaluate() throws SPLException
    {
        logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "evaluate");

        try
        {
            Iterator expIterator = this._expressions.iterator();
            Number min = null;
            while (expIterator.hasNext())
            {
                Expression exp = (Expression) expIterator.next();
                Number evalResult = (Number) exp.evaluate();
               
                if (min == null)
                {
                    min = evalResult;
                }
                else
                {
                    if (ExpressionUtility.compare(evalResult, min) < 0)
                    {
                        min = evalResult;
                    }
                }
            }

            logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "evaluate");
        
            return min;
           
        }
        catch (Exception e)
        {
            logger.severe(Thread.currentThread().getName()+" "+"evaluation error: " + e.toString());
           
            throw new SPLException(Messages.getString(
          "SPL_EVALUATION_ERROR_MSG", new Object[] { e
              .getLocalizedMessage() }));

        }
    }
   
    public boolean validate() throws SPLException
    {
        logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "validate");

        Iterator expIterator = this._expressions.iterator();
        TypeInfo currentDataType = new TypeInfoImpl();
        while (expIterator.hasNext())
        {
            Expression exp = (Expression) expIterator.next();
            TypeInfo type = exp.getType();
            if (!TypeResolver.isNumeric(type))
            {
                logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "validate");
               
                return false;
            }
            if (currentDataType.getType() == TypeConstants.INVALID)
            {
                currentDataType = type;
            }
            _dataType = TypeResolver.binaryNumericPromotionResolver(
                    currentDataType, type);
           
        }

        logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "validate");
    
        return true;
    }
   
    public String toString()
    {
        logger.entering(sourceClass,Thread.currentThread().getName()+" "+ "toString");
        String str = "MIN[ ";
        Iterator iter = this._expressions.iterator();
        while(iter.hasNext()) {
          str = str + ((Expression)iter.next()).toString() + (iter.hasNext()?", ":" ]");
        }
        logger.exiting(sourceClass,Thread.currentThread().getName()+" "+ "toString");
      
        return str;
    }
   
}
TOP

Related Classes of org.apache.imperius.spl.parser.expressions.impl.Min

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.