1 13 14 package mondrian.olap; 15 import mondrian.olap.type.Type; 16 import mondrian.olap.type.NumericType; 17 import mondrian.olap.type.StringType; 18 import mondrian.olap.type.MemberType; 19 import mondrian.mdx.MemberExpr; 20 import mondrian.calc.*; 21 import mondrian.calc.impl.GenericCalc; 22 23 30 public class ParameterImpl 31 implements Parameter, ParameterCompilable { 32 33 private final String name; 34 private String description; 35 private Exp defaultExp; 36 private Type type; 37 private ParameterSlot slot = new ParameterSlot() { 38 Object value; 39 public Object getCachedDefaultValue() { 40 throw new UnsupportedOperationException (); 41 } 42 43 public Calc getDefaultValueCalc() { 44 throw new UnsupportedOperationException (); 45 } 46 47 public int getIndex() { 48 throw new UnsupportedOperationException (); 49 } 50 51 public Parameter getParameter() { 52 return ParameterImpl.this; 53 } 54 55 public Object getParameterValue() { 56 return value; 57 } 58 59 public void setCachedDefaultValue(Object value) { 60 throw new UnsupportedOperationException (); 61 } 62 63 public void setParameterValue(Object value) { 64 this.value = value; 65 } 66 67 }; 68 69 public ParameterImpl( 70 String name, 71 Exp defaultExp, 72 String description, 73 Type type) 74 { 75 this.name = name; 76 this.defaultExp = defaultExp; 77 this.description = description; 78 this.type = type; 79 assert defaultExp != null; 80 assert type instanceof StringType || 81 type instanceof NumericType || 82 type instanceof MemberType; 83 } 84 85 public Scope getScope() { 86 return Scope.Statement; 87 } 88 89 public Type getType() { 90 return type; 91 } 92 93 public Exp getDefaultExp() { 94 return defaultExp; 95 } 96 97 public String getName() { 98 return name; 99 } 100 101 public Object getValue() { 102 if (slot == null) { 103 return null; 106 } else { 107 return slot.getParameterValue(); 108 } 109 } 110 111 public void setValue(Object value) { 112 if (value instanceof MemberExpr) { 113 slot.setParameterValue(((MemberExpr) value).getMember()); 114 } else if (value instanceof Literal) { 115 slot.setParameterValue(((Literal) value).getValue()); 116 } else { 117 slot.setParameterValue(value); 118 } 119 } 120 121 public String getDescription() { 122 return description; 123 } 124 125 public Object [] getChildren() { 128 return null; 129 } 130 131 135 public boolean equals(Object other) { 136 if (!(other instanceof ParameterImpl)) { 137 return false; 138 } 139 ParameterImpl that = (ParameterImpl) other; 140 return that.getName().equals(this.getName()) && 141 that.defaultExp.equals(this.defaultExp); 142 } 143 144 147 public boolean isModifiable() { 148 return true; 149 } 150 151 public void setDescription(String description) { 152 this.description = description; 153 } 154 155 public void setType(Type type) { 156 assert type instanceof StringType || 157 type instanceof NumericType || 158 type instanceof MemberType; 159 this.type = type; 160 } 161 162 public void setDefaultExp(Exp defaultExp) { 163 assert defaultExp != null; 164 this.defaultExp = defaultExp; 165 } 166 167 public Calc compile(ExpCompiler compiler) { 168 final ParameterSlot slot = compiler.registerParameter(this); 169 if (this.slot != null) { 170 slot.setParameterValue(this.slot.getParameterValue()); 172 } 173 this.slot = slot; 174 return new ParameterCalc(slot); 175 } 176 177 181 private static class ParameterCalc 182 extends GenericCalc { 183 private final ParameterSlot slot; 184 185 public ParameterCalc(ParameterSlot slot) { 186 super(new DummyExp(slot.getParameter().getType())); 187 this.slot = slot; 188 } 189 190 public Calc[] getCalcs() { 191 return new Calc[0]; 192 } 193 194 public Object evaluate(Evaluator evaluator) { 195 Object value = evaluator.getParameterValue(slot); 196 if (slot.getParameterValue() == null) { 197 slot.setParameterValue(value); 199 } 200 return value; 201 } 202 } 203 } 204 205 207 | Popular Tags |