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 | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 19 | import org.joda.time.DateTime; |
| 20 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 21 | import com.google.common.base.MoreObjects; |
| 22 | |
| 23 | /** |
| 24 | * Versioned value. |
| 25 | * |
| 26 | * @param <V> value type. |
| 27 | */ |
| 28 | public class Versioned<V> { |
| 29 | |
| 30 | private final V value; |
| 31 | private final long version; |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 32 | private final long creationTime; |
| 33 | |
| 34 | /** |
| 35 | * Constructs a new versioned value. |
| 36 | * @param value value |
| 37 | * @param version version |
| 38 | * @param creationTime milliseconds of the creation event |
| 39 | * from the Java epoch of 1970-01-01T00:00:00Z |
| 40 | */ |
| 41 | public Versioned(V value, long version, long creationTime) { |
| 42 | this.value = value; |
| 43 | this.version = version; |
| 44 | this.creationTime = System.currentTimeMillis(); |
| 45 | } |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Constructs a new versioned value. |
| 49 | * @param value value |
| 50 | * @param version version |
| 51 | */ |
| 52 | public Versioned(V value, long version) { |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 53 | this(value, version, System.currentTimeMillis()); |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Returns the value. |
| 58 | * |
| 59 | * @return value. |
| 60 | */ |
| 61 | public V value() { |
| 62 | return value; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns the version. |
| 67 | * |
| 68 | * @return version |
| 69 | */ |
| 70 | public long version() { |
| 71 | return version; |
| 72 | } |
| 73 | |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 74 | /** |
| 75 | * Returns the system time when this version was created. |
| 76 | * <p> |
| 77 | * Care should be taken when relying on creationTime to |
| 78 | * implement any behavior in a distributed setting. Due |
| 79 | * to the possibility of clock skew it is likely that |
| 80 | * even creationTimes of causally related versions can be |
| 81 | * out or order. |
| 82 | * @return creation time |
| 83 | */ |
| 84 | public long creationTime() { |
| 85 | return creationTime; |
| 86 | } |
| 87 | |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 88 | @Override |
| 89 | public String toString() { |
| 90 | return MoreObjects.toStringHelper(this) |
| 91 | .add("value", value) |
| 92 | .add("version", version) |
Madan Jampani | 30a57f8 | 2015-03-02 12:19:41 -0800 | [diff] [blame] | 93 | .add("creationTime", new DateTime(creationTime)) |
Madan Jampani | 94c2353 | 2015-02-05 17:40:01 -0800 | [diff] [blame] | 94 | .toString(); |
| 95 | } |
| 96 | } |