VehicleLocation { public void update(Date recordedAt, MonitoredVehicleJourney mvj, PausedLocationListener listener) { if (_lastUpdateTime != null && _lastUpdateTime.after(recordedAt)) { _log.warn("time out of order: old=" + _lastUpdateTime + " new=" + recordedAt); return; } if (isStale(recordedAt.getTime())) { clear(listener); } long updateIndex = _lastUpdateIndex + 1; processTripRefChange(recordedAt, updateIndex, mvj, listener); LocationStructure location = mvj.getVehicleLocation(); CoordinatePoint p = new CoordinatePoint( location.getLatitude().doubleValue(), location.getLongitude().doubleValue()); if (!isClustered(p)) { processPausedLocationIfApplicable(listener); _lastLocationUpdateTime = recordedAt; _lastLocationUpdateIndex = updateIndex; _location = p; _mergedLocations.clear(); _lineRef = mvj.getLineRef().getValue(); } _mergedLocations.add(p); _lastUpdateTime = recordedAt; _lastUpdateIndex = updateIndex; if (_lastUpdateIndex < _lastLocationUpdateIndex || _lastUpdateIndex < _tripRefUpdateIndex) { throw new IllegalStateException(); } } VehicleLocation(String vehicleRef); String getVehicleRef(); CoordinatePoint getLocation(); Date getLastLocationUpdateTime(); String getLineRef(); boolean isStale(long currentTime); boolean isPaused(); void update(Date recordedAt, MonitoredVehicleJourney mvj,
      PausedLocationListener listener); CoordinatePoint getLocationCentroid();  }
@Test public void testUpdate() { VehicleLocation location = new VehicleLocation(VEHICLE_REF); assertFalse(location.isPaused()); location.update(mins(0), mvj("t0", 47.668523, -122.290170), _locationListener); assertFalse(location.isPaused()); assertEquals(LINE_REF, location.getLineRef()); assertEquals(mins(0), location.getLastLocationUpdateTime()); location.update(mins(4), mvj("t0", 47.668523, -122.290170), _locationListener); assertFalse(location.isPaused()); assertEquals(mins(0), location.getLastLocationUpdateTime()); location.update(mins(6), mvj("t0", 47.668523, -122.290170), _locationListener); assertTrue(location.isPaused()); location.update(mins(7), mvj("t0", 47.668524, -122.290163), _locationListener); assertTrue(location.isPaused()); CoordinatePoint centroid = location.getLocationCentroid(); assertEquals(47.66852325, centroid.getLat(), 0.0000001); assertEquals(-122.2901682, centroid.getLng(), 0.000001); location.update(mins(8), mvj("t0", 47.668519, -122.289948), _locationListener); assertFalse(location.isPaused()); assertEquals(mins(8), location.getLastLocationUpdateTime()); ArgumentCaptor<PausedLocation> captor = ArgumentCaptor.forClass(PausedLocation.class); Mockito.verify(_locationListener).handlePausedLocation(captor.capture()); PausedLocation paused = captor.getValue(); assertEquals(LINE_REF, paused.getLineRef()); assertEquals(VEHICLE_REF, paused.getVehicleRef()); assertEquals(mins(0), paused.getStartTime()); assertEquals(mins(7), paused.getEndTime()); assertEquals(0, paused.getUpdatesSinceTripChange()); centroid = paused.getLocation(); assertEquals(47.66852325, centroid.getLat(), 0.0000001); assertEquals(-122.2901682, centroid.getLng(), 0.000001); }