Import atomix/{storage,utils}
[controller.git] / third-party / atomix / utils / src / main / java / io / atomix / utils / event / AbstractEvent.java
1 /*
2  * Copyright 2014-present Open Networking Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package io.atomix.utils.event;
17
18 import io.atomix.utils.misc.TimestampPrinter;
19
20 import static com.google.common.base.MoreObjects.toStringHelper;
21
22 /**
23  * Base event implementation.
24  */
25 public class AbstractEvent<T extends Enum, S> implements Event<T, S> {
26   private final long time;
27   private final T type;
28   private final S subject;
29
30   /**
31    * Creates an event of a given type and for the specified subject and the
32    * current time.
33    *
34    * @param type    event type
35    * @param subject event subject
36    */
37   protected AbstractEvent(T type, S subject) {
38     this(type, subject, System.currentTimeMillis());
39   }
40
41   /**
42    * Creates an event of a given type and for the specified subject and time.
43    *
44    * @param type    event type
45    * @param subject event subject
46    * @param time    occurrence time
47    */
48   protected AbstractEvent(T type, S subject, long time) {
49     this.type = type;
50     this.subject = subject;
51     this.time = time;
52   }
53
54   @Override
55   public long time() {
56     return time;
57   }
58
59   @Override
60   public T type() {
61     return type;
62   }
63
64   @Override
65   public S subject() {
66     return subject;
67   }
68
69   @Override
70   public String toString() {
71     return toStringHelper(this)
72         .add("time", new TimestampPrinter(time))
73         .add("type", type())
74         .add("subject", subject())
75         .toString();
76   }
77 }