Skip to content

Commit

Permalink
Better way to localize BikeRental
Browse files Browse the repository at this point in the history
New objects aren't created, just BikeRentalStation is cloned and locale
set.
  • Loading branch information
buma committed May 18, 2015
1 parent c941c1e commit a90eaeb
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 105 deletions.
Expand Up @@ -34,7 +34,6 @@ the License, or (at your option) any later version.
import org.opentripplanner.standalone.Router; import org.opentripplanner.standalone.Router;


import com.vividsolutions.jts.geom.Envelope; import com.vividsolutions.jts.geom.Envelope;
import org.opentripplanner.routing.bike_rental.TranslatedBikeRentalStation;
import org.opentripplanner.util.ResourceBundleSingleton; import org.opentripplanner.util.ResourceBundleSingleton;


@Path("/routers/{routerId}/bike_rental") @Path("/routers/{routerId}/bike_rental")
Expand Down Expand Up @@ -65,11 +64,12 @@ public BikeRentalStationList getBikeRentalStations(
envelope = new Envelope(-180,180,-90,90); envelope = new Envelope(-180,180,-90,90);
} }
Collection<BikeRentalStation> stations = bikeRentalService.getBikeRentalStations(); Collection<BikeRentalStation> stations = bikeRentalService.getBikeRentalStations();
List<TranslatedBikeRentalStation> out = new ArrayList<TranslatedBikeRentalStation>(); List<BikeRentalStation> out = new ArrayList<>();
for (BikeRentalStation station : stations) { for (BikeRentalStation station : stations) {
if (envelope.contains(station.x, station.y)) { if (envelope.contains(station.x, station.y)) {
TranslatedBikeRentalStation translatedBikeRentalStation = new TranslatedBikeRentalStation(station, locale); BikeRentalStation station_localized = station.clone();
out.add(translatedBikeRentalStation); station_localized.locale = locale;
out.add(station_localized);
} }
} }
BikeRentalStationList brsl = new BikeRentalStationList(); BikeRentalStationList brsl = new BikeRentalStationList();
Expand Down
Expand Up @@ -20,10 +20,10 @@ the License, or (at your option) any later version.
import javax.xml.bind.annotation.XmlElements; import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;


import org.opentripplanner.routing.bike_rental.TranslatedBikeRentalStation; import org.opentripplanner.routing.bike_rental.BikeRentalStation;


@XmlRootElement(name="BikeRentalStationList") @XmlRootElement(name="BikeRentalStationList")
public class BikeRentalStationList { public class BikeRentalStationList {
@XmlElements(value = { @XmlElement(name="station") }) @XmlElements(value = { @XmlElement(name="station") })
public List<TranslatedBikeRentalStation> stations = new ArrayList<TranslatedBikeRentalStation>(); public List<BikeRentalStation> stations = new ArrayList<BikeRentalStation>();
} }
Expand Up @@ -23,8 +23,9 @@ the License, or (at your option) any later version.


import com.fasterxml.jackson.databind.annotation.JsonSerialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.opentripplanner.util.I18NString; import org.opentripplanner.util.I18NString;
import org.opentripplanner.util.ResourceBundleSingleton;


public class BikeRentalStation implements Serializable { public class BikeRentalStation implements Serializable, Cloneable {
private static final long serialVersionUID = 8311460609708089384L; private static final long serialVersionUID = 8311460609708089384L;


@XmlAttribute @XmlAttribute
Expand Down Expand Up @@ -62,6 +63,22 @@ public class BikeRentalStation implements Serializable {
@JsonSerialize @JsonSerialize
public boolean realTimeData = true; public boolean realTimeData = true;


/**
* This is used for localization. Currently "bike rental station" isn't part of the name.
* It can be added on the client. But since it is used as Station: name, and Recommended Pick Up: name.
* It isn't used.
*
* Names can be different in different languages if name tags in OSM have language tags.
*
* It is set in {@link org.opentripplanner.api.resource.BikeRental} from URL parameter.
*
* Sets default locale on start
*
*/
@JsonIgnore
@XmlTransient
public Locale locale = ResourceBundleSingleton.INSTANCE.getLocale(null);

public boolean equals(Object o) { public boolean equals(Object o) {
if (!(o instanceof BikeRentalStation)) { if (!(o instanceof BikeRentalStation)) {
return false; return false;
Expand All @@ -77,4 +94,22 @@ public int hashCode() {
public String toString () { public String toString () {
return String.format(Locale.US, "Bike rental station %s at %.6f, %.6f", name, y, x); return String.format(Locale.US, "Bike rental station %s at %.6f, %.6f", name, y, x);
} }

@Override
public BikeRentalStation clone() {
try {
return (BikeRentalStation) super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError(); //can't happen
}
}

/**
* Gets translated name of bike rental station based on locale
*/
@XmlAttribute
@JsonSerialize
public String getName() {
return name.toString(locale);
}
} }

This file was deleted.

0 comments on commit a90eaeb

Please sign in to comment.