private static Trip getTripWithLeg(
     final Plan plan, final Leg leg, final StageActivityTypes stages) {
   for (Trip t : TripStructureUtils.getTrips(plan, stages)) {
     if (t.getTripElements().contains(leg)) return t;
   }
   throw new RuntimeException(plan.getPlanElements() + " doesn't contain " + leg);
 }
 private Leg getDriverLegIfItIs(final Trip subtrip) {
   if (!mainModeIdentifier
       .identifyMainMode(subtrip.getTripElements())
       .equals(JointActingTypes.DRIVER)) return null;
   if (subtrip.getLegsOnly().size() != 1)
     throw new RuntimeException("unexpected driver subtrip length: " + subtrip);
   return subtrip.getLegsOnly().get(0);
 }
  private void setRandomTripMode(final Trip trip, final Plan plan, boolean ffcard, boolean owcard) {

    // carsharing is the new trip
    if (possibleModes.length == 2) {
      if (rng.nextBoolean()) {
        if (owcard)
          TripRouter.insertTrip(
              plan,
              trip.getOriginActivity(),
              Collections.singletonList(new LegImpl("onewaycarsharing")),
              trip.getDestinationActivity());
      } else {
        if (ffcard)
          TripRouter.insertTrip(
              plan,
              trip.getOriginActivity(),
              Collections.singletonList(new LegImpl("freefloating")),
              trip.getDestinationActivity());
      }
    } else if (possibleModes.length == 1 && possibleModes[0] != null) {
      if ((possibleModes[0].equals("freefloating") && ffcard)
          || (possibleModes[0].equals("onewaycarsharing") && owcard))
        TripRouter.insertTrip(
            plan,
            trip.getOriginActivity(),
            Collections.singletonList(new LegImpl(possibleModes[0])),
            trip.getDestinationActivity());
    } else
      TripRouter.insertTrip(
          plan, trip.getOriginActivity(), trip.getTripElements(), trip.getDestinationActivity());
  }
  // package protected for tests
  final void removePassengerTrip(final JointTrip toRemove, final JointPlan jointPlan) {
    final Plan passengerPlan = jointPlan.getIndividualPlan(toRemove.getPassengerId());

    final Trip tripWithLeg =
        getTripWithLeg(passengerPlan, toRemove.getPassengerLeg(), stagesWithJointTypes);

    TripRouter.insertTrip(
        passengerPlan,
        tripWithLeg.getOriginActivity(),
        Collections.singletonList(PopulationUtils.createLeg(TransportMode.pt)),
        tripWithLeg.getDestinationActivity());
  }