001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.el;
018    
019    import javax.servlet.jsp.el.ELException;
020    
021    import org.apache.commons.logging.Log;
022    import org.apache.commons.logging.LogFactory;
023    
024    /**
025     *
026     * <p>The implementation of the integer divide operator
027     * 
028     * @author Nathan Abramson - Art Technology Group
029     * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: bayard $
030     **/
031    
032    public class IntegerDivideOperator
033      extends BinaryOperator
034    {
035        //-------------------------------------
036        // Constants
037        //-------------------------------------
038        private static Log log = LogFactory.getLog(IntegerDivideOperator.class);
039        
040      //-------------------------------------
041      // Singleton
042      //-------------------------------------
043    
044      public static final IntegerDivideOperator SINGLETON =
045        new IntegerDivideOperator ();
046    
047      //-------------------------------------
048      /**
049       *
050       * Constructor
051       **/
052      public IntegerDivideOperator ()
053      {
054      }
055    
056      //-------------------------------------
057      // Expression methods
058      //-------------------------------------
059      /**
060       *
061       * Returns the symbol representing the operator
062       **/
063      public String getOperatorSymbol ()
064      {
065        return "idiv";
066      }
067    
068      //-------------------------------------
069      /**
070       *
071       * Applies the operator to the given value
072       **/
073      public Object apply (Object pLeft, Object pRight)
074        throws ELException
075      {
076        if (pLeft == null &&
077            pRight == null) {
078            if (log.isWarnEnabled()) {
079                log.warn(
080                    MessageUtil.getMessageWithArgs(
081                        Constants.ARITH_OP_NULL, 
082                        getOperatorSymbol()));
083            }     
084          return PrimitiveObjects.getInteger (0);
085        }
086    
087        long left =
088          Coercions.coerceToPrimitiveNumber (pLeft, Long.class).
089          longValue ();
090        long right =
091          Coercions.coerceToPrimitiveNumber (pRight, Long.class).
092          longValue ();
093    
094        try {
095          return PrimitiveObjects.getLong (left / right);
096        }
097        catch (Exception exc) {
098            if (log.isErrorEnabled()) {
099                String message = MessageUtil.getMessageWithArgs(
100                    Constants.ARITH_ERROR,
101                    getOperatorSymbol(),
102                    "" + left,
103                    "" + right);
104                log.error(message);
105            }    
106          return PrimitiveObjects.getInteger (0);
107        }
108      }
109    
110      //-------------------------------------
111    }