Madan Jampani | 2546111 | 2015-02-17 14:17:29 -0800 | [diff] [blame] | 1 | /* |
| 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 Jampani | 393e0f0 | 2015-02-12 07:35:39 +0530 | [diff] [blame] | 17 | package org.onosproject.store.service; |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 18 | |
Madan Jampani | c606956 | 2015-06-12 13:22:45 -0700 | [diff] [blame] | 19 | import java.util.function.Function; |
| 20 | |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 21 | import org.joda.time.DateTime; |
| 22 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 23 | import com.google.common.base.MoreObjects; |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 24 | import com.google.common.base.Objects; |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Versioned value. |
| 28 | * |
| 29 | * @param <V> value type. |
| 30 | */ |
| 31 | public class Versioned<V> { |
| 32 | |
| 33 | private final V value; |
| 34 | private final long version; |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 35 | 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 Jampani | a862046 | 2015-06-04 20:16:12 -0700 | [diff] [blame] | 47 | this.creationTime = creationTime; |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 48 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * Constructs a new versioned value. |
| 52 | * @param value value |
| 53 | * @param version version |
| 54 | */ |
| 55 | public Versioned(V value, long version) { |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 56 | this(value, version, System.currentTimeMillis()); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 57 | } |
| 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 Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 77 | /** |
| 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 Jampani | c606956 | 2015-06-12 13:22:45 -0700 | [diff] [blame] | 91 | /** |
| 92 | * Maps this instance into another after transforming its |
| 93 | * value while retaining the same version and creationTime. |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 94 | * @param transformer function for mapping the value |
Madan Jampani | 0dbac7a | 2015-06-25 10:37:45 -0700 | [diff] [blame] | 95 | * @param <U> value type of the returned instance |
Madan Jampani | c606956 | 2015-06-12 13:22:45 -0700 | [diff] [blame] | 96 | * @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 Jampani | 7ffe53d | 2015-08-17 15:25:35 -0700 | [diff] [blame] | 102 | /** |
| 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 |
| 107 | * @return versioned value or default value if versioned object is null |
| 108 | */ |
| 109 | public static <U> U valueOrElse(Versioned<U> versioned, U defaultValue) { |
| 110 | return versioned == null ? defaultValue : versioned.value(); |
| 111 | } |
| 112 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 113 | @Override |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 114 | public int hashCode() { |
| 115 | return Objects.hashCode(value, version, creationTime); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public boolean equals(Object other) { |
| 120 | if (!(other instanceof Versioned)) { |
| 121 | return false; |
| 122 | } |
| 123 | Versioned<V> that = (Versioned) other; |
| 124 | return Objects.equal(this.value, that.value) && |
| 125 | Objects.equal(this.version, that.version) && |
| 126 | Objects.equal(this.creationTime, that.creationTime); |
| 127 | } |
| 128 | |
| 129 | @Override |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 130 | public String toString() { |
| 131 | return MoreObjects.toStringHelper(this) |
| 132 | .add("value", value) |
| 133 | .add("version", version) |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 134 | .add("creationTime", new DateTime(creationTime)) |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 135 | .toString(); |
| 136 | } |
| 137 | } |