private Map<Integer, MeasurementEvent> getLatestMeasurementEvents() {
    Map<Integer, MeasurementEvent> measMap = new HashMap<Integer, MeasurementEvent>();

    for (AbstractEvent savedEvent : events.values()) {
      if (savedEvent instanceof TriggerFiredEvent) {
        TriggerFiredEvent tfe = (TriggerFiredEvent) savedEvent;
        AbstractEvent[] triggeredEvents = tfe.getEvents();

        for (AbstractEvent triggeredEvent : triggeredEvents) {
          if (triggeredEvent instanceof MeasurementEvent) {
            MeasurementEvent me = (MeasurementEvent) triggeredEvent;
            Integer measurementId = me.getInstanceId();
            MeasurementEvent savedMe = measMap.get(measurementId);
            if (savedMe == null) {
              measMap.put(measurementId, me);
            } else {
              if (me.getTimestamp() > savedMe.getTimestamp()) {
                // replace with the latest measurement event
                measMap.put(measurementId, me);
              }
            }
          }
        }
      }
    }

    return measMap;
  }
  private boolean isStale(TriggerFiredEvent tfe, Map<Integer, MeasurementEvent> measMap) {
    boolean stale = false;
    AbstractEvent[] triggeredEvents = tfe.getEvents();

    for (AbstractEvent triggeredEvent : triggeredEvents) {
      if (triggeredEvent instanceof MeasurementEvent) {
        MeasurementEvent me = (MeasurementEvent) triggeredEvent;
        Integer measurementId = me.getInstanceId();
        MeasurementEvent latestMe = measMap.get(measurementId);
        if ((latestMe != null) && (me.getTimestamp() < latestMe.getTimestamp())) {
          stale = true;
          break;
        }
      }
    }

    return stale;
  }