Hafas: Parse products for stations.

This commit is contained in:
Andreas Schildbach 2015-09-04 16:21:13 +02:00
parent fb6e71ee2b
commit 27f60db22b
2 changed files with 34 additions and 3 deletions

View file

@ -289,6 +289,26 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider
return product;
}
protected final Set<Product> intToProducts(int value)
{
final int allProductsInt = allProductsInt();
checkArgument(value <= allProductsInt, "value " + value + " cannot be greater than " + allProductsInt);
final Set<Product> products = EnumSet.noneOf(Product.class);
for (int i = productsMap.length - 1; i >= 0; i--)
{
final int v = 1 << i;
if (value >= v)
{
final Product product = checkNotNull(productsMap[i], "unknown product " + i);
products.add(product);
value -= v;
}
}
checkState(value == 0);
return products;
}
protected static final Pattern P_SPLIT_NAME_FIRST_COMMA = Pattern.compile("([^,]*), (.*)");
protected static final Pattern P_SPLIT_NAME_LAST_COMMA = Pattern.compile("(.*), ([^,]*)");
protected static final Pattern P_SPLIT_NAME_PAREN = Pattern.compile("(.*) \\((.{3,}?)\\)");
@ -2481,12 +2501,14 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider
final String urlname = ParserUtils.urlDecode(stop.getString("urlname"), jsonNearbyLocationsEncoding);
final int lat = stop.getInt("y");
final int lon = stop.getInt("x");
final int prodclass = stop.optInt("prodclass", -1);
final int stopWeight = stop.optInt("stopweight", -1);
if (stopWeight != 0)
{
final String[] placeAndName = splitStationName(urlname);
locations.add(new Location(LocationType.STATION, id, lat, lon, placeAndName[0], placeAndName[1]));
final Set<Product> products = prodclass != -1 ? intToProducts(prodclass) : null;
locations.add(new Location(LocationType.STATION, id, lat, lon, placeAndName[0], placeAndName[1], products));
}
}
}

View file

@ -22,6 +22,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Set;
import javax.annotation.Nullable;
@ -42,8 +43,10 @@ public final class Location implements Serializable
public final int lat, lon;
public final @Nullable String place;
public final @Nullable String name;
public final @Nullable Set<Product> products;
public Location(final LocationType type, final String id, final int lat, final int lon, final String place, final String name)
public Location(final LocationType type, final String id, final int lat, final int lon, final String place, final String name,
final Set<Product> products)
{
this.type = checkNotNull(type);
this.id = id;
@ -51,6 +54,7 @@ public final class Location implements Serializable
this.lon = lon;
this.place = place;
this.name = name;
this.products = products;
checkArgument(id == null || id.length() > 0, "ID cannot be the empty string");
checkArgument(place == null || name != null, "place '%s' without name cannot exist", place);
@ -61,6 +65,11 @@ public final class Location implements Serializable
}
}
public Location(final LocationType type, final String id, final int lat, final int lon, final String place, final String name)
{
this(type, id, lat, lon, place, name, null);
}
public Location(final LocationType type, final String id, final Point coord, final String place, final String name)
{
this(type, id, coord != null ? coord.lat : 0, coord != null ? coord.lon : 0, place, name);
@ -182,6 +191,6 @@ public final class Location implements Serializable
final ToStringHelper helper = MoreObjects.toStringHelper(this).addValue(type).addValue(id);
if (hasLocation())
helper.addValue(lat + "/" + lon);
return helper.add("place", place).add("name", name).omitNullValues().toString();
return helper.add("place", place).add("name", name).add("products", products).omitNullValues().toString();
}
}