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
9 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================== */
18 package org
.apache
.poi
.hssf
.usermodel
;
20 import java
.lang
.reflect
.Constructor
;
21 import java
.lang
.reflect
.InvocationTargetException
;
22 import java
.lang
.reflect
.Modifier
;
23 import java
.util
.HashMap
;
26 import org
.apache
.poi
.hssf
.record
.formula
.AddPtg
;
27 import org
.apache
.poi
.hssf
.record
.formula
.ConcatPtg
;
28 import org
.apache
.poi
.hssf
.record
.formula
.DividePtg
;
29 import org
.apache
.poi
.hssf
.record
.formula
.EqualPtg
;
30 import org
.apache
.poi
.hssf
.record
.formula
.ExpPtg
;
31 import org
.apache
.poi
.hssf
.record
.formula
.FuncPtg
;
32 import org
.apache
.poi
.hssf
.record
.formula
.FuncVarPtg
;
33 import org
.apache
.poi
.hssf
.record
.formula
.GreaterEqualPtg
;
34 import org
.apache
.poi
.hssf
.record
.formula
.GreaterThanPtg
;
35 import org
.apache
.poi
.hssf
.record
.formula
.LessEqualPtg
;
36 import org
.apache
.poi
.hssf
.record
.formula
.LessThanPtg
;
37 import org
.apache
.poi
.hssf
.record
.formula
.MultiplyPtg
;
38 import org
.apache
.poi
.hssf
.record
.formula
.NotEqualPtg
;
39 import org
.apache
.poi
.hssf
.record
.formula
.OperationPtg
;
40 import org
.apache
.poi
.hssf
.record
.formula
.PercentPtg
;
41 import org
.apache
.poi
.hssf
.record
.formula
.PowerPtg
;
42 import org
.apache
.poi
.hssf
.record
.formula
.Ptg
;
43 import org
.apache
.poi
.hssf
.record
.formula
.SubtractPtg
;
44 import org
.apache
.poi
.hssf
.record
.formula
.UnaryMinusPtg
;
45 import org
.apache
.poi
.hssf
.record
.formula
.UnaryPlusPtg
;
46 import org
.apache
.poi
.hssf
.record
.formula
.eval
.AddEval
;
47 import org
.apache
.poi
.hssf
.record
.formula
.eval
.ConcatEval
;
48 import org
.apache
.poi
.hssf
.record
.formula
.eval
.DivideEval
;
49 import org
.apache
.poi
.hssf
.record
.formula
.eval
.EqualEval
;
50 import org
.apache
.poi
.hssf
.record
.formula
.eval
.FuncVarEval
;
51 import org
.apache
.poi
.hssf
.record
.formula
.eval
.GreaterEqualEval
;
52 import org
.apache
.poi
.hssf
.record
.formula
.eval
.GreaterThanEval
;
53 import org
.apache
.poi
.hssf
.record
.formula
.eval
.LessEqualEval
;
54 import org
.apache
.poi
.hssf
.record
.formula
.eval
.LessThanEval
;
55 import org
.apache
.poi
.hssf
.record
.formula
.eval
.MultiplyEval
;
56 import org
.apache
.poi
.hssf
.record
.formula
.eval
.NotEqualEval
;
57 import org
.apache
.poi
.hssf
.record
.formula
.eval
.OperationEval
;
58 import org
.apache
.poi
.hssf
.record
.formula
.eval
.PercentEval
;
59 import org
.apache
.poi
.hssf
.record
.formula
.eval
.PowerEval
;
60 import org
.apache
.poi
.hssf
.record
.formula
.eval
.SubtractEval
;
61 import org
.apache
.poi
.hssf
.record
.formula
.eval
.UnaryMinusEval
;
62 import org
.apache
.poi
.hssf
.record
.formula
.eval
.UnaryPlusEval
;
65 * This class creates <tt>OperationEval</tt> instances to help evaluate <tt>OperationPtg</tt>
70 final class OperationEvaluatorFactory
{
71 private static final Class
[] OPERATION_CONSTRUCTOR_CLASS_ARRAY
= new Class
[] { Ptg
.class };
73 private static final Map _constructorsByPtgClass
= initialiseConstructorsMap();
75 private OperationEvaluatorFactory() {
76 // no instances of this class
79 private static Map
initialiseConstructorsMap() {
80 Map m
= new HashMap(32);
81 add(m
, AddPtg
.class, AddEval
.class);
82 add(m
, ConcatPtg
.class, ConcatEval
.class);
83 add(m
, DividePtg
.class, DivideEval
.class);
84 add(m
, EqualPtg
.class, EqualEval
.class);
85 add(m
, FuncPtg
.class, FuncVarEval
.class);
86 add(m
, FuncVarPtg
.class, FuncVarEval
.class);
87 add(m
, GreaterEqualPtg
.class, GreaterEqualEval
.class);
88 add(m
, GreaterThanPtg
.class, GreaterThanEval
.class);
89 add(m
, LessEqualPtg
.class, LessEqualEval
.class);
90 add(m
, LessThanPtg
.class, LessThanEval
.class);
91 add(m
, MultiplyPtg
.class, MultiplyEval
.class);
92 add(m
, NotEqualPtg
.class, NotEqualEval
.class);
93 add(m
, PercentPtg
.class, PercentEval
.class);
94 add(m
, PowerPtg
.class, PowerEval
.class);
95 add(m
, SubtractPtg
.class, SubtractEval
.class);
96 add(m
, UnaryMinusPtg
.class, UnaryMinusEval
.class);
97 add(m
, UnaryPlusPtg
.class, UnaryPlusEval
.class);
101 private static void add(Map m
, Class ptgClass
, Class evalClass
) {
103 // perform some validation now, to keep later exception handlers simple
104 if(!Ptg
.class.isAssignableFrom(ptgClass
)) {
105 throw new IllegalArgumentException("Expected Ptg subclass");
107 if(!OperationEval
.class.isAssignableFrom(evalClass
)) {
108 throw new IllegalArgumentException("Expected OperationEval subclass");
110 if (!Modifier
.isPublic(evalClass
.getModifiers())) {
111 throw new RuntimeException("Eval class must be public");
113 if (Modifier
.isAbstract(evalClass
.getModifiers())) {
114 throw new RuntimeException("Eval class must not be abstract");
117 Constructor constructor
;
119 constructor
= evalClass
.getDeclaredConstructor(OPERATION_CONSTRUCTOR_CLASS_ARRAY
);
120 } catch (NoSuchMethodException e
) {
121 throw new RuntimeException("Missing constructor");
123 if (!Modifier
.isPublic(constructor
.getModifiers())) {
124 throw new RuntimeException("Eval constructor must be public");
126 m
.put(ptgClass
, constructor
);
130 * returns the OperationEval concrete impl instance corresponding
131 * to the supplied operationPtg
133 public static OperationEval
create(OperationPtg ptg
) {
135 throw new IllegalArgumentException("ptg must not be null");
138 Class ptgClass
= ptg
.getClass();
140 Constructor constructor
= (Constructor
) _constructorsByPtgClass
.get(ptgClass
);
141 if(constructor
== null) {
142 if(ptgClass
== ExpPtg
.class) {
143 // ExpPtg is used for array formulas and shared formulas.
144 // it is currently unsupported, and may not even get implemented here
145 throw new RuntimeException("ExpPtg currently not supported");
147 throw new RuntimeException("Unexpected operation ptg class (" + ptgClass
.getName() + ")");
151 Object
[] initargs
= { ptg
};
153 result
= constructor
.newInstance(initargs
);
154 } catch (IllegalArgumentException e
) {
155 throw new RuntimeException(e
);
156 } catch (InstantiationException e
) {
157 throw new RuntimeException(e
);
158 } catch (IllegalAccessException e
) {
159 throw new RuntimeException(e
);
160 } catch (InvocationTargetException e
) {
161 throw new RuntimeException(e
);
163 return (OperationEval
) result
;