MATSIM
TravelTimeDataHashMap.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * TravelTimeRoleHashMap.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2008 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.trafficmonitoring;
22 
23 import java.util.Map;
24 import java.util.concurrent.ConcurrentHashMap;
25 
29 
39 class TravelTimeDataHashMap extends TravelTimeData {
40  private final Map<Integer,TimeStruct> travelTimes;
41 
42  protected final Link link;
43 
44  public TravelTimeDataHashMap(final Link link) {
45  this.travelTimes = new ConcurrentHashMap<>();
46  this.link = link;
47 // resetTravelTimes();
48  }
49 
50  @Override
51  public void resetTravelTimes() {
52  this.travelTimes.clear();
53  }
54 
55  @Override
56  public void setTravelTime( final int timeSlice, final double traveltime ) {
57  TimeStruct curr = this.travelTimes.get(IntegerCache.getInteger(timeSlice));
58  if (curr != null) {
59  curr.cnt = 1;
60  curr.timeSum = traveltime;
61  } else {
62  this.travelTimes.put(IntegerCache.getInteger(timeSlice), new TimeStruct(traveltime,1));
63  }
64  }
65 
66  @Override
67  public void addTravelTime(final int timeSlice, final double traveltime) {
68  TimeStruct curr = this.travelTimes.get(IntegerCache.getInteger(timeSlice));
69  if (curr != null) {
70  curr.cnt += 1;
71  curr.timeSum += traveltime;
72  } else {
73  this.travelTimes.put(IntegerCache.getInteger(timeSlice), new TimeStruct(traveltime,1));
74  }
75  }
76 
77  @Override
78  public double getTravelTime(final int timeSlice, final double now) {
79 
80  TimeStruct ts = this.travelTimes.get(IntegerCache.getInteger(timeSlice));
81  if (ts == null) {
82  Link r = ((Link)this.link);
83  return NetworkUtils.getFreespeedTravelTime(r, now) ;
84  }
85  return ts.timeSum / ts.cnt;
86  }
87 
88  private static class TimeStruct {
89  public double timeSum;
90  public int cnt;
91  public TimeStruct(final double timeSum, final int cnt) {
92  this.cnt = cnt;
93  this.timeSum = timeSum;
94  }
95  }
96 
97 }