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;
022import java.util.Iterator;
023
024import lucee.runtime.PageContext;
025import lucee.runtime.dump.DumpData;
026import lucee.runtime.dump.DumpProperties;
027import lucee.runtime.exp.PageException;
028import lucee.runtime.op.Caster;
029import lucee.runtime.op.Duplicator;
030import lucee.runtime.op.Operator;
031import lucee.runtime.op.ThreadLocalDuplication;
032import lucee.runtime.op.date.DateCaster;
033import lucee.runtime.type.dt.DateTime;
034import lucee.runtime.type.it.EntryIterator;
035import lucee.runtime.type.ref.Reference;
036import lucee.runtime.type.util.StructSupport;
037
038public final class SVStruct extends StructSupport implements Reference,Struct {
039
040    private Collection.Key key;
041    private StructImpl parent=new StructImpl();
042
043    /**
044     * constructor of the class
045     * @param key
046     */
047    public SVStruct(Collection.Key key) {
048        this.key=key;
049    }
050    
051    @Override
052    public Collection.Key getKey() {
053        return key;
054    }
055
056    @Override
057    public String getKeyAsString() {
058        return key.getString();
059    }
060
061    @Override
062    public Object get(PageContext pc) throws PageException {
063        return get(key);
064    }
065
066    @Override
067    public Object get(PageContext pc, Object defaultValue) {
068        return get(key,defaultValue);
069    }
070
071    @Override
072    public Object set(PageContext pc, Object value) throws PageException {
073        return set(key,value);
074    }
075
076    @Override
077    public Object setEL(PageContext pc, Object value) {
078        return setEL(key,value);
079    }
080
081    @Override
082    public Object remove(PageContext pc) throws PageException {
083        return remove(key);
084    }
085    
086    @Override
087    public Object removeEL(PageContext pc) {
088        return removeEL(key);
089    }
090
091    @Override
092    public Object touch(PageContext pc) throws PageException {
093        Object o=get(key,null);
094        if(o!=null) return o;
095        return set(key,new StructImpl());
096    }
097    
098    @Override
099    public Object touchEL(PageContext pc) {
100        Object o=get(key,null);
101        if(o!=null) return o;
102        return setEL(key,new StructImpl());
103    }
104
105    public Object getParent() {
106        return parent;
107    }
108
109    @Override
110    public void clear() {
111        parent.clear();
112    }
113
114    @Override
115        public Iterator<Collection.Key> keyIterator() {
116        return parent.keyIterator();
117    }
118    
119        @Override
120        public Iterator<String> keysAsStringIterator() {
121        return parent.keysAsStringIterator();
122    }
123        
124        @Override
125        public Iterator<Entry<Key, Object>> entryIterator() {
126                return new EntryIterator(this, keys());
127        }
128        
129        @Override
130        public Iterator<Object> valueIterator() {
131                return parent.valueIterator();
132        }
133
134    @Override
135    public Collection.Key[] keys() {
136        return parent.keys();
137    }
138
139    @Override
140    public int size() {
141        return parent.size();
142    }
143
144    @Override
145        public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) {
146            return parent.toDumpData(pageContext,maxlevel,dp);
147    }
148
149    @Override
150    public boolean castToBooleanValue() throws PageException {
151        return Caster.toBooleanValue(get(key));
152    }
153    
154    @Override
155    public Boolean castToBoolean(Boolean defaultValue) {
156        Object value = get(key,defaultValue); 
157        if(value==null)return defaultValue;
158        return Caster.toBoolean(value,defaultValue);
159    }
160
161    @Override
162    public DateTime castToDateTime() throws PageException {
163        return Caster.toDate(get(key),null);
164    }
165    
166    @Override
167    public DateTime castToDateTime(DateTime defaultValue) {
168        Object value = get(key,defaultValue);
169        if(value==null)return defaultValue;
170        return DateCaster.toDateAdvanced(value, DateCaster.CONVERTING_TYPE_OFFSET, null, defaultValue); 
171    }
172
173    @Override
174    public double castToDoubleValue() throws PageException {
175        return Caster.toDoubleValue(get(key));
176    }
177    
178    @Override
179    public double castToDoubleValue(double defaultValue) {
180        Object value=get(key,null);
181        if(value==null)return defaultValue;
182        return Caster.toDoubleValue(value,true,defaultValue);
183    }
184
185    @Override
186    public String castToString() throws PageException {
187        return Caster.toString(get(key));
188    }
189    
190        @Override
191        public String castToString(String defaultValue) {
192                Object value = get(key,null);
193                if(value==null) return defaultValue;
194                
195                return Caster.toString(value,defaultValue);
196        }
197
198
199        @Override
200        public int compareTo(boolean b) throws PageException {
201                return Operator.compare(castToBooleanValue(), b);
202        }
203
204        @Override
205        public int compareTo(DateTime dt) throws PageException {
206                return Operator.compare((Date)castToDateTime(), (Date)dt);
207        }
208
209        @Override
210        public int compareTo(double d) throws PageException {
211                return Operator.compare(castToDoubleValue(), d);
212        }
213
214        @Override
215        public int compareTo(String str) throws PageException {
216                return Operator.compare(castToString(), str);
217        }
218
219    @Override
220    public Collection duplicate(boolean deepCopy) {
221        SVStruct svs = new SVStruct(key);
222        boolean inside=ThreadLocalDuplication.set(this, svs);
223        try{
224                Collection.Key[] keys = keys();
225                for(int i=0;i<keys.length;i++) {
226                    if(deepCopy)svs.setEL(keys[i],Duplicator.duplicate(get(keys[i],null),deepCopy));
227                    else svs.setEL(keys[i],get(keys[i],null));
228                }
229                return svs;
230        }
231        finally{
232                if(!inside)ThreadLocalDuplication.reset();
233        }
234    }
235
236        
237        
238    
239
240        @Override
241        public boolean containsKey(Collection.Key key) {
242                return parent.containsKey(key);
243        }
244        
245
246        @Override
247        public Object get(Collection.Key key) throws PageException {
248                return parent.get(key);
249        }
250
251        @Override
252        public Object get(Collection.Key key, Object defaultValue) {
253                return parent.get(key, defaultValue);
254        }
255
256
257        @Override
258        public Object remove(Collection.Key key) throws PageException {
259                return parent.remove(key);
260        }
261
262        @Override
263        public Object removeEL(Collection.Key key) {
264                return parent.removeEL(key);
265        }
266
267        @Override
268        public Object set(Collection.Key key, Object value) throws PageException {
269                return parent.set(key, value);
270        }
271
272        @Override
273        public Object setEL(Collection.Key key, Object value) {
274                return parent.setEL(key, value);
275        }
276
277        @Override
278        public boolean containsValue(Object value) {
279                return parent.containsValue(value);
280        }
281
282        @Override
283        public java.util.Collection values() {
284                return parent.values();
285        }
286
287}