View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.el;
18  
19  import java.lang.reflect.Array;
20  import java.util.Collection;
21  import java.util.Map;
22  
23  import javax.servlet.jsp.el.ELException;
24  
25  /**
26   *
27   * <p>The implementation of the empty operator
28   * 
29   * @author Nathan Abramson - Art Technology Group
30   * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: bayard $
31   **/
32  
33  public class EmptyOperator
34    extends UnaryOperator
35  {
36    //-------------------------------------
37    // Singleton
38    //-------------------------------------
39  
40    public static final EmptyOperator SINGLETON =
41      new EmptyOperator ();
42  
43    //-------------------------------------
44    /**
45     *
46     * Constructor
47     **/
48    public EmptyOperator ()
49    {
50    }
51  
52    //-------------------------------------
53    // Expression methods
54    //-------------------------------------
55    /**
56     *
57     * Returns the symbol representing the operator
58     **/
59    public String getOperatorSymbol ()
60    {
61      return "empty";
62    }
63  
64    //-------------------------------------
65    /**
66     *
67     * Applies the operator to the given value
68     **/
69    public Object apply (Object pValue)
70      throws ELException
71    {
72      // See if the value is null
73      if (pValue == null) {
74        return PrimitiveObjects.getBoolean (true);
75      }
76  
77      // See if the value is a zero-length String
78      else if ("".equals (pValue)) {
79        return PrimitiveObjects.getBoolean (true);
80      }
81  
82      // See if the value is a zero-length array
83      else if (pValue.getClass ().isArray () &&
84  	     Array.getLength (pValue) == 0) {
85        return PrimitiveObjects.getBoolean (true);
86      }
87  
88      // See if the value is an empty Map
89      else if (pValue instanceof Map &&
90  	     ((Map) pValue).isEmpty ()) {
91        return PrimitiveObjects.getBoolean (true);
92      }
93  
94      // See if the value is an empty Collection
95      else if (pValue instanceof Collection &&
96  	     ((Collection) pValue).isEmpty ()) {
97        return PrimitiveObjects.getBoolean (true);
98      }
99  
100     // Otherwise, not empty
101     else {
102       return PrimitiveObjects.getBoolean (false);
103     }
104   }
105 
106   //-------------------------------------
107 }