@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);
      }
    }
 /**
  * Returns a {@link AgencyAndId} with the specified new id value and the appropriate agency id
  * prefix. By default, we use the GTFS reader's default agency id. However, if the specified
  * bean+property has an existing {@link AgencyAndId} value, we use the agency-id specified there.
  */
 public AgencyAndId resolveAgencyAndId(BeanWrapper bean, String propertyName, String newId) {
   GtfsReaderContext context = _reader.getGtfsReaderContext();
   String agencyId = context.getDefaultAgencyId();
   AgencyAndId existingId = (AgencyAndId) bean.getPropertyValue(propertyName);
   if (existingId != null) {
     agencyId = existingId.getAgencyId();
   }
   return new AgencyAndId(agencyId, newId);
 }
  @Override
  public int compare(BlockInstance o1, BlockInstance o2) {

    BlockConfigurationEntry bc1 = o1.getBlock();
    BlockConfigurationEntry bc2 = o2.getBlock();
    BlockEntry b1 = bc1.getBlock();
    BlockEntry b2 = bc2.getBlock();

    AgencyAndId bId1 = b1.getId();
    AgencyAndId bId2 = b2.getId();

    int rc = bId1.compareTo(bId2);

    if (rc != 0) return rc;

    rc = bc1.getServiceIds().compareTo(bc2.getServiceIds());

    if (rc != 0) return rc;

    return Double.compare(o1.getServiceDate(), o2.getServiceDate());
  }
 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);
   }
 }
  /**
   * Range/sanity check the query parameter fields and build a Request object from them.
   *
   * @throws ParameterException when there is a problem interpreting a query parameter
   */
  protected RoutingRequest buildRequest() throws ParameterException {
    Router router = otpServer.getRouter(routerId);
    RoutingRequest request = router.defaultRoutingRequest.clone();
    request.routerId = routerId;
    // The routing request should already contain defaults, which are set when it is initialized or
    // in the JSON
    // router configuration and cloned. We check whether each parameter was supplied before
    // overwriting the default.
    if (fromPlace != null) request.setFromString(fromPlace);

    if (toPlace != null) request.setToString(toPlace);

    request.parseTime(router.graph.getTimeZone(), this.date, this.time);

    if (wheelchair != null) request.setWheelchairAccessible(wheelchair);

    if (numItineraries != null) request.setNumItineraries(numItineraries);

    if (maxWalkDistance != null) {
      request.setMaxWalkDistance(maxWalkDistance);
      request.maxTransferWalkDistance = maxWalkDistance;
    }

    if (maxPreTransitTime != null) request.setMaxPreTransitTime(maxPreTransitTime);

    if (walkReluctance != null) request.setWalkReluctance(walkReluctance);

    if (waitReluctance != null) request.setWaitReluctance(waitReluctance);

    if (waitAtBeginningFactor != null) request.setWaitAtBeginningFactor(waitAtBeginningFactor);

    if (walkSpeed != null) request.walkSpeed = walkSpeed;

    if (bikeSpeed != null) request.bikeSpeed = bikeSpeed;

    if (bikeSwitchTime != null) request.bikeSwitchTime = bikeSwitchTime;

    if (bikeSwitchCost != null) request.bikeSwitchCost = bikeSwitchCost;

    if (optimize != null) {
      // Optimize types are basically combined presets of routing parameters, except for triangle
      request.setOptimize(optimize);
      if (optimize == OptimizeType.TRIANGLE) {
        RoutingRequest.assertTriangleParameters(
            triangleSafetyFactor, triangleTimeFactor, triangleSlopeFactor);
        request.setTriangleSafetyFactor(this.triangleSafetyFactor);
        request.setTriangleSlopeFactor(this.triangleSlopeFactor);
        request.setTriangleTimeFactor(this.triangleTimeFactor);
      }
    }

    if (arriveBy != null) request.setArriveBy(arriveBy);

    if (showIntermediateStops != null) request.showIntermediateStops = showIntermediateStops;

    if (intermediatePlaces != null) request.setIntermediatePlacesFromStrings(intermediatePlaces);

    if (preferredRoutes != null) request.setPreferredRoutes(preferredRoutes);

    if (otherThanPreferredRoutesPenalty != null)
      request.setOtherThanPreferredRoutesPenalty(otherThanPreferredRoutesPenalty);

    if (preferredAgencies != null) request.setPreferredAgencies(preferredAgencies);

    if (unpreferredRoutes != null) request.setUnpreferredRoutes(unpreferredRoutes);

    if (unpreferredAgencies != null) request.setUnpreferredAgencies(unpreferredAgencies);

    if (walkBoardCost != null) request.setWalkBoardCost(walkBoardCost);

    if (bikeBoardCost != null) request.setBikeBoardCost(bikeBoardCost);

    if (bannedRoutes != null) request.setBannedRoutes(bannedRoutes);

    if (bannedAgencies != null) request.setBannedAgencies(bannedAgencies);

    HashMap<AgencyAndId, BannedStopSet> bannedTripMap = makeBannedTripMap(bannedTrips);
    if (bannedTripMap != null) request.bannedTrips = bannedTripMap;

    if (bannedStops != null) request.setBannedStops(bannedStops);

    if (bannedStopsHard != null) request.setBannedStopsHard(bannedStopsHard);

    // The "Least transfers" optimization is accomplished via an increased transfer penalty.
    // See comment on RoutingRequest.transferPentalty.
    if (transferPenalty != null) request.transferPenalty = transferPenalty;
    if (optimize == OptimizeType.TRANSFERS) {
      optimize = OptimizeType.QUICK;
      request.transferPenalty += 1800;
    }

    if (batch != null) request.batch = batch;

    if (optimize != null) request.setOptimize(optimize);

    /* Temporary code to get bike/car parking and renting working. */
    if (modes != null) {
      modes.applyToRoutingRequest(request);
      request.setModes(request.modes);
    }

    if (request.allowBikeRental && bikeSpeed == null) {
      // slower bike speed for bike sharing, based on empirical evidence from DC.
      request.bikeSpeed = 4.3;
    }

    if (boardSlack != null) request.boardSlack = boardSlack;

    if (alightSlack != null) request.alightSlack = alightSlack;

    if (minTransferTime != null)
      request.transferSlack = minTransferTime; // TODO rename field in routingrequest

    if (nonpreferredTransferPenalty != null)
      request.nonpreferredTransferPenalty = nonpreferredTransferPenalty;

    request.assertSlack();

    if (maxTransfers != null) request.maxTransfers = maxTransfers;

    final long NOW_THRESHOLD_MILLIS = 15 * 60 * 60 * 1000;
    boolean tripPlannedForNow =
        Math.abs(request.getDateTime().getTime() - new Date().getTime()) < NOW_THRESHOLD_MILLIS;
    request.useBikeRentalAvailabilityInformation =
        (tripPlannedForNow); // TODO the same thing for GTFS-RT

    if (startTransitStopId != null && !startTransitStopId.isEmpty())
      request.startingTransitStopId = AgencyAndId.convertFromString(startTransitStopId);

    if (startTransitTripId != null && !startTransitTripId.isEmpty())
      request.startingTransitTripId = AgencyAndId.convertFromString(startTransitTripId);

    if (clampInitialWait != null) request.clampInitialWait = clampInitialWait;

    if (reverseOptimizeOnTheFly != null) request.reverseOptimizeOnTheFly = reverseOptimizeOnTheFly;

    if (ignoreRealtimeUpdates != null) request.ignoreRealtimeUpdates = ignoreRealtimeUpdates;

    if (disableRemainingWeightHeuristic != null)
      request.disableRemainingWeightHeuristic = disableRemainingWeightHeuristic;

    if (maxHours != null) request.maxHours = maxHours;

    if (disableAlertFiltering != null) request.disableAlertFiltering = disableAlertFiltering;

    // getLocale function returns defaultLocale if locale is null
    request.locale = ResourceBundleSingleton.INSTANCE.getLocale(locale);
    return request;
  }