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.runtime.interpreter.ref.literal;
020
021import lucee.runtime.PageContext;
022import lucee.runtime.exp.PageException;
023import lucee.runtime.interpreter.ref.Ref;
024import lucee.runtime.interpreter.ref.util.RefUtil;
025import lucee.runtime.op.Caster;
026
027/**
028 * Literal Number
029 */
030public final class LNumber implements Literal {
031
032    public static final LNumber ZERO = new LNumber(new Double(0));
033    public static final LNumber ONE = new LNumber(new Double(1));
034    
035    
036    
037        private Object literal;
038
039    /**
040     * constructor of the class
041     * @param literal
042     */
043    public LNumber(Double literal) {
044        this.literal=literal;
045    }
046
047    /**
048     * constructor of the class
049     * @param literal
050     * @throws PageException 
051     */
052    public LNumber(String literal) throws PageException {
053        this.literal=Caster.toDouble(literal);
054        // in theory this filter (>10) makes not really sense, just better for performance!!!
055        if(literal.length()>10) {
056                if(!Caster.toString(this.literal).equals(literal))
057                        this.literal=literal;
058        }
059    }
060    
061    @Override
062        public Object getValue(PageContext pc) {
063        return literal;
064    }
065    
066    @Override
067        public Object getCollection(PageContext pc) {
068        return getValue(pc);
069    }
070
071    @Override
072    public String getTypeName() {
073        return "number";
074    }
075    
076    @Override
077    public Object touchValue(PageContext pc) {
078        return getValue(pc);
079    }
080
081    @Override
082    public String getString(PageContext pc) {
083        return toString();
084    }
085
086    @Override
087    public String toString() {
088        return literal instanceof String?(String)literal:Caster.toString((Double)literal);
089    }
090
091    @Override
092        public boolean eeq(PageContext pc,Ref other) throws PageException {
093        if(other instanceof LNumber){
094                        if(literal instanceof Double) {
095                                // Double|Double
096                                if(((LNumber)other).literal instanceof Double) {
097                                        return ((Double)literal).doubleValue()==((Double)((LNumber)other).literal).doubleValue();
098                                }
099                                // Double|String
100                                return Caster.toString(((Double)literal).doubleValue()).equals(((LNumber)other).literal);
101                        }
102                        // String|Double
103                        if(((LNumber)other).literal instanceof Double) {
104                                return ((String)literal).equals(Caster.toString(((Double)((LNumber)other).literal).doubleValue()));
105                        }
106                        // String|String
107                        return ((String)literal).equals((((LNumber)other).literal));
108                        
109                }
110                return RefUtil.eeq(pc,this,other);
111        }
112}