Skip to content

Commit c813ac3

Browse files
authored
Merge pull request opentripplanner#7002 from entur/disable_translated_string_cache_in_updaters
Disable translated string cache in updaters
2 parents dc06ae1 + 0ded08b commit c813ac3

File tree

18 files changed

+67
-43
lines changed

18 files changed

+67
-43
lines changed

application/src/ext-test/java/org/opentripplanner/ext/vectortiles/layers/stops/StopsLayerTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public class StopsLayerTest {
2828
put("de", "nameDE");
2929
}
3030
},
31-
false,
3231
false
3332
);
3433
private static final I18NString DESC_TRANSLATIONS = TranslatedString.getI18NString(
@@ -38,7 +37,6 @@ public class StopsLayerTest {
3837
put("de", "descDE");
3938
}
4039
},
41-
false,
4240
false
4341
);
4442

application/src/ext-test/java/org/opentripplanner/ext/vectortiles/layers/vehicleparkings/VehicleParkingGroupsLayerTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public void setUp() {
4848
put("de", "groupDE");
4949
}
5050
},
51-
false,
5251
false
5352
)
5453
)
@@ -64,7 +63,6 @@ public void setUp() {
6463
put("de", "DE");
6564
}
6665
},
67-
false,
6866
false
6967
)
7068
)

application/src/ext-test/java/org/opentripplanner/ext/vectortiles/layers/vehicleparkings/VehicleParkingsLayerTest.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,7 @@ public void setUp() {
5959
vehicleParking = VehicleParking.builder()
6060
.id(ID)
6161
.name(
62-
TranslatedString.getI18NString(
63-
Map.of("", "default name", "de", "deutscher Name"),
64-
false,
65-
false
66-
)
62+
TranslatedString.getI18NString(Map.of("", "default name", "de", "deutscher Name"), false)
6763
)
6864
.coordinate(new WgsCoordinate(2, 1))
6965
.bicyclePlaces(true)

application/src/ext-test/java/org/opentripplanner/ext/vectortiles/layers/vehiclerental/mapper/VehicleRentalLayerTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public void stationWithTranslations() {
7979
put("de", germanName);
8080
}
8181
},
82-
false,
8382
false
8483
)
8584
)

application/src/ext/java/org/opentripplanner/ext/vehicleparking/liipi/LiipiHubToVehicleParkingGroupMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public Map<FeedScopedId, VehicleParkingGroup> parseHub(JsonNode jsonNode) {
5252
});
5353
I18NString name = translations.isEmpty()
5454
? new NonLocalizedString(hubId.getId())
55-
: TranslatedString.getI18NString(translations, true, false);
55+
: TranslatedString.getI18NString(translations, false);
5656
Geometry geometry = GEOMETRY_PARSER.geometryFromJson(jsonNode.path("location"));
5757
var vehicleParkingGroup = VehicleParkingGroup.of(hubId)
5858
.withName(name)

application/src/ext/java/org/opentripplanner/ext/vehicleparking/liipi/LiipiParkToVehicleParkingMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public VehicleParking parsePark(
9090
});
9191
I18NString name = translations.isEmpty()
9292
? new NonLocalizedString(vehicleParkId.getId())
93-
: TranslatedString.getI18NString(translations, true, false);
93+
: TranslatedString.getI18NString(translations, false);
9494
Geometry geometry = GEOMETRY_PARSER.geometryFromJson(jsonNode.path("location"));
9595

9696
var stateText = jsonNode.path("status").asText();

application/src/ext/java/org/opentripplanner/ext/vehicleparking/parkapi/ParkAPIUpdater.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected VehicleParking parseElement(JsonNode jsonNode) {
6666
var noteFiled = noteFieldIterator.next();
6767
noteLocalizations.put(noteFiled.getKey(), noteFiled.getValue().asText());
6868
}
69-
note = TranslatedString.getI18NString(noteLocalizations, true, false);
69+
note = TranslatedString.getI18NString(noteLocalizations, false);
7070
}
7171

7272
var vehicleParkId = createIdForNode(jsonNode);

application/src/main/java/org/opentripplanner/framework/i18n/TranslatedString.java

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,64 @@ public static I18NString getI18NString(String untranslated, String... translatio
4949
for (int i = 0; i < translations.length - 1; i += 2) {
5050
map.put(translations[i], translations[i + 1]);
5151
}
52-
return getI18NString(map, false, false);
52+
return getI18NString(map, false);
5353
}
5454

5555
/**
56-
* Gets an I18NString. If the translations only have a single value, return a NonTranslatedString,
56+
* Gets a deduplicated I18NString. If the translations only have a single value, return a
57+
* NonLocalizedString, otherwise a TranslatedString. The resulting I18NString is interned for
58+
* memory efficiency.
59+
* <p>
60+
* This should be used when calling this method during graph building. This should not be called
61+
* from a real-time updater as this is not thread-safe and may cause a memory leak.
62+
*
63+
* @param translations A Map of languages and translations, a null language is the default
64+
* translation
65+
* @param forceTranslatedString Should the language information be kept, even when only a single
66+
* translation is provided. This is useful when the language
67+
* information is important or is presented to the user.
68+
*/
69+
public static I18NString getDeduplicatedI18NString(
70+
Map<String, String> translations,
71+
boolean forceTranslatedString
72+
) {
73+
return getI18NString(translations, true, forceTranslatedString);
74+
}
75+
76+
/**
77+
* Gets a non-deduplicated I18NString. If the translations only have a single value, return a
78+
* NonLocalizedString, otherwise a TranslatedString. The resulting I18NString is NOT interned.
79+
* <p>
80+
* This should be used from real-time updaters to avoid memory leaks. For graph building, use
81+
* {@link #getDeduplicatedI18NString(Map, boolean)} instead.
82+
*
83+
* @param translations A Map of languages and translations, a null language is the default
84+
* translation
85+
* @param forceTranslatedString Should the language information be kept, even when only a single
86+
* translation is provided. This is useful when the language
87+
* information is important or is presented to the user.
88+
*/
89+
public static I18NString getI18NString(
90+
Map<String, String> translations,
91+
boolean forceTranslatedString
92+
) {
93+
return getI18NString(translations, false, forceTranslatedString);
94+
}
95+
96+
/**
97+
* Gets an I18NString. If the translations only have a single value, return a NonLocalizedString,
5798
* otherwise a TranslatedString
5899
*
59100
* @param translations A Map of languages and translations, a null language is the default
60101
* translation
61102
* @param intern Should the resulting I18NString be interned. This should be used when calling
62-
* this method during graph building, or when the string will be retained until the
63-
* instance is shut down, as it will cause a memory leak otherwise.
103+
* this method during graph building. This should not be called from a real-time
104+
* updater as this is not thread-safe and may cause a memory leak.
64105
* @param forceTranslatedString Should the language information be kept, even when only a single
65106
* translation is provided. This is useful when the language
66107
* information is important or is presented to the user.
67108
*/
68-
public static I18NString getI18NString(
109+
static I18NString getI18NString(
69110
Map<String, String> translations,
70111
boolean intern,
71112
boolean forceTranslatedString

application/src/main/java/org/opentripplanner/gtfs/mapping/TranslationHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ I18NString getTranslation(
157157
hm.put(translation.getLanguage(), translation.getTranslation());
158158
}
159159
}
160-
return TranslatedString.getI18NString(hm, true, false);
160+
return TranslatedString.getDeduplicatedI18NString(hm, false);
161161
}
162162
return NonLocalizedString.ofNullable(defaultValue);
163163
}

application/src/main/java/org/opentripplanner/netex/mapping/StationMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ private I18NString resolveName(StopPlace stopPlace) {
122122
}
123123
}
124124

125-
name = TranslatedString.getI18NString(translations, true, false);
125+
name = TranslatedString.getDeduplicatedI18NString(translations, false);
126126
} else {
127127
name = new NonLocalizedString(stopPlace.getName().getValue());
128128
}

0 commit comments

Comments
 (0)