001/*
002// This software is subject to the terms of the Eclipse Public License v1.0
003// Agreement, available at the following URL:
004// http://www.eclipse.org/legal/epl-v10.html.
005// You must accept the terms of that agreement to use this software.
006//
007// Copyright (C) 2006-2011 Pentaho
008// All Rights Reserved.
009*/
010package mondrian.calc.impl;
011
012import mondrian.calc.*;
013import mondrian.olap.*;
014import mondrian.olap.fun.FunUtil;
015import mondrian.olap.type.*;
016import mondrian.olap.type.DimensionType;
017import mondrian.olap.type.LevelType;
018
019import java.util.Map;
020
021/**
022 * Calculator which always returns the same value.
023 *
024 * @author jhyde
025 * @since Sep 27, 2005
026 */
027public class ConstantCalc extends GenericCalc {
028    private final Object o;
029    private final int i;
030    private final double d;
031
032    public ConstantCalc(Type type, Object o) {
033        super(new DummyExp(type));
034        this.o = o;
035        this.i = initializeInteger(o);
036        this.d = initializeDouble(o);
037    }
038
039    @Override
040    protected String getName() {
041        return "Literal";
042    }
043
044    public ResultStyle getResultStyle() {
045        return o == null
046            ? ResultStyle.VALUE
047            : ResultStyle.VALUE_NOT_NULL;
048    }
049
050    private double initializeDouble(Object o) {
051        double value;
052        if (o instanceof Number) {
053            value = ((Number) o).doubleValue();
054        } else {
055            if (o == null) {
056                value = FunUtil.DoubleNull;
057            } else {
058                value = 0;
059            }
060        }
061        return value;
062    }
063
064    private int initializeInteger(Object o) {
065        int value;
066        if (o instanceof Number) {
067            value = ((Number) o).intValue();
068        } else {
069            if (o == null) {
070                value = FunUtil.IntegerNull;
071            } else {
072                value = 0;
073            }
074        }
075        return value;
076    }
077
078    @Override
079    public void collectArguments(Map<String, Object> arguments) {
080        super.collectArguments(arguments);
081        arguments.put("value", o);
082    }
083
084    public Object evaluate(Evaluator evaluator) {
085        return o;
086    }
087
088    public int evaluateInteger(Evaluator evaluator) {
089        return i;
090    }
091
092    public double evaluateDouble(Evaluator evaluator) {
093        return d;
094    }
095
096    public boolean dependsOn(Hierarchy hierarchy) {
097        // A constant -- including a catalog element -- will evaluate to the
098        // same result regardless of the evaluation context. For example, the
099        // member [Gender].[M] does not 'depend on' the [Gender] dimension.
100        return false;
101    }
102
103    public Calc[] getCalcs() {
104        return new Calc[0];
105    }
106
107    /**
108     * Creates an expression which evaluates to a given integer.
109     *
110     * @param i Integer value
111     * @return Constant integer expression
112     */
113    public static ConstantCalc constantInteger(int i) {
114        return new ConstantCalc(new DecimalType(Integer.MAX_VALUE, 0), i);
115    }
116
117    /**
118     * Creates an expression which evaluates to a given double.
119     *
120     * @param v Double value
121     * @return Constant double expression
122     */
123    public static DoubleCalc constantDouble(double v) {
124        return new ConstantCalc(new NumericType(), v);
125    }
126
127    /**
128     * Creates an expression which evaluates to a given string.
129     *
130     * @param s String value
131     * @return Constant string expression
132     */
133    public static StringCalc constantString(String s) {
134        return new ConstantCalc(new StringType(), s);
135    }
136
137    /**
138     * Creates an expression which evaluates to a given boolean.
139     *
140     * @param b Boolean value
141     * @return Constant boolean expression
142     */
143    public static BooleanCalc constantBoolean(boolean b) {
144        return new ConstantCalc(new BooleanType(), b);
145    }
146
147    /**
148     * Creates an expression which evaluates to null.
149     *
150     * @param type Type
151     * @return Constant null expression
152     */
153    public static ConstantCalc constantNull(Type type) {
154        return new ConstantCalc(type, null);
155    }
156
157    /**
158     * Creates an expression which evaluates to a given member.
159     *
160     * @param member Member
161     * @return Constant member expression
162     */
163    public static Calc constantMember(Member member) {
164        return new ConstantCalc(
165            MemberType.forMember(member),
166            member);
167    }
168
169    /**
170     * Creates an expression which evaluates to a given level.
171     *
172     * @param level Level
173     * @return Constant level expression
174     */
175    public static Calc constantLevel(Level level) {
176        return new ConstantCalc(
177            LevelType.forLevel(level),
178            level);
179    }
180
181    /**
182     * Creates an expression which evaluates to a given hierarchy.
183     *
184     * @param hierarchy Hierarchy
185     * @return Constant hierarchy expression
186     */
187    public static Calc constantHierarchy(Hierarchy hierarchy) {
188        return new ConstantCalc(
189            HierarchyType.forHierarchy(hierarchy),
190            hierarchy);
191    }
192
193    /**
194     * Creates an expression which evaluates to a given dimension.
195     *
196     * @param dimension Dimension
197     * @return Constant dimension expression
198     */
199    public static Calc constantDimension(Dimension dimension) {
200        return new ConstantCalc(
201            DimensionType.forDimension(dimension),
202            dimension);
203    }
204}
205
206// End ConstantCalc.java