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