Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 1 | package org.onlab.onos.store.device.impl; |
| 2 | |
Yuta HIGUCHI | 55a2e46 | 2014-10-01 09:06:59 -0700 | [diff] [blame^] | 3 | import java.util.Objects; |
| 4 | |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 5 | import org.onlab.onos.store.Timestamp; |
| 6 | |
| 7 | /** |
Yuta HIGUCHI | 2e96389 | 2014-09-27 13:00:39 -0700 | [diff] [blame] | 8 | * Wrapper class for a entity that is versioned |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 9 | * and can either be up or down. |
| 10 | * |
| 11 | * @param <T> type of the value. |
| 12 | */ |
| 13 | public class VersionedValue<T> { |
| 14 | private final T entity; |
| 15 | private final Timestamp timestamp; |
| 16 | private final boolean isUp; |
Yuta HIGUCHI | 2e96389 | 2014-09-27 13:00:39 -0700 | [diff] [blame] | 17 | |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 18 | public VersionedValue(T entity, boolean isUp, Timestamp timestamp) { |
| 19 | this.entity = entity; |
| 20 | this.isUp = isUp; |
| 21 | this.timestamp = timestamp; |
| 22 | } |
Yuta HIGUCHI | 2e96389 | 2014-09-27 13:00:39 -0700 | [diff] [blame] | 23 | |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 24 | /** |
| 25 | * Returns the value. |
| 26 | * @return value. |
| 27 | */ |
| 28 | public T entity() { |
| 29 | return entity; |
| 30 | } |
Yuta HIGUCHI | 2e96389 | 2014-09-27 13:00:39 -0700 | [diff] [blame] | 31 | |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Tells whether the entity is up or down. |
| 34 | * @return true if up, false otherwise. |
| 35 | */ |
| 36 | public boolean isUp() { |
| 37 | return isUp; |
| 38 | } |
Yuta HIGUCHI | 2e96389 | 2014-09-27 13:00:39 -0700 | [diff] [blame] | 39 | |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Returns the timestamp (version) associated with this entity. |
| 42 | * @return timestamp. |
| 43 | */ |
| 44 | public Timestamp timestamp() { |
| 45 | return timestamp; |
| 46 | } |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 47 | |
| 48 | |
Yuta HIGUCHI | 55a2e46 | 2014-10-01 09:06:59 -0700 | [diff] [blame^] | 49 | @Override |
| 50 | public int hashCode() { |
| 51 | return Objects.hash(entity, timestamp, isUp); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public boolean equals(Object obj) { |
| 56 | if (this == obj) { |
| 57 | return true; |
| 58 | } |
| 59 | if (obj == null) { |
| 60 | return false; |
| 61 | } |
| 62 | if (getClass() != obj.getClass()) { |
| 63 | return false; |
| 64 | } |
| 65 | @SuppressWarnings("unchecked") |
| 66 | VersionedValue<T> that = (VersionedValue<T>) obj; |
| 67 | return Objects.equals(this.entity, that.entity) && |
| 68 | Objects.equals(this.timestamp, that.timestamp) && |
| 69 | Objects.equals(this.isUp, that.isUp); |
| 70 | } |
| 71 | |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 72 | // Default constructor for serializer |
| 73 | protected VersionedValue() { |
| 74 | this.entity = null; |
| 75 | this.isUp = false; |
| 76 | this.timestamp = null; |
| 77 | } |
Madan Jampani | 61056bc | 2014-09-27 09:07:26 -0700 | [diff] [blame] | 78 | } |