Madan Jampani | 2546111 | 2015-02-17 14:17:29 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Madan Jampani | 2546111 | 2015-02-17 14:17:29 -0800 | [diff] [blame] | 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; |
Madan Jampani | 39fff10 | 2016-02-14 13:17:28 -0800 | [diff] [blame] | 22 | import org.onlab.util.ByteArraySizeHashPrinter; |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 23 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 24 | import com.google.common.base.MoreObjects; |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 25 | import com.google.common.base.Objects; |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 26 | |
| 27 | /** |
| 28 | * Versioned value. |
| 29 | * |
| 30 | * @param <V> value type. |
| 31 | */ |
| 32 | public class Versioned<V> { |
| 33 | |
| 34 | private final V value; |
| 35 | private final long version; |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 36 | 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 Jampani | a862046 | 2015-06-04 20:16:12 -0700 | [diff] [blame] | 48 | this.creationTime = creationTime; |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 49 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
| 52 | * Constructs a new versioned value. |
| 53 | * @param value value |
| 54 | * @param version version |
| 55 | */ |
| 56 | public Versioned(V value, long version) { |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 57 | this(value, version, System.currentTimeMillis()); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 58 | } |
| 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 Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 78 | /** |
| 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 Jampani | c606956 | 2015-06-12 13:22:45 -0700 | [diff] [blame] | 92 | /** |
| 93 | * Maps this instance into another after transforming its |
| 94 | * value while retaining the same version and creationTime. |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 95 | * @param transformer function for mapping the value |
Madan Jampani | 0dbac7a | 2015-06-25 10:37:45 -0700 | [diff] [blame] | 96 | * @param <U> value type of the returned instance |
Madan Jampani | c606956 | 2015-06-12 13:22:45 -0700 | [diff] [blame] | 97 | * @return mapped instance |
| 98 | */ |
Madan Jampani | 6c02d9e | 2016-05-24 15:09:47 -0700 | [diff] [blame] | 99 | public synchronized <U> Versioned<U> map(Function<V, U> transformer) { |
Madan Jampani | c606956 | 2015-06-12 13:22:45 -0700 | [diff] [blame] | 100 | return new Versioned<>(transformer.apply(value), version, creationTime); |
| 101 | } |
| 102 | |
Madan Jampani | 7ffe53d | 2015-08-17 15:25:35 -0700 | [diff] [blame] | 103 | /** |
| 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 Jampani | f97edc1 | 2015-08-31 14:41:01 -0700 | [diff] [blame] | 108 | * @param <U> type of the versioned value |
Madan Jampani | 7ffe53d | 2015-08-17 15:25:35 -0700 | [diff] [blame] | 109 | * @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 Jampani | dfde6ba | 2016-01-13 21:36:09 -0800 | [diff] [blame] | 115 | /** |
| 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 Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 125 | @Override |
Madan Jampani | 8098405 | 2015-07-23 13:11:36 -0700 | [diff] [blame] | 126 | 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 Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 142 | public String toString() { |
| 143 | return MoreObjects.toStringHelper(this) |
Madan Jampani | 39fff10 | 2016-02-14 13:17:28 -0800 | [diff] [blame] | 144 | .add("value", value instanceof byte[] ? new ByteArraySizeHashPrinter((byte[]) value) : value) |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 145 | .add("version", version) |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 146 | .add("creationTime", new DateTime(creationTime)) |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 147 | .toString(); |
| 148 | } |
| 149 | } |