Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mutable Models Bug
/**
* Provides a stream of vehicles that have a price.
*/
public class PricedVehicles {
private Observable<List<Vehicle>> pricedVehicles;
public PricedVehicles(VehicleStream vehicleStream, PricingStream pricingStream) {
this.pricedVehicles = Observable.combineLatest(
vehicleStream.vehicles(),
pricingStream.prices(),
PricedVehicles::filterVehiclesWithoutPrice);
}
/**
* @return a stream that emits lists of priced vehicles.
*/
public Observable<List<Vehicle>> stream() {
return pricedVehicles;
}
/**
* Removes vehicles that don't have a price in the prices map.
* @param vehicles the list of vehicles that are available.
* @param prices a map containing the prices of vehicles.
*/
private static List<Vehicle> filterVehiclesWithoutPrice(
List<Vehicle> vehicles,
Map<VehicleId, Price> prices) {
List<Vehicle> vehiclesToRemove = new ArrayList<>();
for(Vehicle vehicle : vehicles) {
if (prices.get(vehicle.id()) == null) {
vehiclesToRemove.add(vehicle);
}
}
vehicles.removeAll(vehiclesToRemove);
return vehicles;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment