Skip to content

Commit ebcafd2

Browse files
authored
Merge pull request osheroff#99 from janickr/master
Exposed initial capacity of StringBuilder through the JsonStringFormatter constructor
2 parents 798d8ad + c2ea428 commit ebcafd2

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

src/main/java/com/github/shyiko/mysql/binlog/event/deserialization/json/JsonStringFormatter.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,19 @@ public class JsonStringFormatter implements JsonFormatter {
6868

6969
private static final char[] HEX_CODES = "0123456789ABCDEF".toCharArray();
7070

71-
private final StringBuilder sb = new StringBuilder();
71+
private final StringBuilder sb;
72+
73+
public JsonStringFormatter() {
74+
this.sb = new StringBuilder();
75+
}
76+
77+
/**
78+
* Constructs a JsonFormatter with the given initial capacity.
79+
* @param capacity the initial capacity. Should be a positive number.
80+
*/
81+
public JsonStringFormatter(int capacity) {
82+
this.sb = new StringBuilder(capacity);
83+
}
7284

7385
@Override
7486
public String toString() {
@@ -115,12 +127,12 @@ public void value(String value) {
115127

116128
@Override
117129
public void value(int value) {
118-
sb.append(Integer.toString(value));
130+
sb.append(value);
119131
}
120132

121133
@Override
122134
public void value(long value) {
123-
sb.append(Long.toString(value));
135+
sb.append(value);
124136
}
125137

126138
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.github.shyiko.mysql.binlog.event.deserialization.json;
2+
3+
import org.testng.annotations.Test;
4+
5+
import static org.testng.Assert.assertEquals;
6+
7+
public class JsonStringFormatterTest {
8+
9+
@Test
10+
public void testFormatLong() {
11+
assertLong(1234567890L, "1234567890");
12+
assertLong(-1234567890L, "-1234567890");
13+
assertLong(0L, "0");
14+
assertLong(Long.MAX_VALUE, Long.toString(Long.MAX_VALUE));
15+
assertLong(Long.MIN_VALUE, Long.toString(Long.MIN_VALUE));
16+
}
17+
18+
@Test
19+
public void testFormatInt() {
20+
assertInt(1234567890, "1234567890");
21+
assertInt(-1234567890, "-1234567890");
22+
assertInt(0, "0");
23+
assertInt(Integer.MAX_VALUE, Integer.toString(Integer.MAX_VALUE));
24+
assertInt(Integer.MIN_VALUE, Integer.toString(Integer.MIN_VALUE));
25+
}
26+
27+
@Test
28+
public void testNewFormatterWithInitialCapacity() {
29+
JsonStringFormatter formatter = new JsonStringFormatter(32);
30+
formatter.value("test");
31+
assertEquals(formatter.getString(), "\"test\"");
32+
}
33+
34+
private static void assertLong(long value, String expected) {
35+
JsonStringFormatter json = new JsonStringFormatter();
36+
json.value(value);
37+
assertEquals(json.getString(), expected);
38+
}
39+
40+
private static void assertInt(int value, String expected) {
41+
JsonStringFormatter json = new JsonStringFormatter();
42+
json.value(value);
43+
assertEquals(json.getString(), expected);
44+
}
45+
}

0 commit comments

Comments
 (0)