public LegBean reverseLeg(LegV2Bean bean) {

    LegBean leg = new LegBean();

    leg.setStartTime(bean.getStartTime());
    leg.setEndTime(bean.getEndTime());
    leg.setFrom(_factory.reversePoint(bean.getFrom()));
    leg.setTo(_factory.reversePoint(bean.getTo()));
    leg.setDistance(bean.getDistance());
    if (_factory.isStringSet(bean.getMode())) leg.setMode(bean.getMode());

    TransitLegV2Bean transitLegBean = bean.getTransitLeg();
    if (transitLegBean != null) {
      TransitLegBean transitLeg = reverseTransitLeg(transitLegBean);
      leg.setTransitLeg(transitLeg);
    }

    List<StreetLegV2Bean> streetLegBeans = bean.getStreetLegs();
    if (!CollectionsLibrary.isEmpty(streetLegBeans)) {
      List<StreetLegBean> streetLegs = new ArrayList<StreetLegBean>();
      for (StreetLegV2Bean streetLegBean : streetLegBeans) {
        StreetLegBean streetLeg = reverseStreetLeg(streetLegBean);
        streetLegs.add(streetLeg);
      }
      leg.setStreetLegs(streetLegs);
    }

    return leg;
  }
  public LegV2Bean getLeg(LegBean leg) {

    LegV2Bean bean = new LegV2Bean();

    bean.setStartTime(leg.getStartTime());
    bean.setEndTime(leg.getEndTime());
    bean.setFrom(_factory.getPoint(leg.getFrom()));
    bean.setTo(_factory.getPoint(leg.getTo()));
    bean.setDistance(leg.getDistance());
    bean.setMode(leg.getMode());

    TransitLegBean transitLeg = leg.getTransitLeg();
    if (transitLeg != null) {
      TransitLegV2Bean transitLegBean = getTransitLeg(transitLeg);
      bean.setTransitLeg(transitLegBean);
    }

    List<StreetLegBean> streetLegs = leg.getStreetLegs();
    if (!CollectionsLibrary.isEmpty(streetLegs)) {
      List<StreetLegV2Bean> streetLegBeans = new ArrayList<StreetLegV2Bean>();
      for (StreetLegBean streetLeg : streetLegs) {
        StreetLegV2Bean streetLegBean = getStreetLeg(streetLeg);
        streetLegBeans.add(streetLegBean);
      }
      bean.setStreetLegs(streetLegBeans);
    }

    return bean;
  }
 private DefaultHttpHeaders transformOutOfRangeResult() {
   if (isVersion(V1)) {
     return setOkResponse(new StopsBean());
   } else if (isVersion(V2)) {
     BeanFactoryV2 factory = getBeanFactoryV2();
     return setOkResponse(factory.getEmptyList(StopV2Bean.class, true));
   } else {
     return setUnknownVersionResponse();
   }
 }
  public StreetLegBean reverseStreetLeg(StreetLegV2Bean leg) {

    StreetLegBean bean = new StreetLegBean();

    if (_factory.isStringSet(leg.getStreetName())) bean.setStreetName(leg.getStreetName());

    if (_factory.isStringSet(leg.getPath())) bean.setPath(leg.getPath());
    bean.setDistance(leg.getDistance());

    return bean;
  }
 private DefaultHttpHeaders transformResult(StopsBean result) {
   BeanFactoryV2 factory = getBeanFactoryV2(_service);
   factory.filterNonRevenueStops(result);
   if (isVersion(V1)) {
     return setOkResponse(result);
   } else if (isVersion(V2)) {
     return setOkResponse(factory.getResponse(result));
   } else {
     return setUnknownVersionResponse();
   }
 }
  private String getValueAsString(Object value) {

    if (value instanceof StopBean) {
      StopBean stop = (StopBean) value;
      _factory.addToReferences(stop);
      return stop.getId();
    } else {
      return value.toString();
    }
  }
  private TransitLegBean reverseTransitLeg(TransitLegV2Bean leg) {

    TransitLegBean bean = new TransitLegBean();

    String tripId = leg.getTripId();
    if (tripId != null && !tripId.isEmpty()) {
      TripBean trip = new TripBean();
      trip.setId(tripId);
      bean.setTrip(trip);
    }

    bean.setServiceDate(leg.getServiceDate());
    if (_factory.isStringSet(leg.getVehicleId())) bean.setVehicleId(leg.getVehicleId());

    FrequencyV2Bean frequency = leg.getFrequency();
    if (frequency != null) {
      FrequencyBean freqBean = _factory.reverseFrequency(frequency);
      bean.setFrequency(freqBean);
    }

    String fromStopId = leg.getFromStopId();
    if (_factory.isStringSet(fromStopId)) {
      StopBean stop = new StopBean();
      stop.setId(fromStopId);
      bean.setFromStop(stop);
      bean.setFromStopSequence(leg.getFromStopSequence());
    }

    bean.setScheduledDepartureTime(leg.getScheduledDepartureTime());

    String toStopId = leg.getToStopId();
    if (_factory.isStringSet(toStopId)) {
      StopBean stop = new StopBean();
      stop.setId(toStopId);
      bean.setToStop(stop);
      bean.setToStopSequence(leg.getToStopSequence());
    }

    bean.setScheduledArrivalTime(leg.getScheduledArrivalTime());

    return bean;
  }
  public LocationV2Bean getLocation(LocationBean location) {

    LocationV2Bean bean = new LocationV2Bean();
    bean.setLocation(location.getLocation());
    bean.setName(location.getName());
    StopBean stop = location.getStopBean();

    if (stop != null) {
      bean.setStopId(stop.getId());
      _factory.addToReferences(stop);
    }

    return bean;
  }
  public TransitLegV2Bean getTransitLeg(TransitLegBean leg) {

    TransitLegV2Bean bean = new TransitLegV2Bean();

    TripBean trip = leg.getTrip();
    if (trip != null) {
      bean.setTripId(trip.getId());
      _factory.addToReferences(trip);
    }

    bean.setServiceDate(leg.getServiceDate());
    bean.setVehicleId(leg.getVehicleId());

    FrequencyBean frequency = leg.getFrequency();
    if (frequency != null) {
      FrequencyV2Bean freqBean = _factory.getFrequency(frequency);
      bean.setFrequency(freqBean);
    }

    StopBean fromStop = leg.getFromStop();
    if (fromStop != null) {
      bean.setFromStopId(fromStop.getId());
      bean.setFromStopSequence(leg.getFromStopSequence());
      _factory.addToReferences(fromStop);
    }

    bean.setScheduledDepartureTime(leg.getScheduledDepartureTime());
    bean.setPredictedDepartureTime(leg.getPredictedDepartureTime());
    bean.setScheduledDepartureInterval(
        _factory.getTimeInterval(leg.getScheduledDepartureInterval()));
    bean.setPredictedDepartureInterval(
        _factory.getTimeInterval(leg.getPredictedDepartureInterval()));

    StopBean toStop = leg.getToStop();
    if (toStop != null) {
      bean.setToStopId(toStop.getId());
      bean.setToStopSequence(leg.getToStopSequence());
      _factory.addToReferences(toStop);
    }

    bean.setScheduledArrivalTime(leg.getScheduledArrivalTime());
    bean.setPredictedArrivalTime(leg.getPredictedArrivalTime());
    bean.setScheduledArrivalInterval(_factory.getTimeInterval(leg.getScheduledArrivalInterval()));
    bean.setPredictedArrivalInterval(_factory.getTimeInterval(leg.getPredictedArrivalInterval()));

    bean.setRouteShortName(leg.getRouteShortName());
    bean.setRouteLongName(leg.getRouteLongName());
    bean.setTripHeadsign(leg.getTripHeadsign());
    bean.setPath(leg.getPath());

    List<ServiceAlertBean> situations = leg.getSituations();
    if (!CollectionsLibrary.isEmpty(situations)) {
      List<String> situationIds = new ArrayList<String>(situations.size());
      for (ServiceAlertBean situation : situations) {
        situationIds.add(situation.getId());
        _factory.addToReferences(situation);
      }
      bean.setSituationIds(situationIds);
    }

    return bean;
  }