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.type; 020 021import java.util.Date; 022 023import lucee.runtime.PageContext; 024import lucee.runtime.dump.DumpData; 025import lucee.runtime.dump.DumpProperties; 026import lucee.runtime.dump.DumpUtil; 027import lucee.runtime.dump.Dumpable; 028import lucee.runtime.exp.PageException; 029import lucee.runtime.op.Caster; 030import lucee.runtime.op.Operator; 031import lucee.runtime.op.date.DateCaster; 032import lucee.runtime.type.dt.DateTime; 033import lucee.runtime.type.util.ListUtil; 034 035/** 036 * represent a named function value for a functions 037 */ 038public final class FunctionValueImpl implements FunctionValue,Dumpable { 039 040 041 private final Collection.Key name; 042 private final String[] names; 043 private final Object value; 044 045 /** 046 * @param name 047 * @param value 048 * @return 049 */ 050 public static FunctionValue newInstance(String name,Object value) { 051 return new FunctionValueImpl(name,value); 052 } 053 054 public static FunctionValue newInstance(String[] name,Object value) { 055 return new FunctionValueImpl(name,value); 056 } 057 058 public static FunctionValue newInstance(Collection.Key name,Object value) { 059 return new FunctionValueImpl(name,value); 060 } 061 062 /** 063 * constructor of the class 064 * @param name name of the value 065 * @param value value himself 066 */ 067 public FunctionValueImpl(String name,Object value) { 068 this.name=KeyImpl.init(name); 069 this.value=value; 070 names=null; 071 } 072 073 public FunctionValueImpl(Collection.Key name,Object value) { 074 this.name=name; 075 this.value=value; 076 names=null; 077 } 078 079 public FunctionValueImpl(String[] names,Object value) { 080 this.names=names; 081 this.value=value; 082 name=null; 083 } 084 085 @Override 086 public String getName() { 087 return getNameAsString(); 088 } 089 090 @Override 091 public String getNameAsString() { 092 if(name==null){ 093 return ListUtil.arrayToList(names, "."); 094 } 095 return name.getString(); 096 } 097 098 @Override 099 public Collection.Key getNameAsKey() { 100 if(name==null){ 101 return KeyImpl.init(ListUtil.arrayToList(names, ".")); 102 } 103 return name; 104 } 105 106 107 108 public String[] getNames() { 109 return names; 110 } 111 112 @Override 113 public Object getValue() { 114 return value; 115 } 116 117 @Override 118 public String castToString() throws PageException { 119 return Caster.toString(value); 120 } 121 122 @Override 123 public String castToString(String defaultValue) { 124 return Caster.toString(value,defaultValue); 125 } 126 127 @Override 128 public boolean castToBooleanValue() throws PageException { 129 return Caster.toBooleanValue(value); 130 } 131 132 @Override 133 public Boolean castToBoolean(Boolean defaultValue) { 134 return Caster.toBoolean(value,defaultValue); 135 } 136 137 @Override 138 public double castToDoubleValue() throws PageException { 139 return Caster.toDoubleValue(value); 140 } 141 142 @Override 143 public double castToDoubleValue(double defaultValue) { 144 return Caster.toDoubleValue(value,true,defaultValue); 145 } 146 147 @Override 148 public DateTime castToDateTime() throws PageException { 149 return DateCaster.toDateSimple(value,DateCaster.CONVERTING_TYPE_OFFSET,true,null); 150 } 151 152 @Override 153 public DateTime castToDateTime(DateTime defaultValue) { 154 return DateCaster.toDateSimple(value,DateCaster.CONVERTING_TYPE_OFFSET,true,null,defaultValue); 155 } 156 157 @Override 158 public int compareTo(boolean b) throws PageException { 159 return Operator.compare(value, b?1D:0D); 160 } 161 162 @Override 163 public int compareTo(DateTime dt) throws PageException { 164 return Operator.compare(value, (Date)dt); 165 } 166 167 @Override 168 public int compareTo(double d) throws PageException { 169 return Operator.compare(value, d); 170 } 171 172 @Override 173 public int compareTo(String str) throws PageException { 174 return Operator.compare(value, str); 175 } 176 177 public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties properties) { 178 return DumpUtil.toDumpData(value, pageContext, maxlevel, properties); 179 } 180 181 @Override 182 public String toString() { 183 return name+":"+value; 184 } 185 186 public static Struct toStruct(FunctionValueImpl fv1){ 187 StructImpl sct = new StructImpl(StructImpl.TYPE_LINKED); 188 sct.setEL(fv1.getNameAsKey(), fv1); 189 return sct; 190 } 191 public static Struct toStruct(FunctionValueImpl fv1,FunctionValueImpl fv2){ 192 StructImpl sct = new StructImpl(StructImpl.TYPE_LINKED); 193 sct.setEL(fv1.getNameAsKey(), fv1); 194 sct.setEL(fv2.getNameAsKey(), fv2); 195 return sct; 196 } 197 public static Struct toStruct(FunctionValueImpl fv1,FunctionValueImpl fv2,FunctionValueImpl fv3){ 198 StructImpl sct = new StructImpl(StructImpl.TYPE_LINKED); 199 sct.setEL(fv1.getNameAsKey(), fv1); 200 sct.setEL(fv2.getNameAsKey(), fv2); 201 sct.setEL(fv3.getNameAsKey(), fv3); 202 return sct; 203 } 204 public static Struct toStruct(FunctionValueImpl fv1,FunctionValueImpl fv2,FunctionValueImpl fv3,FunctionValueImpl fv4){ 205 StructImpl sct = new StructImpl(StructImpl.TYPE_LINKED); 206 sct.setEL(fv1.getNameAsKey(), fv1); 207 sct.setEL(fv2.getNameAsKey(), fv2); 208 sct.setEL(fv3.getNameAsKey(), fv3); 209 sct.setEL(fv4.getNameAsKey(), fv4); 210 return sct; 211 } 212 213}