Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Ray Milkey | 34c9590 | 2015-04-15 09:47:53 -0700 | [diff] [blame] | 2 | * Copyright 2014-2015 Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.event; |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 17 | |
Yuta HIGUCHI | b87ef95 | 2014-10-28 23:34:23 -0700 | [diff] [blame] | 18 | import org.joda.time.LocalDateTime; |
| 19 | |
Marc De Leenheer | b473b9d | 2015-02-06 15:21:03 -0800 | [diff] [blame] | 20 | import static com.google.common.base.MoreObjects.toStringHelper; |
| 21 | |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 22 | /** |
tom | 96dfcab | 2014-08-28 09:26:03 -0700 | [diff] [blame] | 23 | * Base event implementation. |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 24 | */ |
Sho SHIMIZU | 9e97826 | 2014-11-13 17:57:10 -0800 | [diff] [blame] | 25 | public class AbstractEvent<T extends Enum, S> implements Event<T, S> { |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 26 | |
| 27 | private final long time; |
| 28 | private final T type; |
Ray Milkey | 506455f | 2014-11-12 13:18:35 -0800 | [diff] [blame] | 29 | private final S subject; |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 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 | |
tom | 1a17603 | 2014-09-10 12:21:23 -0700 | [diff] [blame] | 70 | @Override |
| 71 | public String toString() { |
Yuta HIGUCHI | b87ef95 | 2014-10-28 23:34:23 -0700 | [diff] [blame] | 72 | return toStringHelper(this) |
| 73 | .add("time", new LocalDateTime(time)) |
| 74 | .add("type", type()) |
| 75 | .add("subject", subject()) |
| 76 | .toString(); |
tom | 1a17603 | 2014-09-10 12:21:23 -0700 | [diff] [blame] | 77 | } |
tom | e33cc1a | 2014-08-25 21:59:41 -0700 | [diff] [blame] | 78 | } |