edu.kit.ifv.mobitopp.data.local.TravelTimeMatrixCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mobitopp Show documentation
Show all versions of mobitopp Show documentation
mobiTopp (http://mobitopp.ifv.kit.edu/) is an agent-based travel demand model developed at the Institute for transport studies at the Karlsruhe Institute of Technology (http://www.ifv.kit.edu/english/index.php). Publications about mobiTopp can be found on the project site (http://mobitopp.ifv.kit.edu/28.php).
The newest version!
package edu.kit.ifv.mobitopp.data.local;
import java.io.IOException;
import java.util.stream.Stream;
import edu.kit.ifv.mobitopp.data.TravelTimeMatrix;
import edu.kit.ifv.mobitopp.data.local.configuration.MatrixConfiguration;
import edu.kit.ifv.mobitopp.data.local.configuration.TaggedTravelTimeMatrix;
import edu.kit.ifv.mobitopp.data.local.configuration.TravelTimeMatrixId;
import edu.kit.ifv.mobitopp.simulation.StandardMode;
import edu.kit.ifv.mobitopp.time.Time;
public class TravelTimeMatrixCache extends MatrixCache {
private int cachedHour;
private TravelTimeMatrix[] cachedMatrices;
public TravelTimeMatrixCache(MatrixConfiguration configuration) {
super(configuration);
createEmptyCache();
}
public TravelTimeMatrixId idOf(StandardMode mode, Time date) {
return configuration().idOf(mode, date);
}
public TravelTimeMatrix matrixFor(StandardMode mode, Time date) {
if (this.cachedHour != date.getHour()) {
createEmptyCache();
}
if (null != cachedMatrices[mode.ordinal()]) {
return cachedMatrices[mode.ordinal()];
}
return updateCache(mode, date);
}
private TravelTimeMatrix updateCache(StandardMode mode, Time date) {
TravelTimeMatrixId id = idOf(mode, date);
TravelTimeMatrix matrix = matrixFor(id).matrix();
cachedMatrices[mode.ordinal()] = matrix;
cachedHour = date.getHour();
return matrix;
}
private void createEmptyCache() {
cachedMatrices = new TravelTimeMatrix[StandardMode.values().length];
}
protected TaggedTravelTimeMatrix loadMatrixBy(TravelTimeMatrixId id) throws IOException {
return configuration().travelTimeMatrixFor(id);
}
@Override
protected Stream split(TaggedTravelTimeMatrix taggedMatrix) {
return taggedMatrix.id().split();
}
}