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.cast;
020
021import lucee.runtime.PageContext;
022import lucee.runtime.exp.PageException;
023import lucee.runtime.interpreter.ref.Ref;
024import lucee.runtime.interpreter.ref.RefSupport;
025import lucee.runtime.interpreter.ref.var.Variable;
026import lucee.runtime.op.Caster;
027
028/**
029 * cast
030 */
031public final class Casting extends RefSupport implements Ref {
032    
033    private final short type;
034    private final String strType;
035    private Ref ref;
036    private Object val;
037    
038    /**
039     * constructor of the class
040     * @param pc 
041     * @param strType 
042     * @param type
043     * @param ref
044     */
045    public Casting(String strType,short type, Ref ref) {
046        this.type=type;
047        this.strType=strType;
048        this.ref=ref;
049    }
050    public Casting(String strType,short type, Object val) {
051        this.type=type;
052        this.strType=strType;
053        this.val=val;
054    }
055    
056    @Override
057    public Object getValue(PageContext pc) throws PageException {
058        // if ref == null, it is val based Casting
059        if(ref==null) return Caster.castTo(pc,type,strType,val);
060        if(ref instanceof Variable && "queryColumn".equalsIgnoreCase(strType)) {
061                Variable var=(Variable) ref;
062                return Caster.castTo(pc,type,strType,var.getCollection(pc));
063        }
064        return Caster.castTo(pc,type,strType,ref.getValue(pc));
065    }
066    
067    public Ref getRef() {
068        return ref;
069    }
070    
071    public String getStringType() {
072        return strType;
073    }
074    
075    public short getType() {
076        return type;
077    }
078
079    public String getTypeName() {
080        return "operation";
081    }
082    
083
084    public String toString() {
085        return strType+":"+ref+":"+val;
086    }
087}