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; |
| 24 | |
| 25 | /** |
| 26 | * Versioned value. |
| 27 | * |
| 28 | * @param <V> value type. |
| 29 | */ |
| 30 | public class Versioned<V> { |
| 31 | |
| 32 | private final V value; |
| 33 | private final long version; |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 34 | private final long creationTime; |
| 35 | |
| 36 | /** |
| 37 | * Constructs a new versioned value. |
| 38 | * @param value value |
| 39 | * @param version version |
| 40 | * @param creationTime milliseconds of the creation event |
| 41 | * from the Java epoch of 1970-01-01T00:00:00Z |
| 42 | */ |
| 43 | public Versioned(V value, long version, long creationTime) { |
| 44 | this.value = value; |
| 45 | this.version = version; |
Madan Jampani | a862046 | 2015-06-04 20:16:12 -0700 | [diff] [blame] | 46 | this.creationTime = creationTime; |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 47 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 48 | |
| 49 | /** |
| 50 | * Constructs a new versioned value. |
| 51 | * @param value value |
| 52 | * @param version version |
| 53 | */ |
| 54 | public Versioned(V value, long version) { |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 55 | this(value, version, System.currentTimeMillis()); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns the value. |
| 60 | * |
| 61 | * @return value. |
| 62 | */ |
| 63 | public V value() { |
| 64 | return value; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns the version. |
| 69 | * |
| 70 | * @return version |
| 71 | */ |
| 72 | public long version() { |
| 73 | return version; |
| 74 | } |
| 75 | |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 76 | /** |
| 77 | * Returns the system time when this version was created. |
| 78 | * <p> |
| 79 | * Care should be taken when relying on creationTime to |
| 80 | * implement any behavior in a distributed setting. Due |
| 81 | * to the possibility of clock skew it is likely that |
| 82 | * even creationTimes of causally related versions can be |
| 83 | * out or order. |
| 84 | * @return creation time |
| 85 | */ |
| 86 | public long creationTime() { |
| 87 | return creationTime; |
| 88 | } |
| 89 | |
Madan Jampani | c606956 | 2015-06-12 13:22:45 -0700 | [diff] [blame] | 90 | /** |
| 91 | * Maps this instance into another after transforming its |
| 92 | * value while retaining the same version and creationTime. |
| 93 | * @param transformer function to mapping the value |
Madan Jampani | 0dbac7a | 2015-06-25 10:37:45 -0700 | [diff] [blame] | 94 | * @param <U> value type of the returned instance |
Madan Jampani | c606956 | 2015-06-12 13:22:45 -0700 | [diff] [blame] | 95 | * @return mapped instance |
| 96 | */ |
| 97 | public <U> Versioned<U> map(Function<V, U> transformer) { |
| 98 | return new Versioned<>(transformer.apply(value), version, creationTime); |
| 99 | } |
| 100 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 101 | @Override |
| 102 | public String toString() { |
| 103 | return MoreObjects.toStringHelper(this) |
| 104 | .add("value", value) |
| 105 | .add("version", version) |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 106 | .add("creationTime", new DateTime(creationTime)) |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 107 | .toString(); |
| 108 | } |
| 109 | } |