MATSIM
Measurable.java
Go to the documentation of this file.
1 package org.matsim.counts;
2 
3 import it.unimi.dsi.fastutil.ints.Int2DoubleAVLTreeMap;
4 import it.unimi.dsi.fastutil.ints.Int2DoubleMap;
5 import it.unimi.dsi.fastutil.ints.Int2DoubleSortedMap;
6 
7 import java.util.Iterator;
8 import java.util.Objects;
9 import java.util.OptionalDouble;
10 
16 public final class Measurable implements Iterable<Int2DoubleMap.Entry> {
17 
21  public static final String ANY_MODE = "any_vehicle";
22  static final String ELEMENT_NAME = "measurements";
23  public static String VOLUMES = "volumes";
24  public static String VELOCITIES = "velocities";
25  public static String PASSENGERS = "passengers";
26 
30  public static int DAILY = 24 * 60 * 60;
34  public static int HOURLY = 60 * 60;
38  public static int QUARTER_HOURLY = 15 * 60;
39 
40  private final String type;
41  private final String mode;
42 
43  private final Int2DoubleSortedMap values;
44 
48  private final int interval;
49 
50  Measurable(String mode, String type, int interval) {
51  this.mode = mode;
52  this.type = type;
53  this.values = new Int2DoubleAVLTreeMap();
54  this.interval = interval;
55  }
56 
57  Int2DoubleMap getValues() {
58  return values;
59  }
60 
64  public void setAtHour(int hour, double value) {
65  setAtSecond(hour * HOURLY, value);
66  }
67 
72  public void setAtMinute(int minute, double value) {
73  setAtSecond(minute * 60, value);
74  }
75 
76  public void setAtSecond(int seconds, double value) {
77  if (seconds < 0)
78  throw new IllegalArgumentException("Time value starts at 0.");
79 
80  if (seconds % this.interval != 0)
81  throw new IllegalArgumentException("Time value doesn't match the interval!");
82 
83  this.values.put(seconds, value);
84  }
85 
89  public OptionalDouble getDailyValue() {
90  if (interval != DAILY)
91  throw new IllegalArgumentException("Does not contain daily values!");
92 
93  return getAtHour(0);
94  }
95 
96  public void setDailyValue(double value) {
97  if (interval != DAILY)
98  throw new IllegalArgumentException("Does not contain daily values!");
99 
100  setAtHour(0, value);
101  }
102 
106  public double aggregateDaily() {
107  if (interval == DAILY)
108  return values.get(0);
109 
110  return values.values().doubleStream().sum();
111  }
112 
116  public OptionalDouble getAtHour(int hour) {
117  return getAtSecond(hour * HOURLY);
118  }
119 
123  public boolean supportsHourlyAggregate() {
124  return (interval <= HOURLY) && (HOURLY % interval) == 0;
125  }
126 
130  public OptionalDouble aggregateAtHour(int hour) {
132  throw new IllegalArgumentException("Can not aggregate hourly values.");
133 
134  if (interval == HOURLY)
135  return getAtHour(hour);
136 
137  Int2DoubleSortedMap values = this.values.subMap(hour * HOURLY, (hour + 1) * HOURLY);
138  if (values.isEmpty())
139  return OptionalDouble.empty();
140 
141  return OptionalDouble.of(values.values().doubleStream().sum());
142  }
143 
144  public OptionalDouble getAtSecond(int second) {
145  if (values.containsKey(second))
146  return OptionalDouble.of(values.get(second));
147 
148  return OptionalDouble.empty();
149  }
150 
154  public OptionalDouble getAtMinute(int minutes) {
155  return getAtSecond(minutes * 60);
156  }
157 
161  public String getMode() {
162  return mode;
163  }
164 
168  public String getMeasurableType() {
169  return type;
170  }
171 
172  public int getInterval() {
173  return interval;
174  }
175 
179  @Override
180  public Iterator<Int2DoubleMap.Entry> iterator() {
181  return values.int2DoubleEntrySet().iterator();
182  }
183 
187  public int size() {
188  return values.size();
189  }
190 
191  @Override
192  public boolean equals(Object o) {
193  if (this == o) return true;
194  if (o == null || getClass() != o.getClass()) return false;
195  Measurable entries = (Measurable) o;
196  return interval == entries.interval && Objects.equals(type, entries.type) && Objects.equals(mode, entries.mode) && Objects.equals(values, entries.values);
197  }
198 
199  @Override
200  public int hashCode() {
201  return Objects.hash(type, mode, values, interval);
202  }
203 }
204 
Iterator< Int2DoubleMap.Entry > iterator()
void setAtMinute(int minute, double value)
Definition: Measurable.java:72
static final String ANY_MODE
Definition: Measurable.java:21
OptionalDouble getAtSecond(int second)
OptionalDouble getDailyValue()
Definition: Measurable.java:89
final Int2DoubleSortedMap values
Definition: Measurable.java:43
void setAtSecond(int seconds, double value)
Definition: Measurable.java:76
OptionalDouble getAtHour(int hour)
void setDailyValue(double value)
Definition: Measurable.java:96
OptionalDouble getAtMinute(int minutes)
void setAtHour(int hour, double value)
Definition: Measurable.java:64
OptionalDouble aggregateAtHour(int hour)
boolean equals(Object o)