001    package railo.runtime.type;
002    
003    import java.util.Date;
004    
005    import railo.runtime.PageContext;
006    import railo.runtime.dump.DumpData;
007    import railo.runtime.dump.DumpProperties;
008    import railo.runtime.dump.DumpUtil;
009    import railo.runtime.dump.Dumpable;
010    import railo.runtime.exp.PageException;
011    import railo.runtime.op.Caster;
012    import railo.runtime.op.Operator;
013    import railo.runtime.op.date.DateCaster;
014    import railo.runtime.type.dt.DateTime;
015    import railo.runtime.type.util.ListUtil;
016    
017    /**
018     * represent a named function value for a functions
019     */
020    public final class FunctionValueImpl implements FunctionValue,Dumpable {
021            
022    
023            private final Collection.Key name;
024            private final String[] names;
025            private final Object value;
026    
027            /**
028             * @param name
029             * @param value
030             * @return
031             */
032            public static FunctionValue newInstance(String name,Object value) {
033                    return new FunctionValueImpl(name,value);
034            }
035            
036            public static FunctionValue newInstance(String[] name,Object value) {
037                    return new FunctionValueImpl(name,value);
038            }
039    
040            public static FunctionValue newInstance(Collection.Key name,Object value) {
041                    return new FunctionValueImpl(name,value);
042            }
043            
044            /**
045             * constructor of the class
046             * @param name name of the value
047             * @param value value himself
048             */
049            public FunctionValueImpl(String name,Object value) {
050            this.name=KeyImpl.init(name);
051                    this.value=value;
052                    names=null;
053            } 
054    
055            public FunctionValueImpl(Collection.Key name,Object value) {
056            this.name=name;
057                    this.value=value;
058                    names=null;
059            } 
060            
061            public FunctionValueImpl(String[] names,Object value) {
062            this.names=names;
063                    this.value=value;
064                    name=null;
065            } 
066            
067            @Override
068            public String getName() {
069                    return getNameAsString();
070            }
071            
072            @Override
073            public String getNameAsString() {
074                    if(name==null){
075                            return ListUtil.arrayToList(names, ".");
076                    }
077                    return name.getString();
078            }
079            
080            @Override
081            public Collection.Key getNameAsKey() {
082                    if(name==null){
083                            return KeyImpl.init(ListUtil.arrayToList(names, "."));
084                    }
085                    return name;
086            }
087            
088            
089            
090            public String[] getNames() {
091                    return names;
092            }
093            
094            @Override
095            public Object getValue() {
096                    return value;
097            }
098    
099            @Override
100            public String castToString() throws PageException {
101                    return Caster.toString(value);
102            }
103    
104        @Override
105        public String castToString(String defaultValue) {
106            return Caster.toString(value,defaultValue);
107        }
108        
109            @Override
110            public boolean castToBooleanValue() throws PageException {
111                    return Caster.toBooleanValue(value);
112            }
113        
114        @Override
115        public Boolean castToBoolean(Boolean defaultValue) {
116            return Caster.toBoolean(value,defaultValue);
117        }
118            
119            @Override
120            public double castToDoubleValue() throws PageException {
121                    return Caster.toDoubleValue(value);
122            }
123        
124        @Override
125        public double castToDoubleValue(double defaultValue) {
126            return Caster.toDoubleValue(value,defaultValue);
127        }
128        
129            @Override
130            public DateTime castToDateTime() throws PageException {
131                    return DateCaster.toDateSimple(value,true,true,null);
132            }
133        
134        @Override
135        public DateTime castToDateTime(DateTime defaultValue) {
136            return DateCaster.toDateSimple(value,true,true,null,defaultValue);
137        }
138    
139            @Override
140            public int compareTo(boolean b) throws PageException {
141                    return Operator.compare(value, b?1D:0D);
142            }
143    
144            @Override
145            public int compareTo(DateTime dt) throws PageException {
146                    return Operator.compare(value, (Date)dt);
147            }
148    
149            @Override
150            public int compareTo(double d) throws PageException {
151                    return Operator.compare(value, d);
152            }
153    
154            @Override
155            public int compareTo(String str) throws PageException {
156                    return Operator.compare(value, str);
157            }
158    
159            public DumpData toDumpData(PageContext pageContext, int maxlevel, DumpProperties properties) {
160                    return DumpUtil.toDumpData(value, pageContext, maxlevel, properties);
161            }
162    
163            @Override
164            public String toString() {
165                    return name+":"+value;
166            }
167    
168            public static Struct toStruct(FunctionValueImpl fv1){
169                    StructImpl sct = new StructImpl(StructImpl.TYPE_LINKED);
170                    sct.setEL(fv1.getNameAsKey(), fv1);
171                    return sct;
172            }
173            public static Struct toStruct(FunctionValueImpl fv1,FunctionValueImpl fv2){
174                    StructImpl sct = new StructImpl(StructImpl.TYPE_LINKED);
175                    sct.setEL(fv1.getNameAsKey(), fv1);
176                    sct.setEL(fv2.getNameAsKey(), fv2);
177                    return sct;
178            }
179            public static Struct toStruct(FunctionValueImpl fv1,FunctionValueImpl fv2,FunctionValueImpl fv3){
180                    StructImpl sct = new StructImpl(StructImpl.TYPE_LINKED);
181                    sct.setEL(fv1.getNameAsKey(), fv1);
182                    sct.setEL(fv2.getNameAsKey(), fv2);
183                    sct.setEL(fv3.getNameAsKey(), fv3);
184                    return sct;
185            }
186            public static Struct toStruct(FunctionValueImpl fv1,FunctionValueImpl fv2,FunctionValueImpl fv3,FunctionValueImpl fv4){
187                    StructImpl sct = new StructImpl(StructImpl.TYPE_LINKED);
188                    sct.setEL(fv1.getNameAsKey(), fv1);
189                    sct.setEL(fv2.getNameAsKey(), fv2);
190                    sct.setEL(fv3.getNameAsKey(), fv3);
191                    sct.setEL(fv4.getNameAsKey(), fv4);
192                    return sct;
193            }
194            
195    }