blob: 0b8bbd2fc06dd9faf9def824ca065b0aca2d95bd [file] [log] [blame]
alshabibab984662014-12-04 18:56:18 -08001/*
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.service;
Madan Jampani1416d2e2014-11-04 18:11:51 -080017
18import java.util.Arrays;
19
Yuta HIGUCHI75fb1f42014-11-19 13:56:19 -080020import org.onlab.util.ByteArraySizeHashPrinter;
21
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -080022import com.google.common.base.MoreObjects;
23
Madan Jampani1416d2e2014-11-04 18:11:51 -080024/**
25 * Wrapper object that holds the object (as byte array) and its version.
26 */
27public 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 Jampani9b19a822014-11-04 21:37:13 -080034 * @param value value
35 * @param version version
Madan Jampani1416d2e2014-11-04 18:11:51 -080036 */
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 HIGUCHI3bd8cdc2014-11-05 19:11:44 -080058 /**
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 Jampani1416d2e2014-11-04 18:11:51 -080080 @Override
81 public String toString() {
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -080082 return MoreObjects.toStringHelper(getClass())
83 .add("version", version)
Yuta HIGUCHI75fb1f42014-11-19 13:56:19 -080084 .add("value", ByteArraySizeHashPrinter.orNull(value))
Yuta HIGUCHI5e1cfe02014-11-04 21:22:45 -080085 .toString();
Madan Jampani1416d2e2014-11-04 18:11:51 -080086 }
87}