20 package org.matsim.core.network;
22 import java.util.Arrays;
23 import java.util.TreeMap;
29 import com.google.common.base.Preconditions;
35 final class FixedIntervalTimeVariantAttribute
implements TimeVariantAttribute {
36 private final int timeSlice;
37 private final int numSlots;
39 private double baseValue;
40 private double[] values;
42 private int eventsCount = 0;
43 private int eventsCountWhenLastRecalc = -1;
45 public FixedIntervalTimeVariantAttribute(
int timeSlice,
int maxTime) {
46 this.timeSlice = timeSlice;
47 this.numSlots = TimeBinUtils.getTimeBinCount(maxTime, timeSlice);
51 public boolean isRecalcRequired() {
52 return eventsCountWhenLastRecalc != eventsCount;
57 public void recalc(TreeMap<Double, NetworkChangeEvent> changeEvents, ChangeValueGetter valueGetter,
59 this.baseValue = baseValue1;
61 if (eventsCount == 0) {
69 values =
new double[numSlots];
74 double currentValue = baseValue1;
75 if (changeEvents != null) {
76 for (NetworkChangeEvent event : changeEvents.values()) {
77 ChangeValue value = valueGetter.getChangeValue(event);
81 Preconditions.checkArgument(event.getStartTime() >= 0,
82 "The current implementation supports only non-negative change event times");
83 int toBin = (int)(event.getStartTime() / timeSlice);
84 Arrays.fill(values, fromBin, toBin, currentValue);
86 switch (value.getType()) {
87 case ABSOLUTE_IN_SI_UNITS:
88 currentValue = value.getValue();
91 currentValue *= value.getValue();
93 case OFFSET_IN_SI_UNITS:
94 currentValue += value.getValue();
103 Arrays.fill(values, fromBin, values.length, currentValue);
104 eventsCountWhenLastRecalc = eventsCount;
106 if (numEvent != this.eventsCount) {
109 +
") differs from the number of events found (" 116 public double getValue(
final double time) {
117 Preconditions.checkArgument(!Double.isNaN(time),
"NaN time is not supported");
118 if (eventsCount == 0) {
122 int bin = TimeBinUtils.getTimeBinIndex(time, timeSlice, numSlots);
123 return bin < 0 ? baseValue : values[bin];
127 public void incChangeEvents() {
132 public void clearEvents() {
134 eventsCountWhenLastRecalc = -1;