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