blob: a0f485ab64e81a062da62c9e77694c8e35269758 [file] [log] [blame]
Madan Jampani61056bc2014-09-27 09:07:26 -07001package org.onlab.onos.store.device.impl;
2
Yuta HIGUCHI55a2e462014-10-01 09:06:59 -07003import java.util.Objects;
4
Madan Jampani61056bc2014-09-27 09:07:26 -07005import org.onlab.onos.store.Timestamp;
6
7/**
Yuta HIGUCHI2e963892014-09-27 13:00:39 -07008 * Wrapper class for a entity that is versioned
Madan Jampani61056bc2014-09-27 09:07:26 -07009 * and can either be up or down.
10 *
11 * @param <T> type of the value.
12 */
13public class VersionedValue<T> {
14 private final T entity;
15 private final Timestamp timestamp;
16 private final boolean isUp;
Yuta HIGUCHI2e963892014-09-27 13:00:39 -070017
Madan Jampani61056bc2014-09-27 09:07:26 -070018 public VersionedValue(T entity, boolean isUp, Timestamp timestamp) {
19 this.entity = entity;
20 this.isUp = isUp;
21 this.timestamp = timestamp;
22 }
Yuta HIGUCHI2e963892014-09-27 13:00:39 -070023
Madan Jampani61056bc2014-09-27 09:07:26 -070024 /**
25 * Returns the value.
26 * @return value.
27 */
28 public T entity() {
29 return entity;
30 }
Yuta HIGUCHI2e963892014-09-27 13:00:39 -070031
Madan Jampani61056bc2014-09-27 09:07:26 -070032 /**
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 HIGUCHI2e963892014-09-27 13:00:39 -070039
Madan Jampani61056bc2014-09-27 09:07:26 -070040 /**
41 * Returns the timestamp (version) associated with this entity.
42 * @return timestamp.
43 */
44 public Timestamp timestamp() {
45 return timestamp;
46 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070047
48
Yuta HIGUCHI55a2e462014-10-01 09:06:59 -070049 @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 HIGUCHI533ec322014-09-30 13:29:52 -070072 // Default constructor for serializer
73 protected VersionedValue() {
74 this.entity = null;
75 this.isUp = false;
76 this.timestamp = null;
77 }
Madan Jampani61056bc2014-09-27 09:07:26 -070078}