@Override
    public void handleEntity(Object bean) {

      try {
        NycVehicleLocationRecord record = (NycVehicleLocationRecord) bean;
        AgencyAndId vehicleId = record.getVehicleId();

        NycVehicleLocationRecord prev = _lastRecordsByVehicleId.put(vehicleId, record);

        EntityHandler handler = _entityHandlersByVehicleId.get(vehicleId);

        long time = getTimeForRecord(record);

        if (prev == null || getTimeForRecord(prev) + _maxOffset < time || handler == null) {

          Writer writer = _writersByVehicleId.get(vehicleId);
          if (writer != null) writer.close();

          String timeString = _format.format(new Date(time));
          String fileName = vehicleId.getId() + "-" + timeString + ".txt";
          File outputFile = new File(_outputDirectory, fileName);
          writer = new FileWriter(outputFile);

          handler = _factory.createWriter(NycVehicleLocationRecord.class, writer);
          _entityHandlersByVehicleId.put(vehicleId, handler);
          _writersByVehicleId.put(vehicleId, writer);
        }

        handler.handleEntity(record);
      } catch (IOException ex) {
        throw new IllegalStateException(ex);
      }
    }
 private void applyObjectToAnnotation(Graph graph, AnnotationObject annotation, Object o) {
   if (o instanceof Edge) {
     annotation.edge = graph.getIdForEdge((Edge) o);
   } else if (o instanceof Vertex) {
     annotation.vertex = ((Vertex) o).getLabel();
   } else if (o instanceof String) {
     annotation.message = (String) o;
   } else if (o instanceof IdentityBean) {
     IdentityBean<?> bean = (IdentityBean<?>) o;
     Object id = bean.getId();
     applyObjectToAnnotation(graph, annotation, id);
   } else if (o instanceof AgencyAndId) {
     AgencyAndId id = (AgencyAndId) o;
     annotation.agency = id.getAgencyId();
     annotation.id = id.getId();
   } else if (o instanceof Collection) {
     Collection<?> collection = (Collection<?>) o;
     if (collection.isEmpty()) return;
     Object first = collection.iterator().next();
     applyObjectToAnnotation(graph, annotation, first);
   }
 }