MATSIM
ArrayBasedDataContainerProvider.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * ArrayBasedDataContainerProvider.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2013 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 org.matsim.api.core.v01.Id;
27 
28 import java.util.Map;
29 
41 class ArrayBasedDataContainerProvider implements DataContainerProvider {
42 
43  private final TravelTimeData[] arrayLinkData;
44  private final DataContainerProvider delegate;
45 
46  public ArrayBasedDataContainerProvider(Map<Id<Link>, TravelTimeData> linkData, TravelTimeDataFactory ttDataFactory,
47  Network network) {
48  this.arrayLinkData = new TravelTimeData[network.getLinks().size()];
49  this.delegate = new MapBasedDataContainerProvider(linkData, ttDataFactory);
50  }
51 
52  /*
53  * This method is called from the EventHandler part of the TravelTimeCalculator.
54  * There, only link ids are available. We cannot optimize this.
55  */
56  @Override
57  public TravelTimeData getTravelTimeData(final Id<Link> linkId, final boolean createIfMissing) {
58  return this.delegate.getTravelTimeData(linkId, createIfMissing);
59  }
60 
61  /*
62  * This method is called from the TravelTime part of the TravelTimeCalculator.
63  * There, links are available. we can optimize this by using an array instead of a map.
64  *
65  * Probably pre-initialize all DataContainers to avoid the null-check?
66  */
67  @Override
68  public TravelTimeData getTravelTimeData(Link link, boolean createIfMissing) {
69  if (link instanceof HasIndex) {
70  int index = ((HasIndex) link).getArrayIndex();
71  TravelTimeData data = this.arrayLinkData[index];
72  if (data == null) {
73  data = this.delegate.getTravelTimeData(link, createIfMissing);
74  this.arrayLinkData[index] = data;
75  }
76  return data;
77  } else {
78  return this.delegate.getTravelTimeData(link, createIfMissing);
79  }
80  }
81 
82 }
Map< Id< Link >, ? extends Link > getLinks()