blob: 0eec3ffa47e5e227e4a24bb7bb0e9bbaaf9ce0bd [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;
Madan Jampani80984052015-07-23 13:11:36 -070024import com.google.common.base.Objects;
Madan Jampani94c23532015-02-05 17:40:01 -080025
26/**
27 * Versioned value.
28 *
29 * @param <V> value type.
30 */
31public class Versioned<V> {
32
33 private final V value;
34 private final long version;
Madan Jampani30a57f82015-03-02 12:19:41 -080035 private final long creationTime;
36
37 /**
38 * Constructs a new versioned value.
39 * @param value value
40 * @param version version
41 * @param creationTime milliseconds of the creation event
42 * from the Java epoch of 1970-01-01T00:00:00Z
43 */
44 public Versioned(V value, long version, long creationTime) {
45 this.value = value;
46 this.version = version;
Madan Jampania8620462015-06-04 20:16:12 -070047 this.creationTime = creationTime;
Madan Jampani30a57f82015-03-02 12:19:41 -080048 }
Madan Jampani94c23532015-02-05 17:40:01 -080049
50 /**
51 * Constructs a new versioned value.
52 * @param value value
53 * @param version version
54 */
55 public Versioned(V value, long version) {
Madan Jampani30a57f82015-03-02 12:19:41 -080056 this(value, version, System.currentTimeMillis());
Madan Jampani94c23532015-02-05 17:40:01 -080057 }
58
59 /**
60 * Returns the value.
61 *
62 * @return value.
63 */
64 public V value() {
65 return value;
66 }
67
68 /**
69 * Returns the version.
70 *
71 * @return version
72 */
73 public long version() {
74 return version;
75 }
76
Madan Jampani30a57f82015-03-02 12:19:41 -080077 /**
78 * Returns the system time when this version was created.
79 * <p>
80 * Care should be taken when relying on creationTime to
81 * implement any behavior in a distributed setting. Due
82 * to the possibility of clock skew it is likely that
83 * even creationTimes of causally related versions can be
84 * out or order.
85 * @return creation time
86 */
87 public long creationTime() {
88 return creationTime;
89 }
90
Madan Jampanic6069562015-06-12 13:22:45 -070091 /**
92 * Maps this instance into another after transforming its
93 * value while retaining the same version and creationTime.
Madan Jampani80984052015-07-23 13:11:36 -070094 * @param transformer function for mapping the value
Madan Jampani0dbac7a2015-06-25 10:37:45 -070095 * @param <U> value type of the returned instance
Madan Jampanic6069562015-06-12 13:22:45 -070096 * @return mapped instance
97 */
98 public <U> Versioned<U> map(Function<V, U> transformer) {
99 return new Versioned<>(transformer.apply(value), version, creationTime);
100 }
101
Madan Jampani7ffe53d2015-08-17 15:25:35 -0700102 /**
103 * Returns the value of the specified Versioned object if non-null or else returns
104 * a default value.
105 * @param versioned versioned object
106 * @param defaultValue default value to return if versioned object is null
Madan Jampanif97edc12015-08-31 14:41:01 -0700107 * @param <U> type of the versioned value
Madan Jampani7ffe53d2015-08-17 15:25:35 -0700108 * @return versioned value or default value if versioned object is null
109 */
110 public static <U> U valueOrElse(Versioned<U> versioned, U defaultValue) {
111 return versioned == null ? defaultValue : versioned.value();
112 }
113
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800114 /**
115 * Returns the value of the specified Versioned object if non-null or else returns null.
116 * @param versioned versioned object
117 * @param <U> type of the versioned value
118 * @return versioned value or null if versioned object is null
119 */
120 public static <U> U valueOrNull(Versioned<U> versioned) {
121 return valueOrElse(versioned, null);
122 }
123
Madan Jampani94c23532015-02-05 17:40:01 -0800124 @Override
Madan Jampani80984052015-07-23 13:11:36 -0700125 public int hashCode() {
126 return Objects.hashCode(value, version, creationTime);
127 }
128
129 @Override
130 public boolean equals(Object other) {
131 if (!(other instanceof Versioned)) {
132 return false;
133 }
134 Versioned<V> that = (Versioned) other;
135 return Objects.equal(this.value, that.value) &&
136 Objects.equal(this.version, that.version) &&
137 Objects.equal(this.creationTime, that.creationTime);
138 }
139
140 @Override
Madan Jampani94c23532015-02-05 17:40:01 -0800141 public String toString() {
142 return MoreObjects.toStringHelper(this)
143 .add("value", value)
144 .add("version", version)
Madan Jampani30a57f82015-03-02 12:19:41 -0800145 .add("creationTime", new DateTime(creationTime))
Madan Jampani94c23532015-02-05 17:40:01 -0800146 .toString();
147 }
148}