001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.transformer.bytecode.literal;
020
021import lucee.commons.color.ConstantsDouble;
022import lucee.runtime.op.Caster;
023import lucee.transformer.bytecode.BytecodeContext;
024import lucee.transformer.bytecode.Literal;
025import lucee.transformer.bytecode.Position;
026import lucee.transformer.bytecode.expression.ExprDouble;
027import lucee.transformer.bytecode.expression.ExpressionBase;
028import lucee.transformer.bytecode.util.Methods;
029import lucee.transformer.bytecode.util.Types;
030
031import org.objectweb.asm.Type;
032import org.objectweb.asm.commons.GeneratorAdapter;
033
034/**
035 * Literal Double Value
036 */
037public final class LitDouble extends ExpressionBase implements Literal,ExprDouble {
038
039        private static final Type CONSTANTS_DOUBLE = Type.getType(ConstantsDouble.class);
040        public static final LitDouble ZERO=new LitDouble(0,null,null);
041        public static final LitDouble ONE=new LitDouble(1,null,null);
042        public static final LitDouble MINUS_ONE=new LitDouble(-1,null,null);
043        
044    private double d;
045
046        public static LitDouble toExprDouble(double d) {
047                return new LitDouble(d,null,null);
048        }
049        public static LitDouble toExprDouble(double d, Position start,Position end) {
050                return new LitDouble(d,start,end);
051        }
052    
053    /**
054     * constructor of the class
055     * @param d
056     * @param line 
057     */
058        private LitDouble(double d, Position start,Position end) {
059        super(start,end);
060        
061        this.d=d;
062    }
063
064        /**
065     * @return return value as double value
066     */ 
067    public double getDoubleValue() {
068        return d;
069    }
070    
071    /**
072     * @return return value as Double Object
073     */
074    public Double getDouble() {
075        return new Double(d);
076    }
077    
078    /**
079     * @see lucee.transformer.bytecode.Literal#getString()
080     */
081    public String getString() {
082        return Caster.toString(d);
083    }
084    
085    /**
086     * @return return value as a Boolean Object
087     */
088    public Boolean getBoolean() {
089        return Caster.toBoolean(d);
090    }
091    
092    /**
093     * @return return value as a boolean value
094     */
095    public boolean getBooleanValue() {
096        return Caster.toBooleanValue(d);
097    }
098
099    /**
100     * @see lucee.transformer.bytecode.expression.Expression#_writeOut(org.objectweb.asm.commons.GeneratorAdapter, int)
101     */
102    public Type _writeOut(BytecodeContext bc, int mode) {
103        GeneratorAdapter adapter = bc.getAdapter();
104        if(mode==MODE_REF) {
105                String str=ConstantsDouble.getFieldName(d);
106                if(str!=null) {
107                                bc.getAdapter().getStatic(CONSTANTS_DOUBLE, str, Types.DOUBLE);
108                        }
109                        else {
110                                adapter.push(d);
111                        adapter.invokeStatic(Types.CASTER,Methods.METHOD_TO_DOUBLE_FROM_DOUBLE);
112                        }
113            return Types.DOUBLE;
114        }
115        adapter.push(d);
116        
117        return Types.DOUBLE_VALUE;
118    }
119
120    /**
121     * @see lucee.transformer.bytecode.Literal#getDouble(java.lang.Double)
122     */
123    public Double getDouble(Double defaultValue) {
124        return getDouble();
125    }
126
127    /**
128     * @see lucee.transformer.bytecode.Literal#getBoolean(java.lang.Boolean)
129     */
130    public Boolean getBoolean(Boolean defaultValue) {
131        return getBoolean();
132    }
133}