MATSIM
RouteOccupancy.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * RouteOccupancy.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2009 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.pt.analysis;
22 
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Map;
26 import java.util.Set;
27 
28 import org.matsim.api.core.v01.Id;
35 
43 
44  private static final Integer Int1 = Integer.valueOf(1);
45 
46  private final TransitRoute transitRoute;
47  private final VehicleTracker tracker;
48  private Set<Id> routeVehicles = null;
49  private final Map<Id, Integer> enteringPassengers = new HashMap<Id, Integer>();
50  private final Map<Id, Integer> leavingPassengers = new HashMap<Id, Integer>();
51 
52  public RouteOccupancy(final TransitRoute transitRoute, final VehicleTracker tracker) {
53  this.transitRoute = transitRoute;
54  this.tracker = tracker;
55  }
56 
57  public void handleEvent(final PersonEntersVehicleEvent event) {
58  if (this.routeVehicles == null) {
60  }
61  if (this.routeVehicles.contains(event.getVehicleId())) {
62  Id facilityId = this.tracker.getFacilityIdForVehicle(event.getVehicleId());
63  Integer count = this.enteringPassengers.get(facilityId);
64  if (count == null) {
65  this.enteringPassengers.put(facilityId, Int1);
66  } else {
67  this.enteringPassengers.put(facilityId, Integer.valueOf(1 + count.intValue()));
68  }
69  }
70  }
71 
72  public void handleEvent(final PersonLeavesVehicleEvent event) {
73  if (this.routeVehicles == null) {
75  }
76  if (this.routeVehicles.contains(event.getVehicleId())) {
77  Id facilityId = this.tracker.getFacilityIdForVehicle(event.getVehicleId());
78  Integer count = this.leavingPassengers.get(facilityId);
79  if (count == null) {
80  this.leavingPassengers.put(facilityId, Int1);
81  } else {
82  this.leavingPassengers.put(facilityId, Integer.valueOf(1 + count.intValue()));
83  }
84  }
85  }
86 
87  public void reset(final int iteration) {
88  this.routeVehicles = null;
89  this.enteringPassengers.clear();
90  this.leavingPassengers.clear();
91  }
92 
93  public int getNumberOfEnteringPassengers(final Id stopFacilityId) {
94  Integer count = this.enteringPassengers.get(stopFacilityId);
95  if (count == null) {
96  return 0;
97  }
98  return count.intValue();
99  }
100 
101  public int getNumberOfLeavingPassengers(final Id stopFacilityId) {
102  Integer count = this.leavingPassengers.get(stopFacilityId);
103  if (count == null) {
104  return 0;
105  }
106  return count.intValue();
107  }
108 
112  private void collectVehiclesInfo() {
113  Set<Id> set = new HashSet<Id>(this.transitRoute.getDepartures().size()*2);
114 
115  for (Departure departure : this.transitRoute.getDepartures().values()) {
116  if (departure.getVehicleId() != null) {
117  set.add(departure.getVehicleId());
118  }
119  }
120 
121  /* try to make it thread-safe by assigning class-member at the end.
122  * if two threads enter this method, nothing bad should happen,
123  * as both threads should generated the same initialization.
124  */
125  this.routeVehicles = set;
126  }
127 
128 }
static< T > Id< T > get(int index, final Class< T > type)
Definition: Id.java:112
final Map< Id, Integer > enteringPassengers
final Map< Id, Integer > leavingPassengers
void handleEvent(final PersonLeavesVehicleEvent event)
Map< Id< Departure >, Departure > getDepartures()
void handleEvent(final PersonEntersVehicleEvent event)
RouteOccupancy(final TransitRoute transitRoute, final VehicleTracker tracker)
int getNumberOfEnteringPassengers(final Id stopFacilityId)
Id getFacilityIdForVehicle(final Id vehicleId)
int getNumberOfLeavingPassengers(final Id stopFacilityId)