Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NUMBERS-88] Don't use the default locale in message formatting #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.commons.numbers.combinatorics;

import java.text.MessageFormat;
import java.util.Locale;

/**
* Package private exception class with constants for frequently used messages.
Expand Down Expand Up @@ -50,6 +51,6 @@ class CombinatoricsException extends IllegalArgumentException {
/** {@inheritDoc} */
@Override
public String getMessage() {
return MessageFormat.format(super.getMessage(), formatArguments);
return new MessageFormat(super.getMessage(), Locale.ENGLISH).format(formatArguments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.Locale;

/**
* Some useful, arithmetics related, additions to the built-in functions in
Expand Down Expand Up @@ -779,7 +780,7 @@ private static class NumbersArithmeticException extends ArithmeticException {
/** {@inheritDoc} */
@Override
public String getMessage() {
return MessageFormat.format(super.getMessage(), formatArguments);
return new MessageFormat(super.getMessage(), Locale.ENGLISH).format(formatArguments);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.commons.numbers.fraction;

import java.text.MessageFormat;
import java.util.Locale;

/**
* Package private exception class with constants for frequently used messages.
Expand Down Expand Up @@ -53,7 +54,7 @@ class FractionException extends ArithmeticException {
/** {@inheritDoc} */
@Override
public String getMessage() {
return MessageFormat.format(super.getMessage(), formatArguments);
return new MessageFormat(super.getMessage(), Locale.ENGLISH).format(formatArguments);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package org.apache.commons.numbers.fraction;

import java.text.MessageFormat;
import java.text.ParseException;
import java.util.Locale;

/**
* Error thrown when a string cannot be parsed into a fraction.
Expand All @@ -36,8 +36,9 @@ class FractionParseException extends ParseException {
* @param type type of target object
*/
FractionParseException(String source, int position, Class<?> type) {
super(MessageFormat.format("string \"{0}\" unparseable (from position {1}) as an object of type {2}",
source, position, type),
super(String.format(Locale.ENGLISH,
"string \"%s\" unparseable (from position %d) as an object of type %s",
source, position, type),
position);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,14 @@ public void testParseBig() throws Exception {
@Test
public void testNumeratorFormat() {
NumberFormat old = properFormat.getNumeratorFormat();
NumberFormat nf = NumberFormat.getInstance();
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
nf.setParseIntegerOnly(true);
properFormat.setNumeratorFormat(nf);
Assert.assertEquals(nf, properFormat.getNumeratorFormat());
properFormat.setNumeratorFormat(old);

old = improperFormat.getNumeratorFormat();
nf = NumberFormat.getInstance();
nf = NumberFormat.getInstance(Locale.ENGLISH);
nf.setParseIntegerOnly(true);
improperFormat.setNumeratorFormat(nf);
Assert.assertEquals(nf, improperFormat.getNumeratorFormat());
Expand All @@ -294,14 +294,14 @@ public void testNumeratorFormat() {
@Test
public void testDenominatorFormat() {
NumberFormat old = properFormat.getDenominatorFormat();
NumberFormat nf = NumberFormat.getInstance();
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
nf.setParseIntegerOnly(true);
properFormat.setDenominatorFormat(nf);
Assert.assertEquals(nf, properFormat.getDenominatorFormat());
properFormat.setDenominatorFormat(old);

old = improperFormat.getDenominatorFormat();
nf = NumberFormat.getInstance();
nf = NumberFormat.getInstance(Locale.ENGLISH);
nf.setParseIntegerOnly(true);
improperFormat.setDenominatorFormat(nf);
Assert.assertEquals(nf, improperFormat.getDenominatorFormat());
Expand All @@ -313,7 +313,7 @@ public void testWholeFormat() {
ProperBigFractionFormat format = (ProperBigFractionFormat)properFormat;

NumberFormat old = format.getWholeFormat();
NumberFormat nf = NumberFormat.getInstance();
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
nf.setParseIntegerOnly(true);
format.setWholeFormat(nf);
Assert.assertEquals(nf, format.getWholeFormat());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,14 +295,14 @@ public void testParseProperInvalidMinus() {
@Test
public void testNumeratorFormat() {
NumberFormat old = properFormat.getNumeratorFormat();
NumberFormat nf = NumberFormat.getInstance();
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
nf.setParseIntegerOnly(true);
properFormat.setNumeratorFormat(nf);
Assert.assertEquals(nf, properFormat.getNumeratorFormat());
properFormat.setNumeratorFormat(old);

old = improperFormat.getNumeratorFormat();
nf = NumberFormat.getInstance();
nf = NumberFormat.getInstance(Locale.ENGLISH);
nf.setParseIntegerOnly(true);
improperFormat.setNumeratorFormat(nf);
Assert.assertEquals(nf, improperFormat.getNumeratorFormat());
Expand All @@ -312,14 +312,14 @@ public void testNumeratorFormat() {
@Test
public void testDenominatorFormat() {
NumberFormat old = properFormat.getDenominatorFormat();
NumberFormat nf = NumberFormat.getInstance();
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
nf.setParseIntegerOnly(true);
properFormat.setDenominatorFormat(nf);
Assert.assertEquals(nf, properFormat.getDenominatorFormat());
properFormat.setDenominatorFormat(old);

old = improperFormat.getDenominatorFormat();
nf = NumberFormat.getInstance();
nf = NumberFormat.getInstance(Locale.ENGLISH);
nf.setParseIntegerOnly(true);
improperFormat.setDenominatorFormat(nf);
Assert.assertEquals(nf, improperFormat.getDenominatorFormat());
Expand All @@ -331,7 +331,7 @@ public void testWholeFormat() {
ProperFractionFormat format = (ProperFractionFormat)properFormat;

NumberFormat old = format.getWholeFormat();
NumberFormat nf = NumberFormat.getInstance();
NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
nf.setParseIntegerOnly(true);
format.setWholeFormat(nf);
Assert.assertEquals(nf, format.getWholeFormat());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.commons.numbers.gamma;

import java.text.MessageFormat;
import java.util.Locale;

/**
* Package private exception class with constants for frequently used messages.
Expand Down Expand Up @@ -47,6 +48,6 @@ class GammaException extends IllegalArgumentException {

@Override
public String getMessage() {
return MessageFormat.format(super.getMessage(), formatArguments);
return new MessageFormat(super.getMessage(), Locale.ENGLISH).format(formatArguments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.junit.Assert;
import org.junit.Test;

import java.util.Locale;

/**
* Tests for {@link Digamma}.
*/
Expand Down Expand Up @@ -51,7 +53,7 @@ public void testDigammaSmallArgs() {
-1e+17, -1e+18, -1e+19, -1e+20, -1e+21, -1e+22, -1e+23, -1e+24, -1e+25, -1e+26,
-1e+27, -1e+28, -1e+29, -1e+30};
for (double n = 1; n < 30; n++) {
checkRelativeError(String.format("Test %.0f: ", n), expected[(int) (n - 1)], Digamma.value(Math.pow(10.0, -n)), 1e-8);
checkRelativeError(String.format(Locale.ENGLISH, "Test %.0f: ", n), expected[(int) (n - 1)], Digamma.value(Math.pow(10.0, -n)), 1e-8);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.junit.Assert;
import org.junit.Test;

import java.util.Locale;

/**
* Tests for {@link Trigamma}.
*/
Expand All @@ -45,7 +47,7 @@ public void testTrigamma() {
100, 0.010050166663333571395
};
for (int i = data.length - 2; i >= 0; i -= 2) {
Assert.assertEquals(String.format("trigamma %.0f", data[i]), data[i + 1], Trigamma.value(data[i]), eps);
Assert.assertEquals(String.format(Locale.ENGLISH, "trigamma %.0f", data[i]), data[i + 1], Trigamma.value(data[i]), eps);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.commons.numbers.primes;

import java.text.MessageFormat;
import java.util.List;


Expand All @@ -30,8 +29,9 @@
*/
public class Primes {

/** Exception message format when an argument is too small. */
static final String NUMBER_TOO_SMALL = "{0} is smaller than the minimum ({1})";
static String numberTooSmallMessage(int x, int min) {
return x + " is smaller than the minimum (" + min + ")";
}

/**
* Hide utility class.
Expand Down Expand Up @@ -71,8 +71,7 @@ public static boolean isPrime(int n) {
*/
public static int nextPrime(int n) {
if (n < 0) {
throw new IllegalArgumentException(
MessageFormat.format(NUMBER_TOO_SMALL, n, 0));
throw new IllegalArgumentException(numberTooSmallMessage(n, 0));
}
if (n == 2) {
return 2;
Expand Down Expand Up @@ -117,8 +116,7 @@ public static int nextPrime(int n) {
public static List<Integer> primeFactors(int n) {

if (n < 2) {
throw new IllegalArgumentException(
MessageFormat.format(NUMBER_TOO_SMALL, n, 2));
throw new IllegalArgumentException(numberTooSmallMessage(n, 2));
}
return SmallPrimes.trialDivision(n);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.commons.numbers.primes;


import java.text.MessageFormat;
import java.util.HashSet;
import java.util.List;

Expand Down Expand Up @@ -99,9 +98,9 @@ public void testNextPrime() {
Assert.assertEquals(Integer.MAX_VALUE, Primes.nextPrime(Integer.MAX_VALUE - 1));
Assert.assertEquals(Integer.MAX_VALUE, Primes.nextPrime(Integer.MAX_VALUE));

assertNextPrimeException(Integer.MIN_VALUE, MessageFormat.format(Primes.NUMBER_TOO_SMALL,Integer.MIN_VALUE,0));
assertNextPrimeException(-1, MessageFormat.format(Primes.NUMBER_TOO_SMALL,-1,0));
assertNextPrimeException(-13, MessageFormat.format(Primes.NUMBER_TOO_SMALL,-13,0));
assertNextPrimeException(Integer.MIN_VALUE, Primes.numberTooSmallMessage(Integer.MIN_VALUE,0));
assertNextPrimeException(-1, Primes.numberTooSmallMessage(-1,0));
assertNextPrimeException(-13, Primes.numberTooSmallMessage(-13,0));
}

@Test
Expand Down Expand Up @@ -148,7 +147,7 @@ static void checkPrimeFactors(List<Integer> factors){
@Test
public void testPrimeFactors() throws Exception {
for (int i : BELOW_2) {
assertPrimeFactorsException(i, MessageFormat.format(Primes.NUMBER_TOO_SMALL,i,2));
assertPrimeFactorsException(i, Primes.numberTooSmallMessage(i,2));
}
for (int i : NOT_PRIMES) {
List<Integer> factors = Primes.primeFactors(i);
Expand Down
33 changes: 33 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
<maven.compiler.target>1.8</maven.compiler.target>
<numbers.pmd.version>3.9.0</numbers.pmd.version>
<numbers.checkstyle.version>3.0.0</numbers.checkstyle.version>
<numbers.forbiddenapis.version>2.6</numbers.forbiddenapis.version>
<numbers.mathjax.version>2.7.2</numbers.mathjax.version>
<!-- Workaround to avoid duplicating config files. -->
<numbers.parent.dir>${basedir}</numbers.parent.dir>
Expand Down Expand Up @@ -235,6 +236,18 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>de.thetaphi</groupId>
<artifactId>forbiddenapis</artifactId>
<executions>
<execution>
<goals>
<goal>check</goal>
<goal>testCheck</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
Expand Down Expand Up @@ -289,6 +302,26 @@
<resourceExcludes>NOTICE.txt,LICENSE.txt</resourceExcludes>
</configuration>
</plugin>
<plugin>
<groupId>de.thetaphi</groupId>
<artifactId>forbiddenapis</artifactId>
<version>${numbers.forbiddenapis.version}</version>
<configuration>
<bundledSignatures>
<!--
This will automatically choose the right
signatures based on 'maven.compiler.target':
-->
<bundledSignature>jdk-unsafe</bundledSignature>
<bundledSignature>jdk-deprecated</bundledSignature>
<!-- disallow undocumented classes like sun.misc.Unsafe: -->
<bundledSignature>jdk-non-portable</bundledSignature>
</bundledSignatures>
<signaturesFiles>
<signaturesFile>${numbers.parent.dir}/src/main/resources/forbidden-apis/forbidden-apis.txt</signaturesFile>
</signaturesFiles>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
Expand Down
Empty file.