/
Event.java
142 lines (127 loc) · 3.85 KB
/
Event.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014 Sebastian RODRIGUEZ, Nicolas GAUD, Stéphane GALLAND.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.sarl.lang.core;
import java.io.Serializable;
import java.util.UUID;
/**
* Elementary interaction unit inside an {@link EventSpace} An event is the
* specification of some occurrence in a Space that may potentially trigger
* effects by a listener. Within a Space, the notion of {@link Scope} enables to
* precisely control/filter the potential recipients of an event.
*
* @author $Author: srodriguez$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
public abstract class Event implements Serializable {
private static final long serialVersionUID = -4314428111329600890L;
private Address source;
/** Constructs an Event without source.
* The source must be set with {@link #setSource(Address)}
* by the creator of the event, or by the emitting mechanism,
* before sending the event on the event bus.
*/
public Event() {
//
}
/** Constructs an Event with a source.
* @param source - source of the event.
*/
public Event(Address source) {
this.source = source;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj != null && getClass().equals(obj.getClass())) {
Event e = (Event) obj;
return (this.source == null && e.source == null)
|| (this.source != null && this.source.equals(e.source));
}
return false;
}
@Override
public int hashCode() {
int h = 31 + getClass().hashCode();
if (this.source != null) {
h = h * 31 + this.source.hashCode();
}
return h;
}
/**
* The source of the event.
*
* @return the source of the event.
*/
public Address getSource() {
return this.source;
}
/**
* The address of the source of this event.
*
* @param source - the source of the event.
*/
public void setSource(Address source) {
this.source = source;
}
/**
* Returns a String representation of the Event E1 attributes only.
*
* @return the string representation of the attributes of this Event.
*/
protected String attributesToString() {
StringBuilder result = new StringBuilder();
result.append("source = "); //$NON-NLS-1$
result.append(this.source);
return result.toString();
}
@Override
public String toString() {
return getClass().getSimpleName()
+ " [" + attributesToString() //$NON-NLS-1$
+ "]"; //$NON-NLS-1$
}
/** Replies if the event was emitted by an entity with the given address.
*
* @param address - the address of the emitter to test.
* @return <code>true</code> if the given event was emitted by
* an entity with the given address; otherwise <code>false</code>.
* @since 0.2
*/
public boolean isFrom(Address address) {
return (address != null) && address.equals(getSource());
}
/** Replies if the event was emitted by an entity with the given identifier.
*
* @param entityId - the identifier of the emitter to test.
* @return <code>true</code> if the given event was emitted by
* an entity with the given identifier; otherwise <code>false</code>.
* @since 0.2
*/
public boolean isFrom(UUID entityId) {
Address source = getSource();
return (entityId != null) && (source != null)
&& entityId.equals(source.getUUID());
}
}