Skip to content

Commit eb43fcc

Browse files
author
Clifford W. Johnson
committed
TAB-9567 Remove unsynchronized DecimalFormat
1 parent b6314bc commit eb43fcc

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

src/main/java/com/tc/util/Conversion.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,13 @@
1212
import com.tc.exception.TCRuntimeException;
1313

1414
import java.io.UnsupportedEncodingException;
15-
import java.text.DecimalFormat;
16-
import java.text.DecimalFormatSymbols;
1715
import java.util.regex.Matcher;
1816
import java.util.regex.Pattern;
1917

2018
/**
2119
* Data conversion algorithms and whatnot can be found in java.io.DataInput and java.io.DataOutput. Contains methods for
2220
* converting from one kind of thing to another.
23-
*
21+
*
2422
* @author orion
2523
*/
2624
public class Conversion {
@@ -30,11 +28,7 @@ public class Conversion {
3028

3129
private static final Pattern MEMORY_SIZE_PATTERN = Pattern.compile("[0-9]*([.][0-9]+)? *([bkmg])?");
3230
private static final Pattern SIZE_MODIFIER_PATTERN = Pattern.compile("[bkmg]");
33-
private final static DecimalFormat twoDForm = new DecimalFormat();
3431
private static final byte[] EMPTY_BYTE_ARRAY = new byte[] {};
35-
static {
36-
twoDForm.applyLocalizedPattern("#" + new DecimalFormatSymbols().getDecimalSeparator() + "##");
37-
}
3832

3933
private static int makeInt(byte b3, byte b2, byte b1, byte b0) {
4034
return ((((b3 & 0xff) << 24) | ((b2 & 0xff) << 16) | ((b1 & 0xff) << 8) | ((b0 & 0xff) << 0)));
@@ -109,7 +103,7 @@ public static long bytes2uint(byte b[], int offset, int length) {
109103

110104
/**
111105
* Helper method to write a 4 byte unsigned integer value into a given byte array at a given offset
112-
*
106+
*
113107
* @param l the unsigned int value to write
114108
* @param dest the byte array to write the uint into
115109
* @param index starting offset into the destination byte array
@@ -127,7 +121,7 @@ public static void writeUint(long l, byte[] dest, int index) {
127121

128122
/**
129123
* Helper method to write a 4 byte java (signed) integer value into a given byte array at a given offset
130-
*
124+
*
131125
* @param i the signed int value to write
132126
* @param dest the byte array to write the uint into
133127
* @param index starting offset into the destination byte array
@@ -313,7 +307,7 @@ public static String bytesToHex(byte[] b) {
313307

314308
/**
315309
* Converts a single byte to a hex string representation, can be decoded with Byte.parseByte().
316-
*
310+
*
317311
* @param b the byte to encode
318312
* @return a
319313
*/
@@ -420,13 +414,13 @@ public static String memoryBytesAsSize(final long bytes) throws NumberFormatExce
420414
return bytes + "b";
421415
} else if (bytes < MEGA.asBytes()) {
422416
double rv = (bytes / (KILO.asBytes() * 1.0));
423-
return twoDForm.format(rv) + 'k';
417+
return String.format("%.2fk", rv);
424418
} else if (bytes < GIGA.asBytes()) {
425419
double rv = (bytes / (MEGA.asBytes() * 1.0));
426-
return twoDForm.format(rv) + 'm';
420+
return String.format("%.2fm", rv);
427421
} else {
428422
double rv = (bytes / (GIGA.asBytes() * 1.0));
429-
return twoDForm.format(rv) + 'g';
423+
return String.format("%.2fg", rv);
430424
}
431425
}
432426

src/test/java/com/tc/util/ConversionTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* JUnit TestCase to exercise the Conversion class.
2424
* <p>
2525
* TODO: change tests to use min and max values where appropriate.
26-
*
26+
*
2727
* @see Conversion
2828
* @author orion
2929
*/
@@ -294,16 +294,16 @@ public void testMemoryBytesAsSize() {
294294

295295
char dfs = new DecimalFormatSymbols().getDecimalSeparator();
296296
try {
297-
Assert.assertEquals("1k", Conversion.memoryBytesAsSize(MemorySizeUnits.KILO.asBytes()));
298-
Assert.assertEquals("1m", Conversion.memoryBytesAsSize(MemorySizeUnits.MEGA.asBytes()));
299-
Assert.assertEquals("1g", Conversion.memoryBytesAsSize(MemorySizeUnits.GIGA.asBytes()));
297+
Assert.assertEquals("1" + dfs + "00k", Conversion.memoryBytesAsSize(MemorySizeUnits.KILO.asBytes()));
298+
Assert.assertEquals("1" + dfs + "00m", Conversion.memoryBytesAsSize(MemorySizeUnits.MEGA.asBytes()));
299+
Assert.assertEquals("1" + dfs + "00g", Conversion.memoryBytesAsSize(MemorySizeUnits.GIGA.asBytes()));
300300

301-
Assert.assertEquals("4k", Conversion.memoryBytesAsSize(MemorySizeUnits.KILO.asBytes() * 4));
302-
Assert.assertEquals("8m", Conversion.memoryBytesAsSize(MemorySizeUnits.MEGA.asBytes() * 8));
303-
Assert.assertEquals("10g", Conversion.memoryBytesAsSize(MemorySizeUnits.GIGA.asBytes() * 10));
301+
Assert.assertEquals("4" + dfs + "00k", Conversion.memoryBytesAsSize(MemorySizeUnits.KILO.asBytes() * 4));
302+
Assert.assertEquals("8" + dfs + "00m", Conversion.memoryBytesAsSize(MemorySizeUnits.MEGA.asBytes() * 8));
303+
Assert.assertEquals("10" + dfs + "00g", Conversion.memoryBytesAsSize(MemorySizeUnits.GIGA.asBytes() * 10));
304304

305305
Assert.assertEquals("924b", Conversion.memoryBytesAsSize(MemorySizeUnits.KILO.asBytes() - 100));
306-
Assert.assertEquals("1024m", Conversion.memoryBytesAsSize(MemorySizeUnits.GIGA.asBytes() - 100));
306+
Assert.assertEquals("1024" + dfs + "00m", Conversion.memoryBytesAsSize(MemorySizeUnits.GIGA.asBytes() - 100));
307307
Assert.assertEquals("901b", Conversion.memoryBytesAsSize(MemorySizeUnits.KILO.asBytes() - 123));
308308
Assert.assertEquals("1021" + dfs + "71k", Conversion.memoryBytesAsSize(MemorySizeUnits.MEGA.asBytes() - 2344));
309309
Assert.assertEquals("933" + dfs + "84m",

0 commit comments

Comments
 (0)