24
24
import java .util .Arrays ;
25
25
import java .util .HashMap ;
26
26
import java .util .Map ;
27
+
27
28
import org .exist .Namespaces ;
28
29
import org .exist .dom .QName ;
29
30
import org .exist .xquery .value .FunctionParameterSequenceType ;
33
34
/**
34
35
* Describes the signature of a built-in or user-defined function, i.e.
35
36
* its name, the type and cardinality of its arguments and its return type.
36
- *
37
+ *
37
38
* @author wolf
38
39
* @author lcahlander
39
40
* @version 1.3
@@ -43,25 +44,20 @@ public class FunctionSignature {
43
44
/**
44
45
* Default sequence type for function parameters.
45
46
*/
46
- public final static SequenceType DEFAULT_TYPE = new SequenceType (Type .ITEM , Cardinality .ZERO_OR_MORE );
47
+ public static final SequenceType DEFAULT_TYPE = new SequenceType (Type .ITEM , Cardinality .ZERO_OR_MORE );
47
48
48
49
/**
49
50
* Empty array to specify if a function doesn't take any arguments.
50
51
*/
51
- public final static SequenceType [] NO_ARGS = new SequenceType [0 ];
52
+ public static final SequenceType [] NO_ARGS = new SequenceType [0 ];
52
53
53
54
private static final String DEPRECATION_REMOVAL_MESSAGE = "\n This function could be removed in the next major release version." ;
54
-
55
- public static SequenceType [] singleArgument (final SequenceType arg ) {
56
- return new SequenceType [] { arg };
57
- }
58
-
59
- private Annotation [] annotations ;
60
55
private final QName name ;
56
+ private Annotation [] annotations ;
61
57
private SequenceType [] arguments ;
62
58
private SequenceType returnType ;
63
- private boolean isOverloaded = false ;
64
- private String description = null ;
59
+ private boolean isVariadic ;
60
+ private String description ;
65
61
private String deprecated = null ;
66
62
private Map <String , String > metadata = null ;
67
63
@@ -70,7 +66,7 @@ public FunctionSignature(final FunctionSignature other) {
70
66
this .arguments = other .arguments != null ? Arrays .copyOf (other .arguments , other .arguments .length ) : null ;
71
67
this .returnType = other .returnType ;
72
68
this .annotations = other .annotations != null ? Arrays .copyOf (other .annotations , other .annotations .length ) : null ;
73
- this .isOverloaded = other .isOverloaded ;
69
+ this .isVariadic = other .isVariadic ;
74
70
this .deprecated = other .deprecated ;
75
71
this .description = other .description ;
76
72
this .metadata = other .metadata != null ? new HashMap <>(other .metadata ) : null ;
@@ -89,17 +85,13 @@ public FunctionSignature(final QName name, final SequenceType[] arguments, final
89
85
}
90
86
91
87
public FunctionSignature (final QName name , final String description , final SequenceType [] arguments , final SequenceType returnType ) {
92
- this (name , description , arguments , returnType , false );
88
+ this (name , description , arguments , returnType , false );
93
89
}
94
90
95
91
public FunctionSignature (final QName name , final String description , final SequenceType [] arguments , final SequenceType returnType , final String deprecated ) {
96
92
this (name , description , arguments , returnType , false );
97
93
setDeprecated (deprecated );
98
94
}
99
-
100
- // public FunctionSignature(final QName name, final String description, final SequenceType[] arguments, final SequenceType returnType, final FunctionSignature deprecatedBy) {
101
- // this(name, description, arguments, returnType, false, "Moved to the module: " + deprecatedBy.getName().getNamespaceURI() + ", you should now use '" + deprecatedBy.getName().getPrefix() + ":" + deprecatedBy.getName().getLocalPart() + "' instead!");
102
- // }
103
95
104
96
public FunctionSignature (final QName name , final String description , final SequenceType [] arguments , final SequenceType returnType , final boolean overloaded , final String deprecated ) {
105
97
this (name , description , arguments , returnType , overloaded );
@@ -108,40 +100,44 @@ public FunctionSignature(final QName name, final String description, final Seque
108
100
109
101
/**
110
102
* Create a new function signature.
111
- *
112
- * @param name the QName of the function.
103
+ *
104
+ * @param name the QName of the function.
113
105
* @param description documentation string describing the function
114
- * @param arguments the sequence types of all expected arguments
115
- * @param returnType the sequence type returned by the function
116
- * @param overloaded set to true if the function may expect additional parameters
117
- */
106
+ * @param arguments the sequence types of all expected arguments
107
+ * @param returnType the sequence type returned by the function
108
+ * @param overloaded set to true if the function may expect additional parameters
109
+ */
118
110
public FunctionSignature (final QName name , final String description , final SequenceType [] arguments , final SequenceType returnType , final boolean overloaded ) {
119
111
this .name = name ;
120
112
this .arguments = arguments ;
121
113
this .returnType = returnType ;
122
- this .isOverloaded = overloaded ;
114
+ this .isVariadic = overloaded ;
123
115
this .description = description ;
124
116
}
125
117
126
118
public Annotation [] getAnnotations () {
127
119
return annotations ;
128
120
}
129
-
121
+
122
+ public void setAnnotations (final Annotation [] annotations ) {
123
+ this .annotations = annotations ;
124
+ }
125
+
130
126
public QName getName () {
131
127
return name ;
132
128
}
133
129
134
130
public int getArgumentCount () {
135
- if ( isOverloaded ) {
131
+ if ( isVariadic ) {
136
132
return -1 ;
137
133
}
138
134
return arguments != null ? arguments .length : 0 ;
139
135
}
140
-
136
+
141
137
public FunctionId getFunctionId () {
142
138
return new FunctionId (name , getArgumentCount ());
143
139
}
144
-
140
+
145
141
public SequenceType getReturnType () {
146
142
return returnType ;
147
143
}
@@ -157,21 +153,17 @@ public SequenceType[] getArgumentTypes() {
157
153
public void setArgumentTypes (final SequenceType [] types ) {
158
154
this .arguments = types ;
159
155
}
160
-
161
- public void setAnnotations (final Annotation [] annotations ) {
162
- this .annotations = annotations ;
163
- }
164
-
156
+
165
157
public String getDescription () {
166
- return description ;
158
+ return description ;
167
159
}
168
160
169
161
public void setDescription (final String description ) {
170
162
this .description = description ;
171
163
}
172
164
173
165
public void addMetadata (final String key , final String value ) {
174
- if (metadata == null ) {
166
+ if (metadata == null ) {
175
167
metadata = new HashMap <>(5 );
176
168
}
177
169
final String old = metadata .get (key );
@@ -192,7 +184,7 @@ public Map<String, String> getMetadata() {
192
184
}
193
185
194
186
public boolean isOverloaded () {
195
- return isOverloaded ;
187
+ return isVariadic ;
196
188
}
197
189
198
190
public boolean isDeprecated () {
@@ -206,17 +198,17 @@ public String getDeprecated() {
206
198
return null ;
207
199
}
208
200
}
209
-
201
+
210
202
public final void setDeprecated (final String message ) {
211
203
deprecated = message ;
212
204
}
213
205
214
206
public boolean isPrivate () {
215
207
final Annotation [] annotations = getAnnotations ();
216
- if (annotations != null ) {
217
- for (final Annotation annot : annotations ) {
208
+ if (annotations != null ) {
209
+ for (final Annotation annot : annotations ) {
218
210
final QName qn = annot .getName ();
219
- if (qn .getNamespaceURI ().equals (Namespaces .XPATH_FUNCTIONS_NS ) && "private" .equals (qn .getLocalPart ())) {
211
+ if (qn .getNamespaceURI ().equals (Namespaces .XPATH_FUNCTIONS_NS ) && "private" .equals (qn .getLocalPart ())) {
220
212
return true ;
221
213
}
222
214
}
@@ -229,29 +221,29 @@ public String toString() {
229
221
final StringBuilder buf = new StringBuilder ();
230
222
buf .append (name .getStringValue ());
231
223
buf .append ('(' );
232
- if (arguments != null ) {
224
+ if (arguments != null ) {
233
225
final char ANON_VAR = 'a' ;
234
- for (int i = 0 ; i < arguments .length ; i ++) {
235
- if (i > 0 ) {
226
+ for (int i = 0 ; i < arguments .length ; i ++) {
227
+ if (i > 0 ) {
236
228
buf .append (", " );
237
229
}
238
230
buf .append ('$' );
239
- if (arguments [i ] instanceof FunctionParameterSequenceType ) {
240
- buf .append (((FunctionParameterSequenceType )arguments [i ]).getAttributeName ());
231
+ if (arguments [i ] instanceof FunctionParameterSequenceType ) {
232
+ buf .append (((FunctionParameterSequenceType ) arguments [i ]).getAttributeName ());
241
233
} else {
242
- buf .append ((char )(ANON_VAR + i ));
234
+ buf .append ((char ) (ANON_VAR + i ));
243
235
}
244
236
buf .append (" as " );
245
237
buf .append (arguments [i ].toString ());
246
238
}
247
-
248
- if ( isOverloaded ) {
239
+
240
+ if ( isVariadic ) {
249
241
buf .append (", ..." );
250
242
}
251
243
}
252
244
buf .append (") as " );
253
245
buf .append (returnType .toString ());
254
-
246
+
255
247
return buf .toString ();
256
248
}
257
249
@@ -261,14 +253,16 @@ public boolean equals(final Object obj) {
261
253
return false ;
262
254
}
263
255
// quick comparison by object identity
264
- if (this == obj ) { return true ; }
256
+ if (this == obj ) {
257
+ return true ;
258
+ }
265
259
266
260
// anonymous functions cannot be compared by name and argument count
267
261
final FunctionSignature other = (FunctionSignature ) obj ;
268
262
if (
269
263
name == null || other .name == null ||
270
- name .getLocalPart ().equals ("" ) ||
271
- other .name .getLocalPart ().equals ("" )
264
+ name .getLocalPart ().equals ("" ) ||
265
+ other .name .getLocalPart ().equals ("" )
272
266
) {
273
267
return false ;
274
268
}
@@ -288,7 +282,7 @@ public FunctionSignature rename(final QName newName) {
288
282
final SequenceType [] argumentsCopy = arguments != null ? Arrays .copyOf (arguments , arguments .length ) : null ;
289
283
final FunctionSignature newFunctionSignature = new FunctionSignature (newName , description , argumentsCopy , returnType , deprecated );
290
284
newFunctionSignature .annotations = annotations != null ? Arrays .copyOf (annotations , annotations .length ) : null ;
291
- newFunctionSignature .isOverloaded = isOverloaded ;
285
+ newFunctionSignature .isVariadic = isVariadic ;
292
286
newFunctionSignature .metadata = metadata != null ? new HashMap <>(metadata ) : null ;
293
287
return newFunctionSignature ;
294
288
}
0 commit comments