Skip to content

Commit 23cbf62

Browse files
committed
[refactor] Simplify the code in fn:reverse
1 parent 9f409f5 commit 23cbf62

File tree

2 files changed

+20
-63
lines changed

2 files changed

+20
-63
lines changed

exist-core/src/main/java/org/exist/xquery/functions/fn/FnModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public class FnModule extends AbstractInternalModule {
210210
new FunctionDef(FunRemove.signature, FunRemove.class),
211211
new FunctionDef(FunReplace.FS_REPLACE[0], FunReplace.class),
212212
new FunctionDef(FunReplace.FS_REPLACE[1], FunReplace.class),
213-
new FunctionDef(FunReverse.signature, FunReverse.class),
213+
new FunctionDef(FunReverse.FS_REVERSE, FunReverse.class),
214214
new FunctionDef(FunResolveURI.signatures[0], FunResolveURI.class),
215215
new FunctionDef(FunResolveURI.signatures[1], FunResolveURI.class),
216216
new FunctionDef(FunRoot.signatures[0], FunRoot.class),

exist-core/src/main/java/org/exist/xquery/functions/fn/FunReverse.java

Lines changed: 19 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -45,78 +45,40 @@
4545
*/
4646
package org.exist.xquery.functions.fn;
4747

48-
import org.exist.dom.QName;
49-
import org.exist.xquery.*;
50-
import org.exist.xquery.value.FunctionParameterSequenceType;
51-
import org.exist.xquery.value.FunctionReturnSequenceType;
52-
import org.exist.xquery.value.Item;
48+
import org.exist.xquery.BasicFunction;
49+
import org.exist.xquery.FunctionSignature;
50+
import org.exist.xquery.XPathException;
51+
import org.exist.xquery.XQueryContext;
5352
import org.exist.xquery.value.Sequence;
5453
import org.exist.xquery.value.ValueSequence;
55-
import org.exist.xquery.value.SequenceType;
5654
import org.exist.xquery.value.Type;
5755

56+
import static org.exist.xquery.FunctionDSL.optManyParam;
57+
import static org.exist.xquery.FunctionDSL.returnsOptMany;
58+
import static org.exist.xquery.functions.fn.FnModule.functionSignature;
59+
5860
/**
5961
* Implements the fn:reverse function.
6062
*
6163
* @author <a href="mailto:[email protected]">Piotr Kaminski</a>
6264
* @author <a href="mailto:[email protected]">Adam Retter</a>
6365
*/
64-
public class FunReverse extends Function {
66+
public class FunReverse extends BasicFunction {
6567

66-
public final static FunctionSignature signature =
67-
new FunctionSignature(
68-
new QName("reverse", FnModule.NAMESPACE_URI),
69-
"Reverses the order of items in a sequence. If the argument is an empty" +
70-
"sequence, the empty sequence is returned.",
71-
new SequenceType[] {new FunctionParameterSequenceType("arg", Type.ITEM, Cardinality.ZERO_OR_MORE, "The sequence to reverse")},
72-
new FunctionReturnSequenceType(Type.ITEM, Cardinality.ZERO_OR_MORE, "the reverse order sequence"));
68+
public static final FunctionSignature FS_REVERSE = functionSignature(
69+
"reverse",
70+
"Reverses the order of items in a sequence. If the argument is an empty sequence, the empty sequence is returned.",
71+
returnsOptMany(Type.ITEM, "The reverse order sequence"),
72+
optManyParam("arg", Type.ITEM, "The sequence to reverse")
73+
);
7374

74-
public FunReverse(XQueryContext context) {
75-
super(context, signature);
76-
}
77-
78-
public void analyze(final AnalyzeContextInfo contextInfo) throws XPathException {
79-
inPredicate = (contextInfo.getFlags() & IN_PREDICATE) > 0;
80-
contextId = contextInfo.getContextId();
81-
contextInfo.setParent(this);
82-
83-
final SequenceType[] argumentTypes = getSignature().getArgumentTypes();
84-
for (int i = 0; i < getArgumentCount(); i++) {
85-
final Expression arg = getArgument(i);
86-
87-
// call analyze for each argument
88-
final AnalyzeContextInfo argContextInfo = new AnalyzeContextInfo(contextInfo);
89-
arg.analyze(argContextInfo);
90-
if (i == 0) {
91-
contextInfo.setStaticReturnType(argContextInfo.getStaticReturnType());
92-
}
93-
94-
if (!argumentsChecked) {
95-
// statically check the argument
96-
SequenceType argType = null;
97-
if (argumentTypes != null && i < argumentTypes.length) {
98-
argType = argumentTypes[i];
99-
}
100-
checkArgument(arg, argType, argContextInfo, i + 1);
101-
}
102-
}
103-
argumentsChecked = true;
75+
public FunReverse(final XQueryContext context, final FunctionSignature signature) {
76+
super(context, signature);
10477
}
10578

10679
@Override
107-
public Sequence eval(final Sequence contextSequence, final Item contextItem) throws XPathException {
108-
if (context.getProfiler().isEnabled()) {
109-
context.getProfiler().start(this);
110-
context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies()));
111-
if (contextSequence != null) {
112-
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);
113-
}
114-
if (contextItem != null) {
115-
context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());
116-
}
117-
}
118-
119-
final Sequence seq = getArguments(contextSequence, contextItem)[0];
80+
public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException {
81+
final Sequence seq = args[0];
12082
final Sequence result;
12183
if (seq.isEmpty()) {
12284
result = Sequence.EMPTY_SEQUENCE;
@@ -126,11 +88,6 @@ public Sequence eval(final Sequence contextSequence, final Item contextItem) thr
12688
result.add(seq.itemAt(i));
12789
}
12890
}
129-
130-
if (context.getProfiler().isEnabled()) {
131-
context.getProfiler().end(this, "", result);
132-
}
133-
13491
return result;
13592
}
13693
}

0 commit comments

Comments
 (0)