private void handlePlans(final GroupPlans plans) {
      final Map<Id<Person>, Plan> newJointPlan = new LinkedHashMap<Id<Person>, Plan>();
      final List<Plan> newIndividualPlans = new ArrayList<Plan>();

      for (JointPlan jp : plans.getJointPlans()) {
        newIndividualPlans.addAll(jp.getIndividualPlans().values());
      }

      for (Plan p : plans.getIndividualPlans()) {
        newJointPlan.put(p.getPerson().getId(), p);
      }

      plans.clear();
      plans.addJointPlan(factory.createJointPlan(newJointPlan));
      plans.addIndividualPlans(newIndividualPlans);
    }
  private void repareDriverTrips(final JointTrip toRemove, final JointPlan plan) {
    final List<TripStructureUtils.Trip> subtrips =
        getDriverTrip(toRemove, plan.getIndividualPlan(toRemove.getDriverId()));
    final List<PlanElement> newTrip = new ArrayList<PlanElement>();
    newTrip.add(PopulationUtils.createLeg(TransportMode.car));

    // "state" variables, changed in the loop:
    // - keeps track of the passengers currently in the vehicle.
    //   Pick-up or drop-offs are created at each change
    Set<Id> currentPassengers = Collections.<Id>emptySet();
    for (TripStructureUtils.Trip subtrip : subtrips) {
      final Leg leg = getDriverLegIfItIs(subtrip);
      final Route route = leg == null ? null : leg.getRoute();

      final Set<Id> newPassengers =
          route != null
              ? new HashSet<Id>(((DriverRoute) route).getPassengersIds())
              : Collections.<Id>emptySet();
      // note that no check of the mode is done...
      if (!newPassengers.equals(currentPassengers)) {
        newTrip.add(
            PopulationUtils.createActivityFromLinkId(
                JointActingTypes.INTERACTION,
                route != null ? route.getStartLinkId() : subtrip.getOriginActivity().getLinkId()));

        // as the spatial structure of the trip is modified, it is possible
        // that some pre-existing subtours are removed. Thus, a driver that may
        // have walked to a pick up (because at the same location as its departure)
        // may then have to drive to pick up the second passenger directly if
        // the first passenger was removed. Setting all non-driver legs
        // to car ensures to have a consistent mode chain.
        // XXX It could be done in a smarter way, so that if no subtour is removed, no modification
        // is done
        // For instance, when removing an "intern" trip, first PU and last DO are
        // left untouched, and thus access and egress leg need not be touched.
        newTrip.add(leg != null ? leg : PopulationUtils.createLeg(TransportMode.car));
        currentPassengers = newPassengers;
      }
    }

    TripRouter.insertTrip(
        plan.getIndividualPlan(toRemove.getDriverId()),
        subtrips.get(0).getOriginActivity(),
        newTrip,
        subtrips.get(subtrips.size() - 1).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());
  }