MATSIM
Counts.java
Go to the documentation of this file.
1 package org.matsim.counts;
2 
3 import org.matsim.api.core.v01.Id;
10 
11 import java.util.Map;
12 import java.util.Set;
13 import java.util.TreeMap;
14 import java.util.stream.Collectors;
15 
21 public final class Counts<T extends Identifiable<T>> implements Attributable, MatsimToplevelContainer {
22 
23  public static final String ELEMENT_NAME = "counts";
24  private final Map<Id<T>, MeasurementLocation<T>> locations = new TreeMap<>();
25  private final Attributes attributes = new AttributesImpl();
26  private String name;
27  private String description;
28  private String source;
29  private int year;
30 
31  public Counts() {
32  }
33 
37  public MeasurementLocation<T> createAndAddMeasureLocation(final Id<T> id, String stationName) {
38 
39  if (this.locations.containsKey(id)) {
40  throw new RuntimeException("There is already a measurement object for location " + id.toString());
41  }
42 
43  MeasurementLocation<T> loc = new MeasurementLocation<T>(id, stationName);
44  this.locations.put(id, loc);
45 
46  return loc;
47  }
48 
49  public String getName() {
50  return name;
51  }
52 
53  public void setName(String name) {
54  this.name = name;
55  }
56 
57  public String getDescription() {
58  return description;
59  }
60 
61  public void setDescription(String description) {
62  this.description = description;
63  }
64 
65  public String getSource() {
66  return source;
67  }
68 
69  public void setSource(String source) {
70  this.source = source;
71  }
72 
73  public int getYear() {
74  return year;
75  }
76 
77  public void setYear(int year) {
78  this.year = year;
79  }
80 
84  public Set<String> getMeasurableTypes() {
85  return locations.values().stream()
86  .map(MeasurementLocation::getMeasurables)
87  .flatMap(e -> e.keySet().stream())
88  .map(MeasurementLocation.TypeAndMode::type)
89  .collect(Collectors.toSet());
90  }
91 
93  return locations;
94  }
95 
97  return this.locations.get(id);
98  }
99 
101  // These functions belong to the older Counts API and are kept for compatibility
102  // Consider using the new API, which provides more flexibility (createAndAddLocation, getMeasureLocations)
103 
113  @Deprecated
114  public final Count<T> createAndAddCount(final Id<T> linkId, final String stationName) {
115  MeasurementLocation<T> location = createAndAddMeasureLocation(linkId, stationName);
116  return new Count<>(location);
117  }
118 
124  @Deprecated
125  public final TreeMap<Id<T>, Count<T>> getCounts() {
126  TreeMap<Id<T>, Count<T>> result = new TreeMap<>();
127 
128  for (Map.Entry<Id<T>, MeasurementLocation<T>> e : locations.entrySet()) {
129  result.put(e.getKey(), new Count<>(e.getValue()));
130  }
131 
132  return result;
133  }
134 
138  @Deprecated
139  public Count<T> getCount(final Id<T> locId) {
141  return loc == null ? null : new Count<>(loc);
142  }
143 
144 
145  @Override
147  return attributes;
148  }
149 
150  @Override
151  public String toString() {
152  return "[name=" + this.name + "]" + "[nof_locations=" + this.locations.size() + "]";
153  }
154 
155  @Override public MatsimFactory getFactory(){
156  throw new RuntimeException( "not implemented" );
157  // yy I need this to fulfill MatsimToplevelContainer. Which I need to be able to use ProjectionUtils.putCRS(...) in the readers/writers. kai, feb'24
158  }
159 }
MatsimFactory getFactory()
Definition: Counts.java:155
Count< T > getCount(final Id< T > locId)
Definition: Counts.java:139
void setSource(String source)
Definition: Counts.java:69
final TreeMap< Id< T >, Count< T > > getCounts()
Definition: Counts.java:125
MeasurementLocation< T > createAndAddMeasureLocation(final Id< T > id, String stationName)
Definition: Counts.java:37
Attributes getAttributes()
Definition: Counts.java:146
void setName(String name)
Definition: Counts.java:53
Map< Id< T >, MeasurementLocation< T > > getMeasureLocations()
Definition: Counts.java:92
final Map< Id< T >, MeasurementLocation< T > > locations
Definition: Counts.java:24
void setDescription(String description)
Definition: Counts.java:61
void setYear(int year)
Definition: Counts.java:77
static final String ELEMENT_NAME
Definition: Counts.java:23
final Attributes attributes
Definition: Counts.java:25
Set< String > getMeasurableTypes()
Definition: Counts.java:84
final Count< T > createAndAddCount(final Id< T > linkId, final String stationName)
Old API.
Definition: Counts.java:114
MeasurementLocation< T > getMeasureLocation(Id< T > id)
Definition: Counts.java:96