1 package org.opentrafficsim.kpi.sampling.indicator; 2 3 import java.util.List; 4 5 import org.djunits.value.vdouble.scalar.Time; 6 import org.djunits.value.vdouble.scalar.base.DoubleScalarInterface; 7 import org.djutils.exceptions.Throw; 8 import org.opentrafficsim.kpi.interfaces.GtuDataInterface; 9 import org.opentrafficsim.kpi.sampling.Query; 10 import org.opentrafficsim.kpi.sampling.TrajectoryGroup; 11 12 /** 13 * Abstract indicator which stores the last calculated value and returns it in {@code getValue()} for an equal query, start time 14 * and end time. 15 * <p> 16 * Copyright (c) 2013-2020 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br> 17 * BSD-style license. See <a href="http://opentrafficsim.org/node/13">OpenTrafficSim License</a>. 18 * <p> 19 * @version $Revision$, $LastChangedDate$, by $Author$, initial version 1 okt. 2016 <br> 20 * @author <a href="http://www.tbm.tudelft.nl/averbraeck">Alexander Verbraeck</a> 21 * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a> 22 * @author <a href="http://www.transport.citg.tudelft.nl">Wouter Schakel</a> 23 * @param <T> class of the value 24 */ 25 public abstract class AbstractIndicator<T extends DoubleScalarInterface> 26 { 27 28 /** Last query. */ 29 private Query<?> lastQuery; 30 31 /** Last start time. */ 32 private Time lastStartTime; 33 34 /** Last end time. */ 35 private Time lastEndTime; 36 37 /** Last value. */ 38 private T lastValue; 39 40 /** 41 * Get value for given query over time interval, returning earlier calculated value if possible. This method uses 42 * {@code Time.ZERO} as start time. 43 * @param query Query<G>; query 44 * @param endTime Time; start time of interval to calculate indicator over 45 * @param trajectoryGroups List<TrajectoryGroup<G>>; group of trajectories to calculate the indicator for 46 * @param <G> gtu data type 47 * @return value for given query 48 */ 49 public final <G extends GtuDataInterface> T getValue(final Query<G> query, final Time endTime, 50 final List<TrajectoryGroup<G>> trajectoryGroups) 51 { 52 return getValue(query, Time.ZERO, endTime, trajectoryGroups); 53 } 54 55 /** 56 * Get value for given query over time interval, returning earlier calculated value if possible. 57 * @param query Query<G>; query 58 * @param startTime Time; start time of interval to calculate indicator over 59 * @param endTime Time; start time of interval to calculate indicator over 60 * @param trajectoryGroups List<TrajectoryGroup<G>>; group of trajectories to calculate the indicator for 61 * @param <G> gtu data type 62 * @return value for given query 63 */ 64 public final <G extends GtuDataInterface> T getValue(final Query<G> query, final Time startTime, final Time endTime, 65 final List<TrajectoryGroup<G>> trajectoryGroups) 66 { 67 Throw.whenNull(query, "Query may not be null."); 68 Throw.whenNull(startTime, "Start time may not be null."); 69 Throw.whenNull(endTime, "End time may not be null."); 70 if (this.lastQuery == null || !this.lastQuery.equals(query) || !this.lastStartTime.equals(startTime) 71 || !this.lastEndTime.equals(endTime)) 72 { 73 this.lastQuery = query; 74 this.lastStartTime = startTime; 75 this.lastEndTime = endTime; 76 this.lastValue = calculate(query, startTime, endTime, trajectoryGroups); 77 } 78 return this.lastValue; 79 } 80 81 /** 82 * Calculate value for given trajectory group. 83 * @param query Query<G>; query 84 * @param startTime Time; start time of interval to calculate indicator over 85 * @param endTime Time; start time of interval to calculate indicator over 86 * @param trajectoryGroups List<TrajectoryGroup<G>>; group of trajectories to calculate the indicator for 87 * @param <G> gtu data type 88 * @return value for given trajectory group 89 */ 90 protected abstract <G extends GtuDataInterface> T calculate(final Query<G> query, final Time startTime, final Time endTime, 91 final List<TrajectoryGroup<G>> trajectoryGroups); 92 93 }