-
-
Save naturalwarren/e52dac8ad3b5a54f53283b94cf971d38 to your computer and use it in GitHub Desktop.
Mutable Models Bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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