MATSIM
Event.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * Event.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007, 2008 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.api.core.v01.events;
22 
23 import java.util.LinkedHashMap;
24 import java.util.Map;
25 
28 
30 
31 public abstract class Event {
32 
33  public final static String ATTRIBUTE_TIME = "time";
34  public final static String ATTRIBUTE_TYPE = "type";
35  public static final String ATTRIBUTE_X = "x" ;
36  public static final String ATTRIBUTE_Y = "y" ;
37 
38  private double time;
39 
40  public Event(final double time) {
41  this.time = time;
42  }
43 
44  public Map<String, String> getAttributes() {
45  Map<String, String> attr = new LinkedHashMap<>();
46  attr.put(ATTRIBUTE_TIME, Double.toString(this.time));
47  attr.put(ATTRIBUTE_TYPE, getEventType());
48  if (this instanceof HasPersonId hasPersonId && hasPersonId.getPersonId() != null) {
49  attr.put(HasPersonId.ATTRIBUTE_PERSON, hasPersonId.getPersonId().toString());
50  // many derived types do this by themselves, for historical reasons. Since the information is put into a map, it still exists only once under that key. kai,
51  // mar'19
52  }
53  if (this instanceof HasFacilityId hasFacilityId && hasFacilityId.getFacilityId() != null) {
54  attr.put(HasFacilityId.ATTRIBUTE_FACILITY, hasFacilityId.getFacilityId().toString());
55  }
56  if (this instanceof HasLinkId hasLinkId && hasLinkId.getLinkId() != null) {
57  attr.put(HasLinkId.ATTRIBUTE_LINK, hasLinkId.getLinkId().toString());
58  }
59  if (this instanceof BasicLocation basicLocation && basicLocation.getCoord() != null) {
60  if (((BasicLocation)this).getCoord() != null) {
61  attr.put(ATTRIBUTE_X, String.valueOf(basicLocation.getCoord().getX()));
62  attr.put(ATTRIBUTE_Y, String.valueOf(basicLocation.getCoord().getY()));
63  }
64  }
65  if (this instanceof HasVehicleId hasVehicleId && hasVehicleId.getVehicleId() != null) {
66  attr.put(HasVehicleId.ATTRIBUTE_VEHICLE, hasVehicleId.getVehicleId().toString());
67  }
68  return attr;
69  }
70 
72  abstract public String getEventType();
73 
74  public final double getTime() {
75  return this.time;
76  }
77 
78  public void setTime(double time) {
79  this.time = time;
80  }
81 
82  public String toString() {
83  Map<String,String> attr = this.getAttributes() ;
84  StringBuilder eventXML = new StringBuilder("\t<event ");
85  for (Map.Entry<String, String> entry : attr.entrySet()) {
86  eventXML.append(entry.getKey());
87  eventXML.append("=\"");
88  eventXML.append(entry.getValue());
89  eventXML.append("\" ");
90  }
91  eventXML.append(" />");
92  return eventXML.toString();
93  }
94 
95  @Override
96  public boolean equals(Object obj) {
97  if (!(obj instanceof Event)) {
98  return false;
99  } else {
100  Event other = (Event) obj;
101  return time == other.time &&
102  getEventType().equals(other.getEventType()) &&
103  getAttributes().equals(other.getAttributes());
104  }
105  }
106 
107  @Override
108  public int hashCode() {
109  return getAttributes().hashCode(); // Two equal events must at least have the same attributes, so they will get the same hashCode like this.
110  }
111 
112 
116  protected final void writeXMLStart(StringBuilder out) {
117  out.append("\t<event time=\"").append(time).append("\" type=\"");
118  writeEncodedAttributeValue(out, getEventType()).append("\" ");
119 
120  if (this instanceof HasPersonId hasPersonId && hasPersonId.getPersonId() != null) {
121  out.append("person=\"");
122  writeEncodedAttributeValue(out, hasPersonId.getPersonId().toString()).append("\" ");
123  }
124 
125  if (this instanceof HasFacilityId hasFacilityId && hasFacilityId.getFacilityId() != null) {
126  out.append("facility=\"");
127  writeEncodedAttributeValue(out, hasFacilityId.getFacilityId().toString()).append("\" ");
128  }
129 
130  if (this instanceof HasLinkId hasLinkId && hasLinkId.getLinkId() != null) {
131  out.append("link=\"");
132  writeEncodedAttributeValue(out, hasLinkId.getLinkId().toString()).append("\" ");
133  }
134 
135  if (this instanceof BasicLocation basicLocation && basicLocation.getCoord() != null) {
136  if (basicLocation.getCoord() != null) {
137  out.append("x=\"").append(basicLocation.getCoord().getX()).append("\" ");
138  out.append("y=\"").append(basicLocation.getCoord().getY()).append("\" ");
139  }
140  }
141 
142  if (this instanceof HasVehicleId hasVehicleId && hasVehicleId.getVehicleId() != null) {
143  out.append("vehicle=\"");
144  writeEncodedAttributeValue(out, hasVehicleId.getVehicleId().toString()).append("\" ");
145  }
146  }
147 
151  protected final void writeXMLEnd(StringBuilder out) {
152  out.append(" />\n");
153  }
154 
162  public void writeAsXML(StringBuilder out) {
163  out.append("\t<event ");
164  Map<String, String> attr = getAttributes();
165  for (Map.Entry<String, String> entry : attr.entrySet()) {
166  out.append(entry.getKey());
167  out.append("=\"");
168  out.append(XmlUtils.encodeAttributeValue(entry.getValue()));
169  out.append("\" ");
170  }
171  out.append(" />\n");
172  }
173 
174 }
175 
176 
final void writeXMLEnd(StringBuilder out)
Definition: Event.java:151
void writeAsXML(StringBuilder out)
Definition: Event.java:162
static String encodeAttributeValue(final String attributeValue)
Definition: XmlUtils.java:41
static final String ATTRIBUTE_TYPE
Definition: Event.java:34
static final String ATTRIBUTE_TIME
Definition: Event.java:33
static final String ATTRIBUTE_X
Definition: Event.java:35
Map< String, String > getAttributes()
Definition: Event.java:44
final void writeXMLStart(StringBuilder out)
Definition: Event.java:116
boolean equals(Object obj)
Definition: Event.java:96
static StringBuilder writeEncodedAttributeValue(StringBuilder out, String attributeValue)
Definition: XmlUtils.java:89
static final String ATTRIBUTE_Y
Definition: Event.java:36