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