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.runtime.op.Caster;
022import lucee.transformer.bytecode.BytecodeContext;
023import lucee.transformer.bytecode.Literal;
024import lucee.transformer.bytecode.Position;
025import lucee.transformer.bytecode.expression.ExprBoolean;
026import lucee.transformer.bytecode.expression.ExpressionBase;
027import lucee.transformer.bytecode.util.Types;
028
029import org.objectweb.asm.Opcodes;
030import org.objectweb.asm.Type;
031import org.objectweb.asm.commons.GeneratorAdapter;
032
033/**
034 * Literal Boolean
035 */
036public final class LitBoolean extends ExpressionBase implements Literal,ExprBoolean {
037    
038        /**
039         * @see java.lang.Object#toString()
040         */
041        @Override
042        public String toString() {
043                return b+"";
044        }
045
046        private boolean b;
047
048        public static final LitBoolean TRUE=new LitBoolean(true,null,null);
049        public static final LitBoolean FALSE=new LitBoolean(false,null,null);
050
051        public static ExprBoolean toExprBoolean(boolean b, Position start,Position end) {
052                return new LitBoolean(b,start,end);
053        }
054
055        public static ExprBoolean toExprBoolean(boolean b) {
056                return new LitBoolean(b,null,null);
057        }
058        
059    /**
060     * constructor of the class
061     * @param b 
062     * @param line 
063     */
064    public LitBoolean(boolean b, Position start,Position end) {
065        super(start,end);
066        this.b=b;
067    }
068
069    /**
070     * @return return value as double value
071     */ 
072    public double getDoubleValue() {
073        return Caster.toDoubleValue(b);
074    }
075    
076    /**
077     * @return return value as Double Object
078     */
079    public Double getDouble() {
080        return Caster.toDouble(b);
081    }
082    
083    /**
084     * @see lucee.transformer.bytecode.Literal#getString()
085     */
086    public String getString() {
087        return Caster.toString(b);
088    }
089    
090    /**
091     * @return return value as a Boolean Object
092     */
093    public Boolean getBoolean() {
094        return Caster.toBoolean(b);
095    }
096    
097    /**
098     * @return return value as a boolean value
099     */
100    public boolean getBooleanValue() {
101        return b;
102    }
103
104    /**
105     *
106     * @see lucee.transformer.bytecode.expression.ExpressionBase#_writeOut(org.objectweb.asm.commons.GeneratorAdapter, int)
107     */
108    public Type _writeOut(BytecodeContext bc, int mode) {
109        GeneratorAdapter adapter = bc.getAdapter();
110        
111        if(mode==MODE_REF) {
112                adapter.getStatic(Types.BOOLEAN, b?"TRUE":"FALSE", Types.BOOLEAN);
113                return Types.BOOLEAN;
114        }
115        adapter.visitInsn(b?Opcodes.ICONST_1:Opcodes.ICONST_0);
116        return Types.BOOLEAN_VALUE;
117    }
118
119    /**
120     * @see lucee.transformer.bytecode.Literal#getDouble(java.lang.Double)
121     */
122    public Double getDouble(Double defaultValue) {
123        return getDouble();
124    }
125
126    /**
127     * @see lucee.transformer.bytecode.Literal#getBoolean(java.lang.Boolean)
128     */
129    public Boolean getBoolean(Boolean defaultValue) {
130        return getBoolean();
131    }
132
133    /* *
134     * @see lucee.transformer.bytecode.expression.Expression#getType()
135     * /
136    public int getType() {
137        return Types._BOOLEAN;
138    }*/
139}