blob: ecd56b3a7c10d23be6ca68d1c6d7ca943e228442 [file] [log] [blame]
Madan Jampani25461112015-02-17 14:17:29 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Madan Jampani25461112015-02-17 14:17:29 -08003 *
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;
Madan Jampani39fff102016-02-14 13:17:28 -080022import org.onlab.util.ByteArraySizeHashPrinter;
Madan Jampani30a57f82015-03-02 12:19:41 -080023
Madan Jampani94c23532015-02-05 17:40:01 -080024import com.google.common.base.MoreObjects;
Madan Jampani80984052015-07-23 13:11:36 -070025import com.google.common.base.Objects;
Madan Jampani94c23532015-02-05 17:40:01 -080026
27/**
28 * Versioned value.
29 *
30 * @param <V> value type.
31 */
32public class Versioned<V> {
33
34 private final V value;
35 private final long version;
Madan Jampani30a57f82015-03-02 12:19:41 -080036 private final long creationTime;
37
38 /**
39 * Constructs a new versioned value.
40 * @param value value
41 * @param version version
42 * @param creationTime milliseconds of the creation event
43 * from the Java epoch of 1970-01-01T00:00:00Z
44 */
45 public Versioned(V value, long version, long creationTime) {
46 this.value = value;
47 this.version = version;
Madan Jampania8620462015-06-04 20:16:12 -070048 this.creationTime = creationTime;
Madan Jampani30a57f82015-03-02 12:19:41 -080049 }
Madan Jampani94c23532015-02-05 17:40:01 -080050
51 /**
52 * Constructs a new versioned value.
53 * @param value value
54 * @param version version
55 */
56 public Versioned(V value, long version) {
Madan Jampani30a57f82015-03-02 12:19:41 -080057 this(value, version, System.currentTimeMillis());
Madan Jampani94c23532015-02-05 17:40:01 -080058 }
59
60 /**
61 * Returns the value.
62 *
63 * @return value.
64 */
65 public V value() {
66 return value;
67 }
68
69 /**
70 * Returns the version.
71 *
72 * @return version
73 */
74 public long version() {
75 return version;
76 }
77
Madan Jampani30a57f82015-03-02 12:19:41 -080078 /**
79 * Returns the system time when this version was created.
80 * <p>
81 * Care should be taken when relying on creationTime to
82 * implement any behavior in a distributed setting. Due
83 * to the possibility of clock skew it is likely that
84 * even creationTimes of causally related versions can be
85 * out or order.
86 * @return creation time
87 */
88 public long creationTime() {
89 return creationTime;
90 }
91
Madan Jampanic6069562015-06-12 13:22:45 -070092 /**
93 * Maps this instance into another after transforming its
94 * value while retaining the same version and creationTime.
Madan Jampani80984052015-07-23 13:11:36 -070095 * @param transformer function for mapping the value
Madan Jampani0dbac7a2015-06-25 10:37:45 -070096 * @param <U> value type of the returned instance
Madan Jampanic6069562015-06-12 13:22:45 -070097 * @return mapped instance
98 */
99 public <U> Versioned<U> map(Function<V, U> transformer) {
100 return new Versioned<>(transformer.apply(value), version, creationTime);
101 }
102
Madan Jampani7ffe53d2015-08-17 15:25:35 -0700103 /**
104 * Returns the value of the specified Versioned object if non-null or else returns
105 * a default value.
106 * @param versioned versioned object
107 * @param defaultValue default value to return if versioned object is null
Madan Jampanif97edc12015-08-31 14:41:01 -0700108 * @param <U> type of the versioned value
Madan Jampani7ffe53d2015-08-17 15:25:35 -0700109 * @return versioned value or default value if versioned object is null
110 */
111 public static <U> U valueOrElse(Versioned<U> versioned, U defaultValue) {
112 return versioned == null ? defaultValue : versioned.value();
113 }
114
Madan Jampanidfde6ba2016-01-13 21:36:09 -0800115 /**
116 * Returns the value of the specified Versioned object if non-null or else returns null.
117 * @param versioned versioned object
118 * @param <U> type of the versioned value
119 * @return versioned value or null if versioned object is null
120 */
121 public static <U> U valueOrNull(Versioned<U> versioned) {
122 return valueOrElse(versioned, null);
123 }
124
Madan Jampani94c23532015-02-05 17:40:01 -0800125 @Override
Madan Jampani80984052015-07-23 13:11:36 -0700126 public int hashCode() {
127 return Objects.hashCode(value, version, creationTime);
128 }
129
130 @Override
131 public boolean equals(Object other) {
132 if (!(other instanceof Versioned)) {
133 return false;
134 }
135 Versioned<V> that = (Versioned) other;
136 return Objects.equal(this.value, that.value) &&
137 Objects.equal(this.version, that.version) &&
138 Objects.equal(this.creationTime, that.creationTime);
139 }
140
141 @Override
Madan Jampani94c23532015-02-05 17:40:01 -0800142 public String toString() {
143 return MoreObjects.toStringHelper(this)
Madan Jampani39fff102016-02-14 13:17:28 -0800144 .add("value", value instanceof byte[] ? new ByteArraySizeHashPrinter((byte[]) value) : value)
Madan Jampani94c23532015-02-05 17:40:01 -0800145 .add("version", version)
Madan Jampani30a57f82015-03-02 12:19:41 -0800146 .add("creationTime", new DateTime(creationTime))
Madan Jampani94c23532015-02-05 17:40:01 -0800147 .toString();
148 }
149}