View Javadoc

1   /*
2    * Copyright (c) 2011.  The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  
21  package org.apache.expreval.expr;
22  
23  import org.apache.expreval.expr.node.BooleanValue;
24  import org.apache.expreval.expr.node.ByteValue;
25  import org.apache.expreval.expr.node.CharValue;
26  import org.apache.expreval.expr.node.DateValue;
27  import org.apache.expreval.expr.node.GenericValue;
28  import org.apache.expreval.expr.node.NumberValue;
29  import org.apache.expreval.expr.node.ObjectValue;
30  import org.apache.expreval.expr.node.StringValue;
31  
32  import java.util.Collection;
33  
34  public class TypeSupport {
35  
36      public static boolean isACollection(final Object obj) {
37          return isParentClass(Collection.class, obj.getClass());
38      }
39  
40      public static boolean isParentClass(final Class parentClass, final Class... childrenClasses) {
41  
42          final boolean parentIsANumber = NumericType.isANumber(parentClass);
43  
44          for (final Class clazz : childrenClasses) {
45  
46              if (clazz == null)
47                  continue;
48  
49              if (parentIsANumber && NumericType.isANumber(clazz)) {
50                  if (!NumericType.isAssignable(parentClass, clazz))
51                      return false;
52              }
53              else {
54                  if (!parentClass.isAssignableFrom(clazz))
55                      return false;
56              }
57          }
58          return true;
59      }
60  
61      public static Class<? extends GenericValue> getGenericExprType(final GenericValue val) {
62  
63          final Class clazz = val.getClass();
64  
65          if (isParentClass(BooleanValue.class, clazz))
66              return BooleanValue.class;
67  
68          if (isParentClass(ByteValue.class, clazz))
69              return ByteValue.class;
70  
71          if (isParentClass(CharValue.class, clazz))
72              return CharValue.class;
73  
74          if (isParentClass(NumberValue.class, clazz))
75              return NumberValue.class;
76  
77          if (isParentClass(StringValue.class, clazz))
78              return StringValue.class;
79  
80          if (isParentClass(DateValue.class, clazz))
81              return DateValue.class;
82  
83          if (isParentClass(ObjectValue.class, clazz))
84              return ObjectValue.class;
85  
86          return null;
87      }
88  
89      public static boolean allowsNullValues(final Class clazz) {
90  
91          if (isParentClass(StringValue.class, clazz))
92              return true;
93  
94          if (isParentClass(ObjectValue.class, clazz))
95              return true;
96  
97          return false;
98      }
99  }