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.exp.PageException;
010    import railo.runtime.op.Caster;
011    import railo.runtime.op.Operator;
012    import railo.runtime.op.date.DateCaster;
013    import railo.runtime.type.dt.DateTime;
014    import railo.runtime.type.util.ListUtil;
015    
016    public final class CastableArray extends ArrayImpl {
017            
018            private final Object value;
019    
020            /**
021             * Constructor of the class
022             * generates as string list of the array
023             */
024            public CastableArray(){value=null;}
025            
026            public CastableArray(Object value){
027                    this.value=value;
028            }
029    
030            @Override
031            public synchronized Collection duplicate(boolean deepCopy) {
032                    return duplicate(new CastableArray(value),deepCopy);
033            }
034    
035    
036    
037            @Override
038            public boolean castToBooleanValue() throws PageException {
039                    return Caster.toBooleanValue(getValue());
040                    
041            }
042        
043        @Override
044        public Boolean castToBoolean(Boolean defaultValue) {
045            try {
046                            return Caster.toBoolean(getValue(),defaultValue);
047                    } catch (PageException e) {
048                            return defaultValue;
049                    }
050        }
051    
052            @Override
053            public DateTime castToDateTime() throws PageException {
054                    return Caster.toDate(getValue(),null);
055            }
056        
057        @Override
058        public DateTime castToDateTime(DateTime defaultValue) {
059            try {
060                            return DateCaster.toDateAdvanced(getValue(), true, null,defaultValue);
061                    } catch (PageException e) {
062                            return defaultValue;
063                    }
064        }
065    
066            @Override
067            public double castToDoubleValue() throws PageException {
068                    return Caster.toDoubleValue(getValue());
069            }
070        
071        @Override
072        public double castToDoubleValue(double defaultValue) {
073            try {
074                            return Caster.toDoubleValue(getValue(),defaultValue);
075                    } catch (PageException e) {
076                            return defaultValue;
077                    }
078        }
079            
080            @Override
081            public String castToString() throws PageException {
082                    return Caster.toString(getValue());
083            }
084            
085            @Override
086            public String castToString(String defaultValue) {
087                    try {
088                            return Caster.toString(getValue(),defaultValue);
089                    } catch (PageException e) {
090                            return defaultValue;
091                    }
092            }
093    
094            @Override
095            public int compareTo(boolean b) throws PageException {
096                    return Operator.compare(getValue(), b);
097            }
098    
099            @Override
100            public int compareTo(DateTime dt) throws PageException {
101                    return Operator.compare(getValue(), (Date)dt);
102            }
103    
104            @Override
105            public int compareTo(double d) throws PageException {
106                    return Operator.compare(getValue(),d);
107            }
108    
109            @Override
110            public int compareTo(String str) throws PageException {
111                    return Operator.compare(getValue(), str);
112            }
113            
114    
115            private Object getValue() throws PageException {
116                    if(value!=null)return value;
117                    return ListUtil.arrayToList(this, ",");
118            }
119    
120            @Override
121            public DumpData toDumpData(PageContext pageContext, int maxlevel,DumpProperties dp) {
122                    DumpTable dt= (DumpTable) super.toDumpData(pageContext, maxlevel, dp);
123                    dt.setTitle("Castable Array");
124                    return dt;
125            }
126    
127    }