alshabib | ab98466 | 2014-12-04 18:56:18 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.service; |
Madan Jampani | 1416d2e | 2014-11-04 18:11:51 -0800 | [diff] [blame] | 17 | |
| 18 | import java.util.Arrays; |
| 19 | |
Yuta HIGUCHI | 75fb1f4 | 2014-11-19 13:56:19 -0800 | [diff] [blame] | 20 | import org.onlab.util.ByteArraySizeHashPrinter; |
| 21 | |
Yuta HIGUCHI | 5e1cfe0 | 2014-11-04 21:22:45 -0800 | [diff] [blame] | 22 | import com.google.common.base.MoreObjects; |
| 23 | |
Madan Jampani | 1416d2e | 2014-11-04 18:11:51 -0800 | [diff] [blame] | 24 | /** |
| 25 | * Wrapper object that holds the object (as byte array) and its version. |
| 26 | */ |
| 27 | public class VersionedValue { |
| 28 | |
| 29 | private final byte[] value; |
| 30 | private final long version; |
| 31 | |
| 32 | /** |
| 33 | * Creates a new instance with the specified value and version. |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 34 | * @param value value |
| 35 | * @param version version |
Madan Jampani | 1416d2e | 2014-11-04 18:11:51 -0800 | [diff] [blame] | 36 | */ |
| 37 | public VersionedValue(byte[] value, long version) { |
| 38 | this.value = value; |
| 39 | this.version = version; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Returns the value. |
| 44 | * @return value. |
| 45 | */ |
| 46 | public byte[] value() { |
| 47 | return value; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Returns the version. |
| 52 | * @return version. |
| 53 | */ |
| 54 | public long version() { |
| 55 | return version; |
| 56 | } |
| 57 | |
Yuta HIGUCHI | 3bd8cdc | 2014-11-05 19:11:44 -0800 | [diff] [blame] | 58 | /** |
| 59 | * Creates a copy of given VersionedValue. |
| 60 | * |
| 61 | * @param original VersionedValue to create a copy |
| 62 | * @return same as original if original or it's value is null, |
| 63 | * otherwise creates a copy. |
| 64 | */ |
| 65 | public static VersionedValue copy(VersionedValue original) { |
| 66 | if (original == null) { |
| 67 | return null; |
| 68 | } |
| 69 | if (original.value == null) { |
| 70 | // immutable, no need to copy |
| 71 | return original; |
| 72 | } else { |
| 73 | return new VersionedValue( |
| 74 | Arrays.copyOf(original.value, |
| 75 | original.value.length), |
| 76 | original.version); |
| 77 | } |
| 78 | } |
| 79 | |
Madan Jampani | 1416d2e | 2014-11-04 18:11:51 -0800 | [diff] [blame] | 80 | @Override |
| 81 | public String toString() { |
Yuta HIGUCHI | 5e1cfe0 | 2014-11-04 21:22:45 -0800 | [diff] [blame] | 82 | return MoreObjects.toStringHelper(getClass()) |
| 83 | .add("version", version) |
Yuta HIGUCHI | 75fb1f4 | 2014-11-19 13:56:19 -0800 | [diff] [blame] | 84 | .add("value", ByteArraySizeHashPrinter.orNull(value)) |
Yuta HIGUCHI | 5e1cfe0 | 2014-11-04 21:22:45 -0800 | [diff] [blame] | 85 | .toString(); |
Madan Jampani | 1416d2e | 2014-11-04 18:11:51 -0800 | [diff] [blame] | 86 | } |
| 87 | } |