blob: ac7109c29b7220f0589487bbaeca174b3357bdb8 [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 Jampani94c23532015-02-05 17:40:01 -0800102 @Override
Madan Jampani80984052015-07-23 13:11:36 -0700103 public int hashCode() {
104 return Objects.hashCode(value, version, creationTime);
105 }
106
107 @Override
108 public boolean equals(Object other) {
109 if (!(other instanceof Versioned)) {
110 return false;
111 }
112 Versioned<V> that = (Versioned) other;
113 return Objects.equal(this.value, that.value) &&
114 Objects.equal(this.version, that.version) &&
115 Objects.equal(this.creationTime, that.creationTime);
116 }
117
118 @Override
Madan Jampani94c23532015-02-05 17:40:01 -0800119 public String toString() {
120 return MoreObjects.toStringHelper(this)
121 .add("value", value)
122 .add("version", version)
Madan Jampani30a57f82015-03-02 12:19:41 -0800123 .add("creationTime", new DateTime(creationTime))
Madan Jampani94c23532015-02-05 17:40:01 -0800124 .toString();
125 }
126}