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.ByteValue;
24  import org.apache.expreval.expr.node.DoubleValue;
25  import org.apache.expreval.expr.node.FloatValue;
26  import org.apache.expreval.expr.node.GenericValue;
27  import org.apache.expreval.expr.node.IntegerValue;
28  import org.apache.expreval.expr.node.LongValue;
29  import org.apache.expreval.expr.node.NumberValue;
30  import org.apache.expreval.expr.node.ShortValue;
31  
32  public enum NumericType {
33  
34      ByteType(ByteValue.class, Byte.class),
35      ShortType(ShortValue.class, Short.class),
36      IntegerType(IntegerValue.class, Integer.class),
37      LongType(LongValue.class, Long.class),
38      FloatType(FloatValue.class, Float.class),
39      DoubleType(DoubleValue.class, Double.class),
40      NumberType(NumberValue.class, Number.class);  // NumberType is not explicitly referenced, but it is iterated on
41  
42      final Class<? extends GenericValue> exprType;
43      final Class<? extends Number>       primaryType;
44  
45      private NumericType(final Class<? extends GenericValue> exprType, final Class<? extends Number> primaryType) {
46          this.exprType = exprType;
47          this.primaryType = primaryType;
48      }
49  
50      private Class<? extends GenericValue> getExprType() {
51          return this.exprType;
52      }
53  
54      private Class<? extends Number> getPrimaryType() {
55          return this.primaryType;
56      }
57  
58      public static int getTypeRanking(final Class clazz) {
59          for (final NumericType type : values())
60              if (clazz.equals(type.getExprType()) || clazz.equals(type.getPrimaryType()))
61                  return type.ordinal();
62          return -1;
63      }
64  
65      public static boolean isAssignable(final Class parentClass, final Class childClass) {
66          final int parentRanking = getTypeRanking(parentClass);
67          final int childRanking = getTypeRanking(childClass);
68  
69          return childRanking <= parentRanking;
70      }
71  
72      public static Class getHighestRankingNumericArg(final Object... vals) {
73  
74          Class highestRankingNumericArg = NumberValue.class;
75          int highestRank = -1;
76          for (final Object obj : vals) {
77  
78              final Class clazz = obj.getClass();
79              final int rank = NumericType.getTypeRanking(clazz);
80              if (rank > highestRank) {
81                  highestRank = rank;
82                  highestRankingNumericArg = clazz;
83              }
84          }
85          return highestRankingNumericArg;
86      }
87  
88      public static boolean useDecimalNumericArgs(final Class clazz) {
89          return isAFloat(clazz) || isADouble(clazz);
90      }
91  
92      public static boolean isANumber(final Class clazz) {
93          return getTypeRanking(clazz) != -1;
94      }
95  
96      public static boolean isAByte(final Class clazz) {
97          return clazz == ByteType.getExprType() || clazz == (ByteType.getPrimaryType());
98      }
99  
100     public static boolean isAShort(final Class clazz) {
101         return clazz == ShortType.getExprType() || clazz == ShortType.getPrimaryType();
102     }
103 
104     public static boolean isAnInteger(final Class clazz) {
105         return clazz == IntegerType.getExprType() || clazz == IntegerType.getPrimaryType();
106     }
107 
108     public static boolean isALong(final Class clazz) {
109         return clazz == LongType.getExprType() || clazz == LongType.getPrimaryType();
110     }
111 
112     public static boolean isAFloat(final Class clazz) {
113         return clazz == FloatType.getExprType() || clazz == FloatType.getPrimaryType();
114     }
115 
116     public static boolean isADouble(final Class clazz) {
117         return clazz == DoubleType.getExprType() || clazz == DoubleType.getPrimaryType();
118     }
119 }