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.ExpressionException;
028import lucee.runtime.exp.PageException;
029import lucee.runtime.op.Caster;
030import lucee.runtime.op.Operator;
031import lucee.runtime.op.date.DateCaster;
032import lucee.runtime.type.dt.DateTime;
033import lucee.runtime.type.ref.Reference;
034
035/**
036 * Simple Value Array, a Array that can't cast to a Simple Value
037 */
038public final class SVArray extends ArrayImpl implements Reference {
039    
040    private int position=1;
041
042    /**
043     * Constructor of the class
044     */
045    public SVArray() {
046        super();
047    }
048
049    /**
050     * Constructor of the class
051     * @param dimension
052     * @throws ExpressionException
053     */
054    public SVArray(int dimension) throws ExpressionException {
055        super(dimension);
056    }
057
058    
059    public SVArray(int dimension,int initalCapacity) throws ExpressionException {
060        super(dimension,initalCapacity);
061    }
062    
063    private SVArray(int dimension,int initalCapacity, int noFunctionality) {
064        super(dimension,initalCapacity,0);
065    }
066
067    /**
068     * Constructor of the class
069     * @param objects
070     */
071    public SVArray(Object[] objects) {
072        super(objects);
073    }
074
075    /**
076     * @return Returns the position.
077     */
078    public int getPosition() {
079        return position;
080    }
081
082    /**
083     * @param position The position to set.
084     */
085    public void setPosition(int position) {
086        this.position = position;
087    }
088
089    @Override
090    public Collection.Key getKey() {
091        return KeyImpl.init(Caster.toString(position));
092    }
093
094    @Override
095    public String getKeyAsString() {
096        return Caster.toString(position);
097    }
098
099    @Override
100    public Object get(PageContext pc) throws PageException {
101        return getE(position);
102    }
103
104    @Override
105    public Object get(PageContext pc, Object defaultValue) {
106        return get(position,defaultValue);
107    }
108
109    @Override
110    public Object touch(PageContext pc) throws PageException {
111        Object o=get(position,null);
112        if(o!=null) return o;
113        return setE(position,new StructImpl());
114    }
115    
116    @Override
117    public Object touchEL(PageContext pc) {
118        Object o=get(position,null);
119        if(o!=null) return o;
120        return setEL(position,new StructImpl());
121    }
122
123    @Override
124    public Object set(PageContext pc, Object value) throws PageException {
125        return setE(position,value);
126    }
127    
128    @Override
129    public Object setEL(PageContext pc, Object value) {
130        return setEL(position,value);
131    }
132
133    @Override
134    public Object remove(PageContext pc) throws PageException {
135        return removeE(position);
136    }
137    
138    @Override
139    public Object removeEL(PageContext pc) {
140        return removeEL(position);
141    }
142
143    @Override
144    public Object getParent() {
145        return this;
146    }
147
148    @Override
149    public String castToString() throws PageException {
150        return Caster.toString(getE(position));
151    }
152    
153        @Override
154        public String castToString(String defaultValue) {
155                Object value = get(position,null);
156                if(value==null) return defaultValue;
157                return Caster.toString(value,defaultValue);
158        }
159
160    @Override
161    public boolean castToBooleanValue() throws PageException {
162        return Caster.toBooleanValue(getE(position));
163    }
164    
165    @Override
166    public Boolean castToBoolean(Boolean defaultValue) {
167        Object value = get(position,defaultValue); 
168        if(value==null)return defaultValue;
169        return Caster.toBoolean(value,defaultValue);
170    }
171
172    @Override
173    public double castToDoubleValue() throws PageException {
174        return Caster.toDoubleValue(getE(position));
175    }
176    
177    @Override
178    public double castToDoubleValue(double defaultValue) {
179        Object value = get(position,null);
180        if(value==null)return defaultValue;
181        return Caster.toDoubleValue(value,true,defaultValue);
182    }
183
184    @Override
185    public DateTime castToDateTime() throws PageException {
186        return Caster.toDate(getE(position),null);
187    }
188    
189    @Override
190    public DateTime castToDateTime(DateTime defaultValue) {
191        Object value = get(position,defaultValue);
192        if(value==null)return defaultValue;
193        return DateCaster.toDateAdvanced(value, DateCaster.CONVERTING_TYPE_OFFSET, null, defaultValue); 
194    }
195
196
197        @Override
198        public int compareTo(boolean b) throws PageException {
199                return Operator.compare(castToBooleanValue(), b);
200        }
201
202        @Override
203        public int compareTo(DateTime dt) throws PageException {
204                return Operator.compare((Date)castToDateTime(), (Date)dt);
205        }
206
207        @Override
208        public int compareTo(double d) throws PageException {
209                return Operator.compare(castToDoubleValue(), d);
210        }
211
212        @Override
213        public int compareTo(String str) throws PageException {
214                return Operator.compare(castToString(), str);
215        }
216
217        @Override
218        public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) {
219                DumpTable table= (DumpTable) super.toDumpData(pageContext, maxlevel,dp);
220                table.setTitle("SV Array");
221                return table;
222        }
223
224        @Override
225        public synchronized Object clone() {
226                return duplicate(true);
227        }
228
229        @Override
230        public synchronized Collection duplicate(boolean deepCopy) {
231                SVArray sva = new SVArray(getDimension(),size(),0);
232                duplicate(sva,deepCopy);
233                sva.position=position;
234                return sva;
235        }
236}