MATSIM
TransitStopAgentTracker.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * TransitStop.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.core.mobsim.qsim.pt;
22 
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25 import org.matsim.api.core.v01.Id;
30 
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.concurrent.ConcurrentHashMap;
35 import java.util.concurrent.CopyOnWriteArrayList;
36 
40 public class TransitStopAgentTracker implements AgentTracker {
41 
42  private final static Logger log = LogManager.getLogger(TransitStopAgentTracker.class);
43 
44  private final EventsManager events;
45  private final Map<Id<TransitStopFacility>, List<PTPassengerAgent>> agentsAtStops = new ConcurrentHashMap<>();
46 
47  public TransitStopAgentTracker(final EventsManager events) {
48  this.events = events;
49  }
50 
51  public void addAgentToStop(final double now, final PTPassengerAgent agent, final Id<TransitStopFacility> stopId) {
52  if (stopId == null) {
53  throw new NullPointerException("stop must not be null.");
54  }
55  List<PTPassengerAgent> agents = this.agentsAtStops.get(stopId);
56  if (agents == null) {
57  agents = new CopyOnWriteArrayList<>();// TODO check again. this might turn out to be slow, but we likely need something thread safe here. marcel/oct2014
58  this.agentsAtStops.put(stopId, agents);
59  }
60  if ( !agents.add(agent) ) {
61  log.error("did NOT add agent " + agent.getId() + " since it was already there.");
62  }
63  Id<TransitStopFacility> destinationStopId = agent.getDesiredDestinationStopId();
64  events.processEvent(new AgentWaitingForPtEvent(now, agent.getId(), stopId, destinationStopId));
65  }
66 
67  public void removeAgentFromStop(final PTPassengerAgent agent, final Id<TransitStopFacility> stopId) {
68  if (stopId == null) {
69  throw new NullPointerException("stopId must not be null.");
70  }
71  List<PTPassengerAgent> agents = this.agentsAtStops.get(stopId);
72  if (agents != null) {
73  if (!agents.remove(agent)) {
74  log.error("Agent " + agent.getId() + " could not be removed from waiting at stop " + stopId);
75  }
76  } else {
77  log.error("Agent " + agent.getId() + " could not be removed from waiting at stop " + stopId + " since agents list was null.");
78  }
79  }
80 
81  @Override
82  public List<PTPassengerAgent> getAgentsAtFacility(final Id<TransitStopFacility> stopId) {
83  List<PTPassengerAgent> agents = this.agentsAtStops.get(stopId);
84  if (agents == null) {
85  return Collections.emptyList();
86  }
87  return Collections.unmodifiableList(agents);
88  }
89 
90  public Map<Id<TransitStopFacility>, List<PTPassengerAgent>> getAgentsAtStop() {
91  return this.agentsAtStops;
92  }
93 }
final Map< Id< TransitStopFacility >, List< PTPassengerAgent > > agentsAtStops
Id< TransitStopFacility > getDesiredDestinationStopId()
void addAgentToStop(final double now, final PTPassengerAgent agent, final Id< TransitStopFacility > stopId)
void removeAgentFromStop(final PTPassengerAgent agent, final Id< TransitStopFacility > stopId)
List< PTPassengerAgent > getAgentsAtFacility(final Id< TransitStopFacility > stopId)
Map< Id< TransitStopFacility >, List< PTPassengerAgent > > getAgentsAtStop()