blob: 4541f2ce5851ec99720f272371bd6e3cf04f0f3a [file] [log] [blame]
Madan Jampani25461112015-02-17 14:17:29 -08001/*
2 * Copyright 2015 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 */
16
Madan Jampani393e0f02015-02-12 07:35:39 +053017package org.onosproject.store.service;
Madan Jampani94c23532015-02-05 17:40:01 -080018
Madan Jampani30a57f82015-03-02 12:19:41 -080019import org.joda.time.DateTime;
20
Madan Jampani94c23532015-02-05 17:40:01 -080021import com.google.common.base.MoreObjects;
22
23/**
24 * Versioned value.
25 *
26 * @param <V> value type.
27 */
28public class Versioned<V> {
29
30 private final V value;
31 private final long version;
Madan Jampani30a57f82015-03-02 12:19:41 -080032 private final long creationTime;
33
34 /**
35 * Constructs a new versioned value.
36 * @param value value
37 * @param version version
38 * @param creationTime milliseconds of the creation event
39 * from the Java epoch of 1970-01-01T00:00:00Z
40 */
41 public Versioned(V value, long version, long creationTime) {
42 this.value = value;
43 this.version = version;
Madan Jampania8620462015-06-04 20:16:12 -070044 this.creationTime = creationTime;
Madan Jampani30a57f82015-03-02 12:19:41 -080045 }
Madan Jampani94c23532015-02-05 17:40:01 -080046
47 /**
48 * Constructs a new versioned value.
49 * @param value value
50 * @param version version
51 */
52 public Versioned(V value, long version) {
Madan Jampani30a57f82015-03-02 12:19:41 -080053 this(value, version, System.currentTimeMillis());
Madan Jampani94c23532015-02-05 17:40:01 -080054 }
55
56 /**
57 * Returns the value.
58 *
59 * @return value.
60 */
61 public V value() {
62 return value;
63 }
64
65 /**
66 * Returns the version.
67 *
68 * @return version
69 */
70 public long version() {
71 return version;
72 }
73
Madan Jampani30a57f82015-03-02 12:19:41 -080074 /**
75 * Returns the system time when this version was created.
76 * <p>
77 * Care should be taken when relying on creationTime to
78 * implement any behavior in a distributed setting. Due
79 * to the possibility of clock skew it is likely that
80 * even creationTimes of causally related versions can be
81 * out or order.
82 * @return creation time
83 */
84 public long creationTime() {
85 return creationTime;
86 }
87
Madan Jampani94c23532015-02-05 17:40:01 -080088 @Override
89 public String toString() {
90 return MoreObjects.toStringHelper(this)
91 .add("value", value)
92 .add("version", version)
Madan Jampani30a57f82015-03-02 12:19:41 -080093 .add("creationTime", new DateTime(creationTime))
Madan Jampani94c23532015-02-05 17:40:01 -080094 .toString();
95 }
96}