Skip to content

Commit

Permalink
Remove TranslatedString from alertpatch
Browse files Browse the repository at this point in the history
  • Loading branch information
hannesj committed Feb 12, 2015
1 parent 8824a0c commit 9f25cb2
Show file tree
Hide file tree
Showing 13 changed files with 58 additions and 130 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/opentripplanner/api/model/Leg.java
Expand Up @@ -287,12 +287,12 @@ public void addAlert(Alert alert) {
if (alerts == null) {
alerts = new ArrayList<Alert>();
}
String text = alert.alertHeaderText.getSomeTranslation();
String text = alert.alertHeaderText.toString();
if (text == null) {
text = alert.alertDescriptionText.getSomeTranslation();
text = alert.alertDescriptionText.toString();
}
if (text == null) {
text = alert.alertUrl.getSomeTranslation();
text = alert.alertUrl.toString();
}
Note note = new Note(text);
if (!notes.contains(note)) {
Expand Down
Expand Up @@ -18,8 +18,8 @@ the License, or (props, at your option) any later version.
import org.opentripplanner.common.model.T2;
import org.opentripplanner.openstreetmap.model.OSMWithTags;
import org.opentripplanner.routing.alertpatch.Alert;
import org.opentripplanner.routing.alertpatch.TranslatedString;
import org.opentripplanner.routing.services.notes.NoteMatcher;
import org.opentripplanner.util.TranslatedString;

public class NoteProperties {

Expand All @@ -33,13 +33,9 @@ public NoteProperties(String notePattern, NoteMatcher noteMatcher) {
}

public T2<Alert, NoteMatcher> generateNote(OSMWithTags way) {
Map<String, String> noteText = TemplateLibrary.generateI18N(notePattern, way,
Alert.defaultLanguage);
Map<String, String> noteText = TemplateLibrary.generateI18N(notePattern, way);
Alert note = new Alert();
note.alertHeaderText = new TranslatedString();
for (Map.Entry<String, String> kv : noteText.entrySet()) {
note.alertHeaderText.addTranslation(kv.getKey(), kv.getValue());
}
note.alertHeaderText = TranslatedString.getI18NString(noteText);
return new T2<>(note, noteMatcher);
}
}
Expand Up @@ -56,19 +56,16 @@ public static String generate(String pattern, OSMWithTags way) {
* Tag names between {} are replaced by the OSM tag value, if it is present (or the empty
* string if not).
* @param way The way containing the tag values
* @param defaultLang The default lang to apply when no lang suffix are specified in OSM tags
* (usually the language of the country of OSM coverage)
* @return A map language code -> text, with at least one entry for the default language, and
* any other language found in OSM tag.
*/
public static Map<String, String> generateI18N(String pattern, OSMWithTags way,
String defaultLang) {
public static Map<String, String> generateI18N(String pattern, OSMWithTags way) {

if (pattern == null)
return null;

Map<String, StringBuffer> i18n = new HashMap<String, StringBuffer>();
i18n.put(defaultLang, new StringBuffer());
i18n.put(null, new StringBuffer());
Matcher matcher = patternMatcher.matcher(pattern);

int lastEnd = 0;
Expand All @@ -86,12 +83,15 @@ public static Map<String, String> generateI18N(String pattern, OSMWithTags way,
if (!kv.getKey().equals(defKey)) {
String lang = kv.getKey().substring(defKey.length() + 1);
if (!i18n.containsKey(lang))
i18n.put(lang, new StringBuffer(i18n.get(defaultLang)));
i18n.put(lang, new StringBuffer(i18n.get(null)));
}
}
}
// get the simple value (eg: description=...)
String defTag = way.getTag(defKey);
if (defTag == null && i18nTags != null && i18nTags.size() != 0) {
defTag = i18nTags.values().iterator().next();
}
// get the translated value, if exists
for (String lang : i18n.keySet()) {
String i18nTag = way.getTag(defKey + ":" + lang);
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/org/opentripplanner/routing/alertpatch/Alert.java
Expand Up @@ -13,6 +13,9 @@ the License, or (at your option) any later version.

package org.opentripplanner.routing.alertpatch;

import org.opentripplanner.util.I18NString;
import org.opentripplanner.util.NonLocalizedString;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
Expand All @@ -24,16 +27,14 @@ the License, or (at your option) any later version.
public class Alert implements Serializable {
private static final long serialVersionUID = 8305126586053909836L;

public static final String defaultLanguage = "en";

@XmlElement
public TranslatedString alertHeaderText;
public I18NString alertHeaderText;

@XmlElement
public TranslatedString alertDescriptionText;
public I18NString alertDescriptionText;

@XmlElement
public TranslatedString alertUrl;
public I18NString alertUrl;

//null means unknown
@XmlElement
Expand All @@ -48,8 +49,7 @@ public static HashSet<Alert> newSimpleAlertSet(String text) {

public static Alert createSimpleAlerts(String text) {
Alert note = new Alert();
note.alertHeaderText = new TranslatedString();
note.alertHeaderText.addTranslation(defaultLanguage, text);
note.alertHeaderText = new NonLocalizedString(text);
return note;
}

Expand Down Expand Up @@ -92,8 +92,8 @@ public int hashCode() {
@Override
public String toString() {
return "Alert('"
+ (alertHeaderText != null ? alertHeaderText.getSomeTranslation()
: alertDescriptionText != null ? alertDescriptionText.getSomeTranslation()
+ (alertHeaderText != null ? alertHeaderText.toString()
: alertDescriptionText != null ? alertDescriptionText.toString()
: "?") + "')";
}
}

This file was deleted.

Expand Up @@ -13,17 +13,15 @@ the License, or (at your option) any later version.

package org.opentripplanner.updater.alerts;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.*;

import org.onebusaway.gtfs.model.AgencyAndId;
import org.opentripplanner.routing.alertpatch.Alert;
import org.opentripplanner.routing.alertpatch.AlertPatch;
import org.opentripplanner.routing.alertpatch.TimePeriod;
import org.opentripplanner.routing.alertpatch.TranslatedString;
import org.opentripplanner.routing.services.AlertPatchService;
import org.opentripplanner.util.I18NString;
import org.opentripplanner.util.TranslatedString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -154,14 +152,14 @@ private String createId(String id, EntitySelector informed) {
*
* @return A TranslatedString containing the same information as the input
*/
private TranslatedString deBuffer(GtfsRealtime.TranslatedString input) {
TranslatedString result = new TranslatedString();
private I18NString deBuffer(GtfsRealtime.TranslatedString input) {
Map<String, String> translations = new HashMap<>();
for (GtfsRealtime.TranslatedString.Translation translation : input.getTranslationList()) {
String language = translation.getLanguage();
String string = translation.getText();
result.addTranslation(language, string);
translations.put(language, string);
}
return result;
return translations.isEmpty() ? null : TranslatedString.getI18NString(translations);
}

public void setDefaultAgencyId(String defaultAgencyId) {
Expand Down
Expand Up @@ -2,7 +2,8 @@

import org.opengis.feature.simple.SimpleFeature;
import org.opentripplanner.routing.alertpatch.Alert;
import org.opentripplanner.routing.alertpatch.TranslatedString;
import org.opentripplanner.util.NonLocalizedString;

import java.util.Date;

/**
Expand All @@ -24,7 +25,7 @@ public class WinkkiPollingGraphUpdater extends WFSNotePollingGraphUpdater {
protected Alert getNote(SimpleFeature feature) {
Alert alert = Alert.createSimpleAlerts("winkki:" + feature.getAttribute("licence_type"));
alert.alertDescriptionText = feature.getAttribute("event_description") == null ?
new TranslatedString("") : new TranslatedString(feature.getAttribute("event_description").toString());
new NonLocalizedString("") : new NonLocalizedString(feature.getAttribute("event_description").toString());
alert.effectiveStartDate = feature.getAttribute("licence_startdate") == null ?
(Date) feature.getAttribute("event_startdate") : (Date) feature.getAttribute("licence_startdate");
return alert;
Expand Down
Expand Up @@ -27,6 +27,11 @@ public NonLocalizedString(String name) {
this.name = name;
}

@Override
public boolean equals(Object other) {
return other instanceof NonLocalizedString && this.name.equals(((NonLocalizedString)other).name);
}

@Override
public String toString() {
return this.name;
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/org/opentripplanner/util/TranslatedString.java
Expand Up @@ -26,6 +26,11 @@ private TranslatedString(Map<String, String> translations) {
}
}

@Override
public boolean equals(Object other) {
return (other instanceof TranslatedString) && this.translations.equals(((TranslatedString)other).translations);
}

/**
* Gets an interned I18NString.
* If the translations only have a single value, return a NonTranslatedString, otherwise a TranslatedString
Expand Down Expand Up @@ -54,7 +59,7 @@ public static I18NString getI18NString(Map<String, String> translations) {
*/
@Override
public String toString() {
return translations.get(null);
return translations.containsKey(null) ? translations.get(null) : translations.values().iterator().next();
}

/**
Expand All @@ -67,6 +72,6 @@ public String toString(Locale locale) {
if (locale != null) {
language = locale.getLanguage().toLowerCase();
}
return translations.containsKey(language) ? translations.get(language) : translations.get(null);
return translations.containsKey(language) ? translations.get(language) : toString();
}
}
2 changes: 1 addition & 1 deletion src/test/java/org/opentripplanner/GtfsTest.java
Expand Up @@ -179,7 +179,7 @@ public void validateLeg(Leg leg, long startTime, long endTime, String toStopId,
if (alert != null) {
assertNotNull(leg.alerts);
assertEquals(1, leg.alerts.size());
assertEquals(alert, leg.alerts.get(0).alertHeaderText.getSomeTranslation());
assertEquals(alert, leg.alerts.get(0).alertHeaderText.toString());
} else {
assertNull(leg.alerts);
}
Expand Down
Expand Up @@ -1364,7 +1364,7 @@ private void compareSteps(WalkStep[][] steps, Type type) {
* the arctic regions. Of course, longitude becomes meaningless at the poles themselves, but
* walking towards the pole, past it, and then back again will now yield correct results.
*/
assertEquals(alertsExample, steps[7][0].alerts.get(0).alertHeaderText.getSomeTranslation());
assertEquals(alertsExample, steps[7][0].alerts.get(0).alertHeaderText.toString());
assertEquals(AbsoluteDirection.SOUTH, steps[7][0].absoluteDirection);
assertEquals(RelativeDirection.CONTINUE, steps[7][0].relativeDirection);
assertEquals(SOUTH, steps[7][0].angle, EPSILON);
Expand Down
Expand Up @@ -283,10 +283,10 @@ public void testAlerts() throws Exception {

assertNotNull(step0.alerts);
assertEquals(1, step0.alerts.size());
assertEquals("SE", step0.alerts.get(0).alertHeaderText.getSomeTranslation());
assertEquals("SE", step0.alerts.get(0).alertHeaderText.toString());

assertEquals(1, step1.alerts.size());
assertEquals("NE", step1.alerts.get(0).alertHeaderText.getSomeTranslation());
assertEquals("NE", step1.alerts.get(0).alertHeaderText.toString());
}

public void testIntermediate() throws Exception {
Expand Down
Expand Up @@ -41,34 +41,32 @@ public void testTemplate() {
assertEquals("Wheelchair note: Wheelchair description EN",
TemplateLibrary.generate("Wheelchair note: {wheelchair:description}", osmTags));

assertEquals(null, TemplateLibrary.generateI18N(null, osmTags, "en"));
assertEquals(null, TemplateLibrary.generateI18N(null, osmTags));
Map<String, String> expected = new HashMap<>();

expected.put("en", "");
assertEquals(expected, TemplateLibrary.generateI18N("", osmTags, "en"));
expected.put(null, "");
assertEquals(expected, TemplateLibrary.generateI18N("", osmTags));

expected.clear();
expected.put("en", "Note: Note EN");
assertEquals(expected, TemplateLibrary.generateI18N("Note: {note}", osmTags, "en"));

expected.clear();
expected.put("fr", "Note: Note EN");
assertEquals(expected, TemplateLibrary.generateI18N("Note: {note}", osmTags, "fr"));
expected.put(null, "Note: Note EN");
assertEquals(expected, TemplateLibrary.generateI18N("Note: {note}", osmTags));

expected.clear();
expected.put(null, "Desc: Description FR");
expected.put("fr", "Desc: Description FR");
assertEquals(expected, TemplateLibrary.generateI18N("Desc: {description}", osmTags, "fr"));
assertEquals(expected, TemplateLibrary.generateI18N("Desc: {description}", osmTags));

expected.clear();
expected.put("en", "Note: Note EN, Wheelchair description EN");
expected.put(null, "Note: Note EN, Wheelchair description EN");
expected.put("fr", "Note: Note EN, Wheelchair description FR");
assertEquals(expected, TemplateLibrary.generateI18N(
"Note: {note}, {wheelchair:description}", osmTags, "en"));
"Note: {note}, {wheelchair:description}", osmTags));

expected.clear();
expected.put(null, "Note: Note EN, Wheelchair description EN, ");
expected.put("fr", "Note: Note EN, Wheelchair description FR, ");
assertEquals(expected, TemplateLibrary.generateI18N(
"Note: {note}, {wheelchair:description}, {foobar:description}", osmTags, "fr"));
"Note: {note}, {wheelchair:description}, {foobar:description}", osmTags));

}

Expand Down

0 comments on commit 9f25cb2

Please sign in to comment.