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.DumpTable; 009 import railo.runtime.dump.DumpTablePro; 010 import railo.runtime.dump.DumpUtil; 011 import railo.runtime.dump.SimpleDumpData; 012 import railo.runtime.exp.PageException; 013 import railo.runtime.op.Caster; 014 import railo.runtime.op.Duplicator; 015 import railo.runtime.op.Operator; 016 import railo.runtime.op.date.DateCaster; 017 import railo.runtime.type.dt.DateTime; 018 019 public final class CastableStruct extends StructImpl { 020 021 private Object value; 022 023 public CastableStruct() { 024 } 025 public CastableStruct(Object value) { 026 this.value=value; 027 } 028 public CastableStruct(Object value, int type) { 029 super(type); 030 this.value=value; 031 } 032 033 /** 034 * @return the value 035 */ 036 public Object getValue() { 037 return value; 038 } 039 040 /** 041 * @param value the value to set 042 */ 043 public void setValue(Object value) { 044 this.value = value; 045 } 046 047 /** 048 * 049 * @throws PageException 050 * @see railo.runtime.type.StructImpl#castToBooleanValue() 051 */ 052 public boolean castToBooleanValue() throws PageException { 053 if(value==null) return super.castToBooleanValue(); 054 return Caster.toBooleanValue(value); 055 056 } 057 058 /** 059 * @see railo.runtime.op.Castable#castToBoolean(java.lang.Boolean) 060 */ 061 public Boolean castToBoolean(Boolean defaultValue) { 062 if(value==null) return super.castToBoolean(defaultValue); 063 return Caster.toBoolean(value,defaultValue); 064 } 065 066 /** 067 * 068 * @throws PageException 069 * @see railo.runtime.type.StructImpl#castToDateTime() 070 */ 071 public DateTime castToDateTime() throws PageException { 072 if(value==null) return super.castToDateTime(); 073 return Caster.toDate(value, null); 074 } 075 076 /** 077 * @see railo.runtime.op.Castable#castToDateTime(railo.runtime.type.dt.DateTime) 078 */ 079 public DateTime castToDateTime(DateTime defaultValue) { 080 if(value==null) return super.castToDateTime(defaultValue); 081 return DateCaster.toDateAdvanced(value,true,null,defaultValue); 082 } 083 084 /** 085 * 086 * @throws PageException 087 * @see railo.runtime.type.StructImpl#castToDoubleValue() 088 */ 089 public double castToDoubleValue() throws PageException { 090 if(value==null) return super.castToDoubleValue(); 091 return Caster.toDoubleValue(value); 092 } 093 094 /** 095 * @see railo.runtime.op.Castable#castToDoubleValue(double) 096 */ 097 public double castToDoubleValue(double defaultValue) { 098 if(value==null) return super.castToDoubleValue(defaultValue); 099 return Caster.toDoubleValue(value,defaultValue); 100 } 101 102 /** 103 * 104 * @throws PageException 105 * @see railo.runtime.type.StructImpl#castToString() 106 */ 107 public String castToString() throws PageException { 108 if(value==null) return super.castToString(); 109 return Caster.toString(value); 110 } 111 112 /** 113 * @see railo.runtime.type.util.StructSupport#castToString(java.lang.String) 114 */ 115 public String castToString(String defaultValue) { 116 if(value==null) return super.castToString(defaultValue); 117 return Caster.toString(value,defaultValue); 118 } 119 120 /** 121 * 122 * @throws PageException 123 * @see railo.runtime.type.StructImpl#compareTo(boolean) 124 */ 125 public int compareTo(boolean b) throws PageException { 126 if(value==null) return super.compareTo(b); 127 return Operator.compare(value, b); 128 } 129 130 /** 131 * 132 * @see railo.runtime.type.StructImpl#compareTo(railo.runtime.type.dt.DateTime) 133 */ 134 public int compareTo(DateTime dt) throws PageException { 135 if(value==null) return super.compareTo(dt); 136 return Operator.compare(value, (Date)dt); 137 } 138 139 /** 140 * 141 * @see railo.runtime.type.StructImpl#compareTo(double) 142 */ 143 public int compareTo(double d) throws PageException { 144 if(value==null) return super.compareTo(d); 145 return Operator.compare(value,d); 146 } 147 148 /** 149 * 150 * @see railo.runtime.type.StructImpl#compareTo(java.lang.String) 151 */ 152 public int compareTo(String str) throws PageException { 153 if(value==null) return super.compareTo(str); 154 return Operator.compare(value, str); 155 } 156 157 /** 158 * 159 * @see railo.runtime.type.StructImpl#duplicate(boolean) 160 */ 161 public Collection duplicate(boolean deepCopy) { 162 if(value==null) return super.duplicate(deepCopy); 163 Struct sct=new CastableStruct(deepCopy?Duplicator.duplicate(value,deepCopy):value); 164 copy(this,sct,deepCopy); 165 return sct; 166 } 167 168 /** 169 * 170 * @see railo.runtime.type.StructImpl#toDumpData(railo.runtime.PageContext, int) 171 */ 172 public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) { 173 if(value==null) return super.toDumpData(pageContext, maxlevel,dp); 174 DumpTable table = new DumpTablePro("struct","#9999ff","#ccccff","#000000"); 175 table.setTitle("Value Struct"); 176 maxlevel--; 177 table.appendRow(1,new SimpleDumpData("value"),DumpUtil.toDumpData(value, pageContext,maxlevel,dp)); 178 table.appendRow(1,new SimpleDumpData("struct"),super.toDumpData(pageContext, maxlevel,dp)); 179 180 return table; 181 } 182 183 184 185 }