blob: 0fc8eaa2eb0a640efd181b98919e5da25feb0bb9 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
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 */
tome33cc1a2014-08-25 21:59:41 -070016package org.onlab.onos.event;
17
tom1a176032014-09-10 12:21:23 -070018import static com.google.common.base.MoreObjects.toStringHelper;
19
Yuta HIGUCHIb87ef952014-10-28 23:34:23 -070020import org.joda.time.LocalDateTime;
21
tome33cc1a2014-08-25 21:59:41 -070022/**
tom96dfcab2014-08-28 09:26:03 -070023 * Base event implementation.
tome33cc1a2014-08-25 21:59:41 -070024 */
25public class AbstractEvent<T extends Enum, S extends Object> implements Event<T, S> {
26
27 private final long time;
28 private final T type;
29 private S subject;
30
31 /**
32 * Creates an event of a given type and for the specified subject and the
33 * current time.
34 *
35 * @param type event type
36 * @param subject event subject
37 */
38 protected AbstractEvent(T type, S subject) {
39 this(type, subject, System.currentTimeMillis());
40 }
41
42 /**
43 * Creates an event of a given type and for the specified subject and time.
44 *
45 * @param type event type
46 * @param subject event subject
47 * @param time occurrence time
48 */
49 protected AbstractEvent(T type, S subject, long time) {
50 this.type = type;
51 this.subject = subject;
52 this.time = time;
53 }
54
55 @Override
56 public long time() {
57 return time;
58 }
59
60 @Override
61 public T type() {
62 return type;
63 }
64
65 @Override
66 public S subject() {
67 return subject;
68 }
69
tom1a176032014-09-10 12:21:23 -070070 @Override
71 public String toString() {
Yuta HIGUCHIb87ef952014-10-28 23:34:23 -070072 return toStringHelper(this)
73 .add("time", new LocalDateTime(time))
74 .add("type", type())
75 .add("subject", subject())
76 .toString();
tom1a176032014-09-10 12:21:23 -070077 }
78
tome33cc1a2014-08-25 21:59:41 -070079}