001    package railo.runtime.type;
002    
003    import java.io.Externalizable;
004    import java.io.IOException;
005    import java.io.ObjectInput;
006    import java.io.ObjectOutput;
007    
008    import railo.commons.lang.CFTypes;
009    import railo.commons.lang.ExternalizableUtil;
010    
011    /**
012     * a single argument of a function
013     */
014    public final class FunctionArgumentImpl implements FunctionArgument,Externalizable {
015            
016            private String dspName;
017            private String hint;
018            private Collection.Key name;
019            private short type;
020            private String strType;
021            private boolean required;
022            private Struct meta;
023            private int defaultType;
024            private boolean passByReference;
025            
026    
027            /** @deprecated use other constructor */
028            public FunctionArgumentImpl(String name,String type,boolean required) {
029                    this(name,type,required,"","");
030            }
031            /** @deprecated use other constructor */
032            public FunctionArgumentImpl(String name,String type,boolean required,String dspName,String hint) {
033                    this(name,type,required,DEFAULT_TYPE_RUNTIME_EXPRESSION,true,dspName,hint,null);
034            }
035            /** @deprecated use other constructor */
036            public FunctionArgumentImpl(String name,String type,boolean required,String dspName,String hint,StructImpl meta) {
037                    this(name,type,required,DEFAULT_TYPE_RUNTIME_EXPRESSION,true,dspName,hint,meta);
038            }
039            /** @deprecated use other constructor */
040            public FunctionArgumentImpl(String name,String type,boolean required,int defaultType,String dspName,String hint,StructImpl meta) {
041                    this(name, type, required, defaultType,true, dspName, hint, meta);
042            }
043            /** @deprecated use other constructor */
044            public FunctionArgumentImpl(String name,String type,boolean required,double defaultType,String dspName,String hint,StructImpl meta) {
045                    this(name, type, required, (int)defaultType,true, dspName, hint, meta);
046            }
047            /** @deprecated use other constructor */
048            public FunctionArgumentImpl(String name,String type,boolean required,double defaultType,boolean passByReference,String dspName,String hint,StructImpl meta) {
049                    this(name, type, required, (int)defaultType,passByReference, dspName, hint, meta);
050            }
051            /** @deprecated use other constructor */
052            public FunctionArgumentImpl(String name,String type,boolean required,int defaultType,boolean passByReference,String dspName,String hint,StructImpl meta) {
053                    this(KeyImpl.init(name),type,required,defaultType,passByReference,dspName,hint,meta);
054            }
055            /** @deprecated use other constructor */
056            public FunctionArgumentImpl(String name,String strType,short type,boolean required,int defaultType,boolean passByReference,String dspName,String hint,StructImpl meta) {
057                    this(KeyImpl.init(name), strType, type, required, defaultType, passByReference, dspName, hint, meta);
058            }
059            /** @deprecated use other constructor */
060            public FunctionArgumentImpl(Collection.Key name,String type,boolean required,int defaultType,boolean passByReference,String dspName,String hint,StructImpl meta) {
061                    this.name=name;
062                    this.strType=(type);
063                    this.type=CFTypes.toShortStrict(type,CFTypes.TYPE_UNKNOW);
064                    this.required=required;
065                    this.defaultType=defaultType;
066                    this.dspName=dspName;
067                    this.hint=hint;
068                    this.meta=meta;
069                    this.passByReference=passByReference;           
070            }
071    
072            /**
073             * NEVER USE THIS CONSTRUCTOR, this constructor is only for deserialize this object from stream
074             */
075            public FunctionArgumentImpl() {}
076    
077            
078            public FunctionArgumentImpl(Collection.Key name) {
079                    this(name, "any", CFTypes.TYPE_ANY, false, DEFAULT_TYPE_NULL, true, "", "", null);
080            }
081    
082            public FunctionArgumentImpl(Collection.Key name,short type) {
083                    this(name, CFTypes.toString(type,"any"), type, false, DEFAULT_TYPE_NULL, true, "", "", null);
084            }
085    
086            public FunctionArgumentImpl(Collection.Key name,String strType,short type) {
087                    this(name, strType, type, false, DEFAULT_TYPE_NULL, true, "", "", null);
088            }
089    
090            public FunctionArgumentImpl(Collection.Key name,String strType,short type,boolean required) {
091                    this(name, strType, type, required, DEFAULT_TYPE_NULL, true, "", "", null);
092            }
093            
094            public FunctionArgumentImpl(Collection.Key name,String strType,short type,boolean required,int defaultType) {
095                    this(name, strType, type, required, defaultType, true, "", "", null);
096            }
097            
098            public FunctionArgumentImpl(Collection.Key name,String strType,short type,boolean required,int defaultType,boolean passByReference) {
099                    this(name, strType, type, required, defaultType, passByReference, "", "", null);
100            }
101            
102            public FunctionArgumentImpl(Collection.Key name,String strType,short type,boolean required,int defaultType,boolean passByReference,String dspName) {
103                    this(name, strType, type, required, defaultType, passByReference, dspName, "", null);
104            }
105    
106            public FunctionArgumentImpl(Collection.Key name,String strType,short type,boolean required,int defaultType,boolean passByReference,String dspName,String hint) {
107                    this(name, strType, type, required, defaultType, passByReference, dspName, hint, null);
108            }
109            
110            public FunctionArgumentImpl(Collection.Key name,String strType,short type,boolean required,int defaultType,boolean passByReference,String dspName,String hint,StructImpl meta) {
111                    this.name=name;
112                    this.strType=strType;
113                    this.type=type;
114                    this.required=required;
115                    this.defaultType=defaultType;
116                    this.dspName=dspName;
117                    this.hint=hint;
118                    this.meta=meta;
119                    this.passByReference=passByReference;
120            }
121            
122            
123            
124            
125            
126            
127            
128            //private static StructImpl sct=new StructImpl();
129    
130    
131            /**
132             * @return the defaultType
133             */
134            public int getDefaultType() {
135                    return defaultType;
136            }
137    
138    
139            @Override
140            public Collection.Key getName() {
141                    return name;
142            }
143    
144            @Override
145            public boolean isRequired() {
146                    return required;
147            }
148    
149            @Override
150            public short getType() {
151                    return type;
152            }
153    
154            @Override
155            public String getTypeAsString() {
156                    return strType;
157            }
158    
159            @Override
160            public String getHint() {
161                    return hint;
162            }
163    
164    
165            @Override
166            public String getDisplayName() {
167                    return dspName;
168            }
169            
170            @Override
171            public Struct getMetaData() {
172                    return meta;
173            }
174            
175            public boolean isPassByReference() {
176                    return passByReference;
177            }
178    
179    
180            public void readExternal(ObjectInput in) throws IOException,ClassNotFoundException {
181                    dspName=ExternalizableUtil.readString(in);
182                    hint=ExternalizableUtil.readString(in);
183                    name=KeyImpl.init(ExternalizableUtil.readString(in));
184                    type=in.readShort();
185                    strType=ExternalizableUtil.readString(in);
186                    required=in.readBoolean();
187                    meta=(Struct) in.readObject();
188                    defaultType=in.readInt();
189                    passByReference=in.readBoolean();
190            }
191    
192    
193            public void writeExternal(ObjectOutput out) throws IOException {
194                    ExternalizableUtil.writeString(out, dspName);
195                    ExternalizableUtil.writeString(out, hint);
196                    ExternalizableUtil.writeString(out, name.getString());
197                    out.writeShort(type);
198                    ExternalizableUtil.writeString(out, strType);
199                    out.writeBoolean(required);
200                    out.writeObject(meta);
201                    out.writeInt(defaultType);
202                    out.writeBoolean(passByReference);
203            }
204            
205    
206            public boolean equals(Object obj){
207                    if(!(obj instanceof FunctionArgument)) return false;
208                    return equals(this,(FunctionArgument)obj);
209            }
210            
211            public static boolean equals(FunctionArgument left, FunctionArgument right) {
212                    if(
213                                    left.getDefaultType()!=right.getDefaultType()
214                                    || left.getType()!=right.getType()
215                                    || !_eq(left.getName(), right.getName())
216                                    || !_eq(left.getTypeAsString(), right.getTypeAsString())
217                                    || left.isPassByReference()!=right.isPassByReference()
218                                    || left.isRequired()!=right.isRequired()
219                    )
220                            return false;
221                    
222                    
223                    return true;
224            }
225            
226            private static boolean _eq(Object left, Object right) {
227                    if(left==null) return right==null;
228                    return left.equals(right);
229            }
230    }