001    package railo.runtime.type.util;
002    
003    import java.util.Iterator;
004    import java.util.Map;
005    import java.util.Set;
006    
007    import railo.runtime.PageContext;
008    import railo.runtime.converter.LazyConverter;
009    import railo.runtime.dump.DumpData;
010    import railo.runtime.dump.DumpProperties;
011    import railo.runtime.exp.ExpressionException;
012    import railo.runtime.exp.PageException;
013    import railo.runtime.type.Collection;
014    import railo.runtime.type.KeyImpl;
015    import railo.runtime.type.List;
016    import railo.runtime.type.Sizeable;
017    import railo.runtime.type.Struct;
018    import railo.runtime.type.dt.DateTime;
019    
020    public abstract class StructSupport implements Map,Struct,Sizeable {
021    
022            private static final long serialVersionUID = 7433668961838400995L;
023    
024            /**
025             * throw exception for invalid key
026             * @param key Invalid key
027             * @return returns a invalid key Exception
028             */
029            protected ExpressionException invalidKey(Key key) {
030                    return new ExpressionException("key ["+key.getString()+"] doesn't exist in struct (keys:"+List.arrayToList(keysAsString(), ",")+")");
031            }
032            public static ExpressionException invalidKey(String[] keys, Key key) {
033                    return new ExpressionException("key ["+key.getString()+"] doesn't exist in struct (keys:"+List.arrayToList(keys, ",")+")");
034            }
035            
036            
037            /**
038             * @see railo.runtime.type.Sizeable#sizeOf()
039             */
040            public long sizeOf() {
041                    return StructUtil.sizeOf(this);
042            }
043            
044            /**
045             * @see java.util.Map#entrySet()
046             */
047            public Set entrySet() {
048                    return StructUtil.entrySet(this);
049            }
050    
051            /**
052             * @see java.util.Map#get(java.lang.Object)
053             */
054            public final Object get(Object key) {
055                    return get(KeyImpl.toKey(key,null), null);
056            }
057    
058            /**
059             * @see java.util.Map#isEmpty()
060             */
061            public final boolean isEmpty() {
062                    return size()==0;
063            }
064    
065            /**
066             * @see java.util.Map#keySet()
067             */
068            public Set keySet() {
069                    return StructUtil.keySet(this);
070            }
071    
072            /**
073             * @see java.util.Map#put(K, V)
074             */
075            public final Object put(Object key, Object value) {
076                    return setEL(KeyImpl.toKey(key,null), value);
077            }
078    
079            /**
080             * @see java.util.Map#putAll(java.util.Map)
081             */
082            public final void putAll(Map t) {
083                    StructUtil.putAll(this, t);
084            }
085    
086            /**
087             * @see java.util.Map#remove(java.lang.Object)
088             */
089            public final Object remove(Object key) {
090                    return removeEL(KeyImpl.toKey(key,null));
091            }
092    
093            /**
094             * @see java.lang.Object#clone()
095             */
096            public final Object clone(){
097                    return duplicate(true);
098            }
099            
100            /**
101             * @see java.util.Map#containsKey(java.lang.Object)
102             */
103            public final boolean containsKey(Object key) {
104                    return containsKey(KeyImpl.toKey(key,null));
105            }
106    
107            /**
108             * @see railo.runtime.type.Collection#containsKey(java.lang.String)
109             */
110            public final boolean containsKey(String key) {
111                    return containsKey(KeyImpl.init(key));
112            }
113    
114            /**
115             * @see railo.runtime.type.Collection#get(java.lang.String, java.lang.Object)
116             */
117            public final Object get(String key, Object defaultValue) {
118                    return get(KeyImpl.init(key), defaultValue);
119            }
120    
121            /**
122             * @see railo.runtime.type.Collection#get(java.lang.String)
123             */
124            public final Object get(String key) throws PageException {
125                    return get(KeyImpl.init(key));
126            }
127    
128            /**
129             * @see railo.runtime.type.Collection#set(java.lang.String, java.lang.Object)
130             */
131            public final Object set(String key, Object value) throws PageException {
132                    return set(KeyImpl.init(key), value);
133            }
134    
135            /**
136             * @see railo.runtime.type.Collection#setEL(java.lang.String, java.lang.Object)
137             */
138            public final Object setEL(String key, Object value) {
139                    //print.dumpStack("StructSupport.setEL");
140                    return setEL(KeyImpl.init(key), value);
141            }
142    
143            /**
144             * @see railo.runtime.dump.Dumpable#toDumpData(railo.runtime.PageContext, int, railo.runtime.dump.DumpProperties)
145             */
146            public DumpData toDumpData(PageContext pageContext, int maxlevel,DumpProperties properties) {
147                    return StructUtil.toDumpTable(this,"Struct",pageContext,maxlevel,properties);
148            }
149    
150            /**
151         * @see railo.runtime.op.Castable#castToBooleanValue()
152         */
153        public boolean castToBooleanValue() throws PageException {
154            throw new ExpressionException("can't cast Complex Object Type Struct to a boolean value");
155        }
156        
157        /**
158         * @see railo.runtime.op.Castable#castToBoolean(java.lang.Boolean)
159         */
160        public Boolean castToBoolean(Boolean defaultValue) {
161            return defaultValue;
162        }
163    
164        /**
165         * @see railo.runtime.op.Castable#castToDoubleValue()
166         */
167        public double castToDoubleValue() throws PageException {
168            throw new ExpressionException("can't cast Complex Object Type Struct to a number value");
169        }
170        
171        /**
172         * @see railo.runtime.op.Castable#castToDoubleValue(double)
173         */
174        public double castToDoubleValue(double defaultValue) {
175            return defaultValue;
176        }
177    
178    
179        /**
180         * @see railo.runtime.op.Castable#castToDateTime()
181         */
182        public DateTime castToDateTime() throws PageException {
183            throw new ExpressionException("can't cast Complex Object Type Struct to a Date");
184        }
185        
186        /**
187         * @see railo.runtime.op.Castable#castToDateTime(railo.runtime.type.dt.DateTime)
188         */
189        public DateTime castToDateTime(DateTime defaultValue) {
190            return defaultValue;
191        }
192    
193        /**
194         * @see railo.runtime.op.Castable#castToString()
195         */
196        public String castToString() throws PageException {
197            throw new ExpressionException("Can't cast Complex Object Type Struct to String",
198              "Use Build-In-Function \"serialize(Struct):String\" to create a String from Struct");
199        }
200    
201        /**
202         * @see railo.runtime.op.Castable#castToString(java.lang.String)
203         */
204        public String castToString(String defaultValue) {
205            return defaultValue;
206        }
207    
208    
209            /**
210             * @see railo.runtime.op.Castable#compare(boolean)
211             */
212            public int compareTo(boolean b) throws PageException {
213                    throw new ExpressionException("can't compare Complex Object Type Struct with a boolean value");
214            }
215    
216            /**
217             * @see railo.runtime.op.Castable#compareTo(railo.runtime.type.dt.DateTime)
218             */
219            public int compareTo(DateTime dt) throws PageException {
220                    throw new ExpressionException("can't compare Complex Object Type Struct with a DateTime Object");
221            }
222    
223            /**
224             * @see railo.runtime.op.Castable#compareTo(double)
225             */
226            public int compareTo(double d) throws PageException {
227                    throw new ExpressionException("can't compare Complex Object Type Struct with a numeric value");
228            }
229    
230            /**
231             * @see railo.runtime.op.Castable#compareTo(java.lang.String)
232             */
233            public int compareTo(String str) throws PageException {
234                    throw new ExpressionException("can't compare Complex Object Type Struct with a String");
235            }
236            
237            /**
238             *
239             * @see java.lang.Object#toString()
240             */
241            public String toString() {
242                    return LazyConverter.serialize(this);
243            }
244    
245            /**
246             * @see java.util.Map#values()
247             */
248            public java.util.Collection values() {
249                    return StructUtil.values(this);
250            }
251    
252            /**
253             * @see java.util.Map#containsValue(java.lang.Object)
254             */
255            public boolean containsValue(Object value) {
256                    return values().contains(value);
257            }
258            
259            /**
260             *
261             * @see railo.runtime.type.Iteratorable#iterator()
262             */
263            public final Iterator iterator() {
264                    return keyIterator();
265            }
266            
267    
268            /**
269             * @see railo.runtime.type.Iteratorable#valueIterator()
270             */
271            public Iterator valueIterator() {
272                    return values().iterator();
273            }
274            
275            public boolean equals(Object obj){
276                    if(!(obj instanceof Collection)) return false;
277                    return CollectionUtil.equals(this,(Collection)obj);
278            }
279    }