001    package railo.runtime.type;
002    
003    import java.util.Date;
004    import java.util.Iterator;
005    
006    import railo.runtime.PageContext;
007    import railo.runtime.dump.DumpData;
008    import railo.runtime.dump.DumpProperties;
009    import railo.runtime.exp.PageException;
010    import railo.runtime.op.Caster;
011    import railo.runtime.op.Duplicator;
012    import railo.runtime.op.Operator;
013    import railo.runtime.op.ThreadLocalDuplication;
014    import railo.runtime.op.date.DateCaster;
015    import railo.runtime.type.dt.DateTime;
016    import railo.runtime.type.ref.Reference;
017    import railo.runtime.type.util.StructSupport;
018    
019    public final class SVStruct extends StructSupport implements Reference,Struct {
020    
021        private Collection.Key key;
022        private StructImpl parent=new StructImpl();
023    
024        /**
025         * constructor of the class
026         * @param key
027         */
028        public SVStruct(Collection.Key key) {
029            this.key=key;
030        }
031        
032        /**
033         *
034         * @see railo.runtime.type.ref.Reference#getKey()
035         */
036        public Collection.Key getKey() {
037            return key;
038        }
039    
040        /**
041         *
042         * @see railo.runtime.type.ref.Reference#getKeyAsString()
043         */
044        public String getKeyAsString() {
045            return key.getString();
046        }
047    
048        /**
049         * @see railo.runtime.type.ref.Reference#get(railo.runtime.PageContext)
050         */
051        public Object get(PageContext pc) throws PageException {
052            return get(key);
053        }
054    
055        /**
056         *
057         * @see railo.runtime.type.ref.Reference#get(railo.runtime.PageContext, java.lang.Object)
058         */
059        public Object get(PageContext pc, Object defaultValue) {
060            return get(key,defaultValue);
061        }
062    
063        /**
064         * @see railo.runtime.type.ref.Reference#set(railo.runtime.PageContext, java.lang.Object)
065         */
066        public Object set(PageContext pc, Object value) throws PageException {
067            return set(key,value);
068        }
069    
070        /**
071         * @see railo.runtime.type.ref.Reference#setEL(railo.runtime.PageContext, java.lang.Object)
072         */
073        public Object setEL(PageContext pc, Object value) {
074            return setEL(key,value);
075        }
076    
077        /**
078         * @see railo.runtime.type.ref.Reference#remove(railo.runtime.PageContext)
079         */
080        public Object remove(PageContext pc) throws PageException {
081            return remove(key);
082        }
083        
084        /**
085         * @see railo.runtime.type.ref.Reference#removeEL(railo.runtime.PageContext)
086         */
087        public Object removeEL(PageContext pc) {
088            return removeEL(key);
089        }
090    
091        /**
092         * @see railo.runtime.type.ref.Reference#touch(railo.runtime.PageContext)
093         */
094        public Object touch(PageContext pc) throws PageException {
095            Object o=get(key,null);
096            if(o!=null) return o;
097            return set(key,new StructImpl());
098        }
099        
100        /**
101         * @see railo.runtime.type.ref.Reference#touchEL(railo.runtime.PageContext)
102         */
103        public Object touchEL(PageContext pc) {
104            Object o=get(key,null);
105            if(o!=null) return o;
106            return setEL(key,new StructImpl());
107        }
108    
109        public Object getParent() {
110            return parent;
111        }
112    
113        /**
114         * @see railo.runtime.type.Collection#clear()
115         */
116        public void clear() {
117            parent.clear();
118        }
119    
120    
121        /**
122         * @see railo.runtime.type.Collection#keyIterator()
123         */
124        public Iterator keyIterator() {
125            return parent.keyIterator();
126        }
127    
128        /**
129         *
130         * @see railo.runtime.type.Collection#keys()
131         */
132        public Collection.Key[] keys() {
133            return parent.keys();
134        }
135    
136        /**
137         *
138         * @see railo.runtime.type.Collection#keysAsString()
139         */
140        public String[] keysAsString() {
141            return parent.keysAsString();
142        }
143    
144        /**
145         * @see railo.runtime.type.Collection#remove(java.lang.String)
146         */
147        public Object remove(String key) throws PageException {
148            return parent.remove(key);
149        }
150    
151    
152        /**
153         * @see railo.runtime.type.Collection#size()
154         */
155        public int size() {
156            return parent.size();
157        }
158    
159        /**
160             * @see railo.runtime.dump.Dumpable#toDumpData(railo.runtime.PageContext, int)
161             */
162            public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties dp) {
163                return parent.toDumpData(pageContext,maxlevel,dp);
164        }
165    
166        /**
167         * @see railo.runtime.op.Castable#castToBooleanValue()
168         */
169        public boolean castToBooleanValue() throws PageException {
170            return Caster.toBooleanValue(get(key));
171        }
172        
173        /**
174         * @see railo.runtime.op.Castable#castToBoolean(java.lang.Boolean)
175         */
176        public Boolean castToBoolean(Boolean defaultValue) {
177            Object value = get(key,defaultValue); 
178            if(value==null)return defaultValue;
179            return Caster.toBoolean(value,defaultValue);
180        }
181    
182        /**
183         * @see railo.runtime.op.Castable#castToDateTime()
184         */
185        public DateTime castToDateTime() throws PageException {
186            return Caster.toDate(get(key),null);
187        }
188        
189        /**
190         * @see railo.runtime.op.Castable#castToDateTime(railo.runtime.type.dt.DateTime)
191         */
192        public DateTime castToDateTime(DateTime defaultValue) {
193            Object value = get(key,defaultValue);
194            if(value==null)return defaultValue;
195            return DateCaster.toDateAdvanced(value, true, null, defaultValue); 
196        }
197    
198        /**
199         * @see railo.runtime.op.Castable#castToDoubleValue()
200         */
201        public double castToDoubleValue() throws PageException {
202            return Caster.toDoubleValue(get(key));
203        }
204        
205        /**
206         * @see railo.runtime.op.Castable#castToDoubleValue(double)
207         */
208        public double castToDoubleValue(double defaultValue) {
209            Object value=get(key,null);
210            if(value==null)return defaultValue;
211            return Caster.toDoubleValue(value,defaultValue);
212        }
213    
214        /**
215         * @see railo.runtime.op.Castable#castToString()
216         */
217        public String castToString() throws PageException {
218            return Caster.toString(get(key));
219        }
220        
221            /**
222             * @see railo.runtime.type.util.StructSupport#castToString(java.lang.String)
223             */
224            public String castToString(String defaultValue) {
225                    Object value = get(key,null);
226                    if(value==null) return defaultValue;
227                    
228                    return Caster.toString(value,defaultValue);
229            }
230    
231    
232            /**
233             * @see railo.runtime.op.Castable#compare(boolean)
234             */
235            public int compareTo(boolean b) throws PageException {
236                    return Operator.compare(castToBooleanValue(), b);
237            }
238    
239            /**
240             * @see railo.runtime.op.Castable#compareTo(railo.runtime.type.dt.DateTime)
241             */
242            public int compareTo(DateTime dt) throws PageException {
243                    return Operator.compare((Date)castToDateTime(), (Date)dt);
244            }
245    
246            /**
247             * @see railo.runtime.op.Castable#compareTo(double)
248             */
249            public int compareTo(double d) throws PageException {
250                    return Operator.compare(castToDoubleValue(), d);
251            }
252    
253            /**
254             * @see railo.runtime.op.Castable#compareTo(java.lang.String)
255             */
256            public int compareTo(String str) throws PageException {
257                    return Operator.compare(castToString(), str);
258            }
259    
260        /**
261         * @see railo.runtime.type.Collection#duplicate(boolean)
262         */
263        public Collection duplicate(boolean deepCopy) {
264            SVStruct svs = new SVStruct(key);
265            ThreadLocalDuplication.set(this, svs);
266            try{
267                    Collection.Key[] keys = keys();
268                    for(int i=0;i<keys.length;i++) {
269                        if(deepCopy)svs.setEL(keys[i],Duplicator.duplicate(get(keys[i],null),deepCopy));
270                        else svs.setEL(keys[i],get(keys[i],null));
271                    }
272                    return svs;
273            }
274            finally{
275                    ThreadLocalDuplication.remove(this);
276            }
277        }
278    
279            
280            
281        
282    
283            /**
284             *
285             * @see railo.runtime.type.Collection#containsKey(railo.runtime.type.Collection.Key)
286             */
287            public boolean containsKey(Collection.Key key) {
288                    return parent.containsKey(key);
289            }
290            
291    
292            /**
293             *
294             * @see railo.runtime.type.Collection#get(railo.runtime.type.Collection.Key)
295             */
296            public Object get(Collection.Key key) throws PageException {
297                    return parent.get(key);
298            }
299    
300            /**
301             *
302             * @see railo.runtime.type.Collection#get(railo.runtime.type.Collection.Key, java.lang.Object)
303             */
304            public Object get(Collection.Key key, Object defaultValue) {
305                    return parent.get(key, defaultValue);
306            }
307    
308    
309            /**
310             * @see railo.runtime.type.Collection#remove(railo.runtime.type.Collection.Key)
311             */
312            public Object remove(Collection.Key key) throws PageException {
313                    return parent.remove(key);
314            }
315    
316            /**
317             *
318             * @see railo.runtime.type.Collection#removeEL(railo.runtime.type.Collection.Key)
319             */
320            public Object removeEL(Collection.Key key) {
321                    return parent.removeEL(key);
322            }
323    
324            /**
325             *
326             * @see railo.runtime.type.Collection#set(railo.runtime.type.Collection.Key, java.lang.Object)
327             */
328            public Object set(Collection.Key key, Object value) throws PageException {
329                    return parent.set(key, value);
330            }
331    
332            /**
333             *
334             * @see railo.runtime.type.Collection#setEL(railo.runtime.type.Collection.Key, java.lang.Object)
335             */
336            public Object setEL(Collection.Key key, Object value) {
337                    return parent.setEL(key, value);
338            }
339    
340            /**
341             * @see java.util.Map#containsValue(java.lang.Object)
342             */
343            public boolean containsValue(Object value) {
344                    return parent.containsValue(value);
345            }
346    
347            /**
348             * @see java.util.Map#values()
349             */
350            public java.util.Collection values() {
351                    return parent.values();
352            }
353    
354    }