Skip to content

Commit a8d3971

Browse files
DerStoeckida-Kai
andauthored
Common: Eventbuilder received build method and EventReader received toString + hashCode + equals method (OpenEMS#2408)
* EventBuilder received a Build method for new Events and * EventReader received toString, hashCode and equals method This commit is one of many prepare PRs / commits for a bigger PR: SumStateAlert OpenEMS#2260 By Seperating the big OpenEMS#2260 into smaller commits we would like to make the review easier. Co-authored-by: Kai Jeschek <99220919+da-Kai@users.noreply.github.com>
1 parent 5fc3d44 commit a8d3971

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

io.openems.common/src/io/openems/common/event/EventBuilder.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static EventBuilder from(EventAdmin eventAdmin, String eventTopic) {
3636
* @throws SecurityException If the caller does not have
3737
* TopicPermission[topic,PUBLISH] for the topic
3838
* specified in the event.
39-
* @see org.osgi.service.event.EventAdmin#postEvent(Event)
39+
* @see EventAdmin#postEvent(Event)
4040
*/
4141
public static void post(EventAdmin eventAdmin, String eventTopic) {
4242
eventAdmin.postEvent(new Event(eventTopic, Map.of()));
@@ -48,10 +48,10 @@ public static void post(EventAdmin eventAdmin, String eventTopic) {
4848
* @throws SecurityException If the caller does not have
4949
* TopicPermission[topic,PUBLISH] for the topic
5050
* specified in the event.
51-
* @see org.osgi.service.event.EventAdmin#postEvent(Event)
51+
* @see EventAdmin#postEvent(Event)
5252
*/
5353
public void post() throws SecurityException {
54-
this.eventAdmin.postEvent(new Event(this.eventTopic, this.eventArgs));
54+
this.eventAdmin.postEvent(this.build());
5555
}
5656

5757
/**
@@ -62,7 +62,7 @@ public void post() throws SecurityException {
6262
* @throws SecurityException If the caller does not have
6363
* TopicPermission[topic,PUBLISH] for the topic
6464
* specified in the event.
65-
* @see org.osgi.service.event.EventAdmin#sendEvent(Event)
65+
* @see EventAdmin#sendEvent(Event)
6666
*/
6767
public static void send(EventAdmin eventAdmin, String eventTopic) {
6868
eventAdmin.sendEvent(new Event(eventTopic, Map.of()));
@@ -74,10 +74,19 @@ public static void send(EventAdmin eventAdmin, String eventTopic) {
7474
* @throws SecurityException If the caller does not have
7575
* TopicPermission[topic,PUBLISH] for the topic
7676
* specified in the event.
77-
* @see org.osgi.service.event.EventAdmin#sendEvent(Event)
77+
* @see EventAdmin#sendEvent(Event)
7878
*/
7979
public void send() throws SecurityException {
80-
this.eventAdmin.sendEvent(new Event(this.eventTopic, this.eventArgs));
80+
this.eventAdmin.sendEvent(this.build());
81+
}
82+
83+
/**
84+
* Build event and return it.
85+
*
86+
* @return built {@link Event}
87+
*/
88+
public Event build() {
89+
return new Event(this.eventTopic, this.eventArgs);
8190
}
8291

8392
/**

io.openems.common/src/io/openems/common/event/EventReader.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.time.ZoneId;
44
import java.time.ZonedDateTime;
5+
import java.util.Objects;
56

67
import org.osgi.service.event.Event;
78

@@ -116,4 +117,31 @@ public ZonedDateTime getZonedDateTime(String propertyId, String timeZone) {
116117
return this.getZonedDateTime(propertyId, ZoneId.of(timeZone));
117118
}
118119

120+
@Override
121+
public String toString() {
122+
StringBuilder asString = new StringBuilder("EventReader{Event={Topic=");
123+
asString.append(this.getTopic()).append(", Params={");
124+
for (var property : this.event.getPropertyNames()) {
125+
if (property.equals("event.topics")) {
126+
break;
127+
}
128+
asString.append(property).append("=").append(this.event.getProperty(property)).append(", ");
129+
}
130+
asString.delete(asString.length() - 2, asString.length());
131+
asString.append("}}}");
132+
return asString.toString();
133+
}
134+
135+
@Override
136+
public int hashCode() {
137+
return Objects.hash(this.event);
138+
}
139+
140+
@Override
141+
public boolean equals(Object obj) {
142+
return this == obj //
143+
|| obj instanceof EventReader other //
144+
&& Objects.equals(this.event, other.event);
145+
}
146+
119147
}

0 commit comments

Comments
 (0)