@Test public void testCalcLaneNumber2() { int[] count ={0}; double[] speeds = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; Integer[] expectedCountByLane = {4, 4, 4}; TrafficDensity3 trafficDensity = new TrafficDensity3(){ @Override protected double calcSpeed(Vehicle vehicle, double traction, double timeSec) { return speeds[count[0]++]; } }; double timeSec = 10.0; int trafficUnitsNumber = speeds.length; double[] speedLimitByLane = {4.5, 8.5, 12.5}; Integer[] trafficByLane = trafficDensity.trafficByLane(getTrafficUnitStream(trafficUnitsNumber), trafficUnitsNumber, timeSec, FactorySpeedModel.getSpeedModel(), speedLimitByLane ); assertArrayEquals("Assert count of " + speeds.length + " vehicles by " + speedLimitByLane.length + " lanes with speed limit " + Arrays.stream(speedLimitByLane).mapToObj(Double::toString).collect(Collectors.joining(", ")), expectedCountByLane, trafficByLane); }
public Integer[] trafficByLane(Stream<TrafficUnit> stream, int trafficUnitsNumber, double timeSec, SpeedModel speedModel, double[] speedLimitByLane) { int lanesCount = speedLimitByLane.length; Map<Integer, Integer> trafficByLane = new HashMap<>(); for(int i = 1; i <= lanesCount; i++){ trafficByLane.put(i, 0); } stream.limit(trafficUnitsNumber) .map(TrafficUnitWrapper::new) .map(tuw -> tuw.setSpeedModel(speedModel)) .map(tuw -> calcSpeed(tuw.getVehicle(), tuw.getTraction(), timeSec)) .forEach(speed -> trafficByLane.computeIfPresent(calcLaneNumber(lanesCount, speedLimitByLane, speed), (k, v) -> ++v)); return trafficByLane.values().toArray(new Integer[lanesCount]); }
TrafficDensity3 { public Integer[] trafficByLane(Stream<TrafficUnit> stream, int trafficUnitsNumber, double timeSec, SpeedModel speedModel, double[] speedLimitByLane) { int lanesCount = speedLimitByLane.length; Map<Integer, Integer> trafficByLane = new HashMap<>(); for(int i = 1; i <= lanesCount; i++){ trafficByLane.put(i, 0); } stream.limit(trafficUnitsNumber) .map(TrafficUnitWrapper::new) .map(tuw -> tuw.setSpeedModel(speedModel)) .map(tuw -> calcSpeed(tuw.getVehicle(), tuw.getTraction(), timeSec)) .forEach(speed -> trafficByLane.computeIfPresent(calcLaneNumber(lanesCount, speedLimitByLane, speed), (k, v) -> ++v)); return trafficByLane.values().toArray(new Integer[lanesCount]); } }
TrafficDensity3 { public Integer[] trafficByLane(Stream<TrafficUnit> stream, int trafficUnitsNumber, double timeSec, SpeedModel speedModel, double[] speedLimitByLane) { int lanesCount = speedLimitByLane.length; Map<Integer, Integer> trafficByLane = new HashMap<>(); for(int i = 1; i <= lanesCount; i++){ trafficByLane.put(i, 0); } stream.limit(trafficUnitsNumber) .map(TrafficUnitWrapper::new) .map(tuw -> tuw.setSpeedModel(speedModel)) .map(tuw -> calcSpeed(tuw.getVehicle(), tuw.getTraction(), timeSec)) .forEach(speed -> trafficByLane.computeIfPresent(calcLaneNumber(lanesCount, speedLimitByLane, speed), (k, v) -> ++v)); return trafficByLane.values().toArray(new Integer[lanesCount]); }  }
TrafficDensity3 { public Integer[] trafficByLane(Stream<TrafficUnit> stream, int trafficUnitsNumber, double timeSec, SpeedModel speedModel, double[] speedLimitByLane) { int lanesCount = speedLimitByLane.length; Map<Integer, Integer> trafficByLane = new HashMap<>(); for(int i = 1; i <= lanesCount; i++){ trafficByLane.put(i, 0); } stream.limit(trafficUnitsNumber) .map(TrafficUnitWrapper::new) .map(tuw -> tuw.setSpeedModel(speedModel)) .map(tuw -> calcSpeed(tuw.getVehicle(), tuw.getTraction(), timeSec)) .forEach(speed -> trafficByLane.computeIfPresent(calcLaneNumber(lanesCount, speedLimitByLane, speed), (k, v) -> ++v)); return trafficByLane.values().toArray(new Integer[lanesCount]); }  Integer[] trafficByLane(Stream<TrafficUnit> stream, int trafficUnitsNumber, double timeSec,
                                   SpeedModel speedModel, double[] speedLimitByLane); }
TrafficDensity3 { public Integer[] trafficByLane(Stream<TrafficUnit> stream, int trafficUnitsNumber, double timeSec, SpeedModel speedModel, double[] speedLimitByLane) { int lanesCount = speedLimitByLane.length; Map<Integer, Integer> trafficByLane = new HashMap<>(); for(int i = 1; i <= lanesCount; i++){ trafficByLane.put(i, 0); } stream.limit(trafficUnitsNumber) .map(TrafficUnitWrapper::new) .map(tuw -> tuw.setSpeedModel(speedModel)) .map(tuw -> calcSpeed(tuw.getVehicle(), tuw.getTraction(), timeSec)) .forEach(speed -> trafficByLane.computeIfPresent(calcLaneNumber(lanesCount, speedLimitByLane, speed), (k, v) -> ++v)); return trafficByLane.values().toArray(new Integer[lanesCount]); }  Integer[] trafficByLane(Stream<TrafficUnit> stream, int trafficUnitsNumber, double timeSec,
                                   SpeedModel speedModel, double[] speedLimitByLane);  }