MATSIM
MinimalTransferTimesImpl.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * *
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2018 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** */
19 
20 package org.matsim.pt.transitSchedule;
21 
22 import java.util.Iterator;
23 import java.util.Map;
24 import java.util.NoSuchElementException;
25 import java.util.concurrent.ConcurrentHashMap;
26 
27 import org.matsim.api.core.v01.Id;
30 
36 class MinimalTransferTimesImpl implements MinimalTransferTimes {
37 
38  private final Map<Id<TransitStopFacility>, Map<Id<TransitStopFacility>, Double>> minimalTransferTimes = new ConcurrentHashMap<>();
39 
40  @Override
41  public double set(Id<TransitStopFacility> fromStop, Id<TransitStopFacility> toStop, double seconds) {
42  if (Double.isNaN(seconds)) {
43  return remove(fromStop, toStop);
44  }
45  Map<Id<TransitStopFacility>, Double> innerMap = this.minimalTransferTimes.computeIfAbsent(fromStop, key -> new ConcurrentHashMap<>());
46  Double value = innerMap.put(toStop, seconds);
47  if (value == null) {
48  return Double.NaN;
49  }
50  return 0;
51  }
52 
53  @Override
54  public double get(Id<TransitStopFacility> fromStop, Id<TransitStopFacility> toStop) {
55  return get(fromStop, toStop, Double.NaN);
56  }
57 
58  @Override
59  public double get(Id<TransitStopFacility> fromStop, Id<TransitStopFacility> toStop, double defaultSeconds) {
60  Map<Id<TransitStopFacility>, Double> innerMap = this.minimalTransferTimes.get(fromStop);
61  if (innerMap == null) {
62  return getInnerStopTransferTime(toStop,defaultSeconds);
63  }
64  Double value = innerMap.get(toStop);
65  if (value == null) {
66  return Math.max(getInnerStopTransferTime(toStop,defaultSeconds),getInnerStopTransferTime(fromStop,defaultSeconds));
67  }
68  return value;
69  }
70 
71  private double getInnerStopTransferTime(Id<TransitStopFacility> stopId, double defaultSeconds){
72  Map<Id<TransitStopFacility>, Double> innerMap = this.minimalTransferTimes.get(stopId);
73  return innerMap!=null?innerMap.getOrDefault(stopId,defaultSeconds):defaultSeconds;
74 
75  }
76 
77  @Override
78  public double remove(Id<TransitStopFacility> fromStop, Id<TransitStopFacility> toStop) {
79  Map<Id<TransitStopFacility>, Double> innerMap = this.minimalTransferTimes.get(fromStop);
80  if (innerMap == null) {
81  return Double.NaN;
82  }
83  Double value = innerMap.remove(toStop);
84  if (value == null) {
85  return Double.NaN;
86  }
87  return value;
88  }
89 
90  @Override
91  public MinimalTransferTimesIterator iterator() {
92  return new MinimalTransferTimesIteratorImpl(this.minimalTransferTimes);
93  }
94 
96 
100  private double seconds = Double.NaN;
101  private boolean hasElement = false;
102 
103  private final Iterator<Map.Entry<Id<TransitStopFacility>, Map<Id<TransitStopFacility>, Double>>> outerIterator;
104  private Iterator<Map.Entry<Id<TransitStopFacility>, Double>> innerIterator;
105 
107  this.outerIterator = values.entrySet().iterator();
108  }
109 
110  @Override
111  public boolean hasNext() {
112  if (this.innerIterator != null && this.innerIterator.hasNext()) {
113  return true;
114  }
115  while (this.outerIterator.hasNext()) {
116  Map.Entry<Id<TransitStopFacility>, Map<Id<TransitStopFacility>, Double>> outerEntry = this.outerIterator.next();
117  Map<Id<TransitStopFacility>, Double> innerMap = outerEntry.getValue();
118  this.innerIterator = innerMap.entrySet().iterator();
119  if (this.innerIterator.hasNext()) {
120  this.nextFromStopId = outerEntry.getKey();
121  return true;
122  }
123  }
124 
125  this.nextFromStopId = null;
126  return false;
127  }
128 
129  @Override
130  public void next() {
131  if (this.innerIterator != null && this.innerIterator.hasNext()) {
132  Map.Entry<Id<TransitStopFacility>, Double> e = this.innerIterator.next();
133  this.fromStopId = this.nextFromStopId;
134  this.toStopId = e.getKey();
135  this.seconds = e.getValue();
136  this.hasElement = true;
137  } else {
138  this.hasElement = false;
139  throw new NoSuchElementException();
140  }
141  }
142 
143  @Override
145  if (this.hasElement) {
146  return this.fromStopId;
147  }
148  throw new NoSuchElementException();
149  }
150 
151  @Override
153  if (this.hasElement) {
154  return this.toStopId;
155  }
156  throw new NoSuchElementException();
157  }
158 
159  @Override
160  public double getSeconds() {
161  if (this.hasElement) {
162  return this.seconds;
163  }
164  throw new NoSuchElementException();
165  }
166  }
167 }
final Iterator< Map.Entry< Id< TransitStopFacility >, Map< Id< TransitStopFacility >, Double > > > outerIterator