Skip to content

Instantly share code, notes, and snippets.

@naturalwarren
Last active January 16, 2017 23:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naturalwarren/e52dac8ad3b5a54f53283b94cf971d38 to your computer and use it in GitHub Desktop.
Save naturalwarren/e52dac8ad3b5a54f53283b94cf971d38 to your computer and use it in GitHub Desktop.
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