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 java.lang.reflect.Array;
020    import java.util.Collection;
021    import java.util.Map;
022    
023    import javax.servlet.jsp.el.ELException;
024    
025    /**
026     *
027     * <p>The implementation of the empty operator
028     * 
029     * @author Nathan Abramson - Art Technology Group
030     * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: bayard $
031     **/
032    
033    public class EmptyOperator
034      extends UnaryOperator
035    {
036      //-------------------------------------
037      // Singleton
038      //-------------------------------------
039    
040      public static final EmptyOperator SINGLETON =
041        new EmptyOperator ();
042    
043      //-------------------------------------
044      /**
045       *
046       * Constructor
047       **/
048      public EmptyOperator ()
049      {
050      }
051    
052      //-------------------------------------
053      // Expression methods
054      //-------------------------------------
055      /**
056       *
057       * Returns the symbol representing the operator
058       **/
059      public String getOperatorSymbol ()
060      {
061        return "empty";
062      }
063    
064      //-------------------------------------
065      /**
066       *
067       * Applies the operator to the given value
068       **/
069      public Object apply (Object pValue)
070        throws ELException
071      {
072        // See if the value is null
073        if (pValue == null) {
074          return PrimitiveObjects.getBoolean (true);
075        }
076    
077        // See if the value is a zero-length String
078        else if ("".equals (pValue)) {
079          return PrimitiveObjects.getBoolean (true);
080        }
081    
082        // See if the value is a zero-length array
083        else if (pValue.getClass ().isArray () &&
084                 Array.getLength (pValue) == 0) {
085          return PrimitiveObjects.getBoolean (true);
086        }
087    
088        // See if the value is an empty Map
089        else if (pValue instanceof Map &&
090                 ((Map) pValue).isEmpty ()) {
091          return PrimitiveObjects.getBoolean (true);
092        }
093    
094        // See if the value is an empty Collection
095        else if (pValue instanceof Collection &&
096                 ((Collection) pValue).isEmpty ()) {
097          return PrimitiveObjects.getBoolean (true);
098        }
099    
100        // Otherwise, not empty
101        else {
102          return PrimitiveObjects.getBoolean (false);
103        }
104      }
105    
106      //-------------------------------------
107    }