blob: f0e70bdda3ddc4f6ea1b74129e2324c3e793b441 [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 Jampanic6069562015-06-12 13:22:45 -070019import java.util.function.Function;
20
Madan Jampani30a57f82015-03-02 12:19:41 -080021import org.joda.time.DateTime;
22
Madan Jampani94c23532015-02-05 17:40:01 -080023import com.google.common.base.MoreObjects;
24
25/**
26 * Versioned value.
27 *
28 * @param <V> value type.
29 */
30public class Versioned<V> {
31
32 private final V value;
33 private final long version;
Madan Jampani30a57f82015-03-02 12:19:41 -080034 private final long creationTime;
35
36 /**
37 * Constructs a new versioned value.
38 * @param value value
39 * @param version version
40 * @param creationTime milliseconds of the creation event
41 * from the Java epoch of 1970-01-01T00:00:00Z
42 */
43 public Versioned(V value, long version, long creationTime) {
44 this.value = value;
45 this.version = version;
Madan Jampania8620462015-06-04 20:16:12 -070046 this.creationTime = creationTime;
Madan Jampani30a57f82015-03-02 12:19:41 -080047 }
Madan Jampani94c23532015-02-05 17:40:01 -080048
49 /**
50 * Constructs a new versioned value.
51 * @param value value
52 * @param version version
53 */
54 public Versioned(V value, long version) {
Madan Jampani30a57f82015-03-02 12:19:41 -080055 this(value, version, System.currentTimeMillis());
Madan Jampani94c23532015-02-05 17:40:01 -080056 }
57
58 /**
59 * Returns the value.
60 *
61 * @return value.
62 */
63 public V value() {
64 return value;
65 }
66
67 /**
68 * Returns the version.
69 *
70 * @return version
71 */
72 public long version() {
73 return version;
74 }
75
Madan Jampani30a57f82015-03-02 12:19:41 -080076 /**
77 * Returns the system time when this version was created.
78 * <p>
79 * Care should be taken when relying on creationTime to
80 * implement any behavior in a distributed setting. Due
81 * to the possibility of clock skew it is likely that
82 * even creationTimes of causally related versions can be
83 * out or order.
84 * @return creation time
85 */
86 public long creationTime() {
87 return creationTime;
88 }
89
Madan Jampanic6069562015-06-12 13:22:45 -070090 /**
91 * Maps this instance into another after transforming its
92 * value while retaining the same version and creationTime.
93 * @param transformer function to mapping the value
Madan Jampani0dbac7a2015-06-25 10:37:45 -070094 * @param <U> value type of the returned instance
Madan Jampanic6069562015-06-12 13:22:45 -070095 * @return mapped instance
96 */
97 public <U> Versioned<U> map(Function<V, U> transformer) {
98 return new Versioned<>(transformer.apply(value), version, creationTime);
99 }
100
Madan Jampani94c23532015-02-05 17:40:01 -0800101 @Override
102 public String toString() {
103 return MoreObjects.toStringHelper(this)
104 .add("value", value)
105 .add("version", version)
Madan Jampani30a57f82015-03-02 12:19:41 -0800106 .add("creationTime", new DateTime(creationTime))
Madan Jampani94c23532015-02-05 17:40:01 -0800107 .toString();
108 }
109}