Skip to content

Commit b881ae2

Browse files
Optimise performance by instantiating context only once
1 parent 727ce56 commit b881ae2

File tree

4 files changed

+22
-16
lines changed

4 files changed

+22
-16
lines changed

application/src/ext-test/java/org/opentripplanner/ext/ojp/trias/OjpMapperTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,6 @@ void ojpToTrias() {
109109
RESOLVE_FEED_LANG
110110
);
111111
var ojp = mapper.mapCalls(List.of(new CallAtStop(TRIP_TIMES_ON_DATE, WALK_TIME)), timestamp);
112-
OjpToTriasTransformer.transform(ojp, new PrintWriter(System.out));
112+
OjpToTriasTransformer.ojpToTrias(ojp, new PrintWriter(System.out));
113113
}
114114
}

application/src/ext-test/java/org/opentripplanner/ext/ojp/trias/OjpToTriasTransformerTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void stopEventRequest(String name) throws JAXBException, TransformerException {
3838
@Test
3939
void error() {
4040
var ojp = ErrorMapper.error("An error occurred", ZDT);
41-
var actual = OjpToTriasTransformer.transform(ojp);
41+
var actual = OjpToTriasTransformer.ojpToTrias(ojp);
4242
var file = LOADER.extTestResourceFile("error.xml");
4343
var original = readFile(file);
4444
writeFile(file, actual);

application/src/ext/java/org/opentripplanner/ext/ojp/trias/OjpToTriasTransformer.java

+17-10
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,31 @@ class OjpToTriasTransformer {
2929
"trias_to_ojp2.0_request.xslt"
3030
);
3131

32-
static String transform(OJP ojp) {
32+
private static final JAXBContext CONTEXT = jaxbContext();
33+
34+
static String ojpToTrias(OJP ojp) {
3335
var writer = new StringWriter();
34-
transform(ojp, writer);
36+
ojpToTrias(ojp, writer);
3537
return writer.toString();
3638
}
3739

38-
static void transform(OJP ojp, Writer writer) {
40+
static void ojpToTrias(OJP ojp, Writer writer) {
3941
try {
40-
var context = JAXBContext.newInstance(OJP.class);
41-
var marshaller = context.createMarshaller();
42+
var marshaller = CONTEXT.createMarshaller();
4243

4344
// Convert Java object to XML string
4445
var outputStream = new ByteArrayOutputStream();
4546
marshaller.marshal(ojp, outputStream);
4647

4748
var xmlSource = new StreamSource(new ByteArrayInputStream(outputStream.toByteArray()));
4849

49-
transform(writer, xmlSource);
50+
ojpToTrias(writer, xmlSource);
5051
} catch (IOException | JAXBException | TransformerException e) {
5152
throw new RuntimeException(e);
5253
}
5354
}
5455

5556
static OJP triasToOjp(String trias) throws JAXBException, TransformerException {
56-
var context = JAXBContext.newInstance(OJP.class);
57-
5857
var xmlSource = new StreamSource(
5958
new ByteArrayInputStream(trias.getBytes(StandardCharsets.UTF_8))
6059
);
@@ -64,13 +63,13 @@ static OJP triasToOjp(String trias) throws JAXBException, TransformerException {
6463
transformer.transform(xmlSource, new StreamResult(writer));
6564
var transformedXml = writer.toString(StandardCharsets.UTF_8);
6665

67-
var unmarshaller = context.createUnmarshaller();
66+
var unmarshaller = CONTEXT.createUnmarshaller();
6867
return (OJP) unmarshaller.unmarshal(
6968
new ByteArrayInputStream(transformedXml.getBytes(StandardCharsets.UTF_8))
7069
);
7170
}
7271

73-
static void transform(Writer writer, StreamSource xmlSource)
72+
static void ojpToTrias(Writer writer, StreamSource xmlSource)
7473
throws IOException, TransformerException {
7574
var transformer = OJP_TO_TRIAS_TEMPLATE.newTransformer();
7675
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
@@ -87,4 +86,12 @@ static Templates loadTemplate(String name) {
8786
throw new RuntimeException(e);
8887
}
8988
}
89+
90+
private static JAXBContext jaxbContext() {
91+
try {
92+
return JAXBContext.newInstance(OJP.class);
93+
} catch (JAXBException e) {
94+
throw new RuntimeException(e);
95+
}
96+
}
9097
}

application/src/ext/java/org/opentripplanner/ext/ojp/trias/TriasResource.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,11 @@ private static Response error(String value) {
107107
}
108108

109109
private static StreamingOutput ojpToTrias(OJP ojpOutput) {
110-
StreamingOutput stream = os -> {
111-
Writer writer = new BufferedWriter(new OutputStreamWriter(os));
112-
OjpToTriasTransformer.transform(ojpOutput, writer);
110+
return os -> {
111+
Writer writer = new OutputStreamWriter(os);
112+
OjpToTriasTransformer.ojpToTrias(ojpOutput, writer);
113113
writer.flush();
114114
};
115-
return stream;
116115
}
117116

118117
@GET

0 commit comments

Comments
 (0)