Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | |
| 17 | package org.onosproject.drivers.p4runtime.mirror; |
| 18 | |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame^] | 19 | import com.google.common.base.MoreObjects; |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 20 | import org.onosproject.net.pi.runtime.PiEntity; |
| 21 | import org.onosproject.store.service.WallClockTimestamp; |
| 22 | |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame^] | 23 | import java.util.Objects; |
| 24 | |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 25 | public class TimedEntry<E extends PiEntity> { |
| 26 | |
| 27 | private final long timestamp; |
| 28 | private final E entity; |
| 29 | |
| 30 | TimedEntry(long timestamp, E entity) { |
| 31 | this.timestamp = timestamp; |
| 32 | this.entity = entity; |
| 33 | } |
| 34 | |
| 35 | public long timestamp() { |
| 36 | return timestamp; |
| 37 | } |
| 38 | |
| 39 | public E entry() { |
| 40 | return entity; |
| 41 | } |
| 42 | |
| 43 | public long lifeSec() { |
| 44 | final long now = new WallClockTimestamp().unixTimestamp(); |
| 45 | return (now - timestamp) / 1000; |
| 46 | } |
Carmelo Cascone | b4863b3 | 2019-03-13 18:54:34 -0700 | [diff] [blame^] | 47 | |
| 48 | @Override |
| 49 | public int hashCode() { |
| 50 | return Objects.hash(timestamp, entity); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public boolean equals(Object obj) { |
| 55 | if (this == obj) { |
| 56 | return true; |
| 57 | } |
| 58 | if (obj == null || getClass() != obj.getClass()) { |
| 59 | return false; |
| 60 | } |
| 61 | final TimedEntry other = (TimedEntry) obj; |
| 62 | return Objects.equals(this.timestamp, other.timestamp) |
| 63 | && Objects.equals(this.entity, other.entity); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public String toString() { |
| 68 | return MoreObjects.toStringHelper(this) |
| 69 | .add("timestamp", timestamp) |
| 70 | .add("entity", entity) |
| 71 | .toString(); |
| 72 | } |
Carmelo Cascone | 6a0b5a3 | 2017-11-20 23:08:32 -0800 | [diff] [blame] | 73 | } |