• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

evolvedbinary / elemental / 982

29 Apr 2025 08:34PM UTC coverage: 56.409% (+0.007%) from 56.402%
982

push

circleci

adamretter
[feature] Improve README.md badges

28451 of 55847 branches covered (50.94%)

Branch coverage included in aggregate %.

77468 of 131924 relevant lines covered (58.72%)

0.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.88
/exist-core/src/main/java/org/exist/xquery/functions/fn/FnFormatIntegers.java
1
/*
2
 * Elemental
3
 * Copyright (C) 2024, Evolved Binary Ltd
4
 *
5
 * admin@evolvedbinary.com
6
 * https://www.evolvedbinary.com | https://www.elemental.xyz
7
 *
8
 * Use of this software is governed by the Business Source License 1.1
9
 * included in the LICENSE file and at www.mariadb.com/bsl11.
10
 *
11
 * Change Date: 2028-04-27
12
 *
13
 * On the date above, in accordance with the Business Source License, use
14
 * of this software will be governed by the Apache License, Version 2.0.
15
 *
16
 * Additional Use Grant: Production use of the Licensed Work for a permitted
17
 * purpose. A Permitted Purpose is any purpose other than a Competing Use.
18
 * A Competing Use means making the Software available to others in a commercial
19
 * product or service that: substitutes for the Software; substitutes for any
20
 * other product or service we offer using the Software that exists as of the
21
 * date we make the Software available; or offers the same or substantially
22
 * similar functionality as the Software.
23
 */
24
package org.exist.xquery.functions.fn;
25

26
import org.exist.xquery.BasicFunction;
27
import org.exist.xquery.FunctionSignature;
28
import org.exist.xquery.XPathException;
29
import org.exist.xquery.XQueryContext;
30
import org.exist.xquery.functions.integer.IntegerPicture;
31
import org.exist.xquery.value.*;
32

33
import java.math.BigInteger;
34
import java.util.ArrayList;
35
import java.util.List;
36

37
import static org.exist.xquery.FunctionDSL.*;
38
import static org.exist.xquery.functions.fn.FnModule.functionSignatures;
39

40
/**
41
 * Implements fn:format-integer as per W3C XPath and XQuery Functions and Operators 3.1
42
 * <p>
43
 * fn:format-number($value as integer?, $picture as xs:string) as xs:string
44
 * fn:format-number($value as integer?, $picture as xs:string, $lang as xs:string) as xs:string
45
 *
46
 * @author <a href="mailto:alan@evolvedbinary.com">Alan Paxton</a>
47
 */
48
public class FnFormatIntegers extends BasicFunction {
49

50
    private static final FunctionParameterSequenceType FS_PARAM_VALUE = optParam("value", Type.INTEGER, "The number to format");
1✔
51
    private static final FunctionParameterSequenceType FS_PARAM_PICTURE = param("picture", Type.STRING, "The picture string to use for formatting. To understand the picture string syntax, see: https://www.w3.org/TR/xpath-functions-31/#func-format-number");
1✔
52

53
    private static final String FS_FORMAT_INTEGER_NAME = "format-integer";
54
    static final FunctionSignature[] FS_FORMAT_INTEGER = functionSignatures(
1✔
55
            FS_FORMAT_INTEGER_NAME,
1✔
56
            "Returns a string containing an integer formatted according to a given picture string.",
1✔
57
            returns(Type.STRING, "The formatted string representation of the supplied integer"),
1✔
58
            arities(
1✔
59
                    arity(
1✔
60
                            FS_PARAM_VALUE,
1✔
61
                            FS_PARAM_PICTURE
1✔
62
                    ),
63
                    arity(
1✔
64
                            FS_PARAM_VALUE,
1✔
65
                            FS_PARAM_PICTURE,
1✔
66
                            optParam("lang", Type.STRING, "The language in which to format the integers.")
1✔
67
                    )
68
            )
69
    );
1✔
70

71
    public FnFormatIntegers(final XQueryContext context, final FunctionSignature signature) {
72
        super(context, signature);
1✔
73
    }
1✔
74

75
    @Override
76
    public Sequence eval(final Sequence[] args, final Sequence contextSequence)
77
            throws XPathException {
78
        // If $value is an empty sequence, the function returns a zero-length string
79
        // https://www.w3.org/TR/xpath-functions-31/#func-format-integer
80
        if (args[0].isEmpty()) {
1✔
81
            return Sequence.EMPTY_SEQUENCE;
1✔
82
        }
83

84
        // If the value of $value is negative, the rules below are applied to the absolute value of $value,
85
        // and a minus sign is prepended to the result.
86
        final IntegerValue integerValue = (IntegerValue) args[0].itemAt(0);
1✔
87
        final BigInteger bigInteger = integerValue.toJavaObject(BigInteger.class);
1✔
88

89
        final IntegerPicture picture = IntegerPicture.fromString(args[1].getStringValue());
1✔
90

91
        // Build a list of languages to try
92
        // the called picture will use the first one with a valid locale
93
        final List<String> languages = new ArrayList<>(2);
1✔
94
        if (args.length == 3 && !args[2].isEmpty()) {
1!
95
            languages.add(args[2].getStringValue());
×
96
        }
97
        languages.add(context.getDefaultLanguage());
1✔
98

99
        return new StringValue(this, picture.formatInteger(bigInteger, languages));
1✔
100
    }
101
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc