Slice out byte[] printer
Change-Id: I095575df5f03057c386be77328897be59c96eaf9
Showing
4 changed files
with
130 additions
and
4 deletions
| ... | @@ -2,6 +2,8 @@ package org.onlab.onos.store.service; | ... | @@ -2,6 +2,8 @@ package org.onlab.onos.store.service; |
| 2 | 2 | ||
| 3 | import java.util.Arrays; | 3 | import java.util.Arrays; |
| 4 | 4 | ||
| 5 | +import org.onlab.util.ByteArraySizeHashPrinter; | ||
| 6 | + | ||
| 5 | import com.google.common.base.MoreObjects; | 7 | import com.google.common.base.MoreObjects; |
| 6 | 8 | ||
| 7 | /** | 9 | /** |
| ... | @@ -64,7 +66,7 @@ public class VersionedValue { | ... | @@ -64,7 +66,7 @@ public class VersionedValue { |
| 64 | public String toString() { | 66 | public String toString() { |
| 65 | return MoreObjects.toStringHelper(getClass()) | 67 | return MoreObjects.toStringHelper(getClass()) |
| 66 | .add("version", version) | 68 | .add("version", version) |
| 67 | - .add("value", value != null ? "[" + value.length + " bytes]" : value) | 69 | + .add("value", ByteArraySizeHashPrinter.orNull(value)) |
| 68 | .toString(); | 70 | .toString(); |
| 69 | } | 71 | } |
| 70 | } | 72 | } | ... | ... |
| ... | @@ -6,6 +6,8 @@ import static org.onlab.onos.store.service.WriteRequest.Type.*; | ... | @@ -6,6 +6,8 @@ import static org.onlab.onos.store.service.WriteRequest.Type.*; |
| 6 | 6 | ||
| 7 | import java.util.Objects; | 7 | import java.util.Objects; |
| 8 | 8 | ||
| 9 | +import org.onlab.util.ByteArraySizeHashPrinter; | ||
| 10 | + | ||
| 9 | import com.google.common.base.MoreObjects; | 11 | import com.google.common.base.MoreObjects; |
| 10 | 12 | ||
| 11 | /** | 13 | /** |
| ... | @@ -185,13 +187,13 @@ public class WriteRequest { | ... | @@ -185,13 +187,13 @@ public class WriteRequest { |
| 185 | 187 | ||
| 186 | @Override | 188 | @Override |
| 187 | public String toString() { | 189 | public String toString() { |
| 188 | - return MoreObjects.toStringHelper(getClass()) | 190 | + return MoreObjects.toStringHelper(getClass()) |
| 189 | .add("type", type) | 191 | .add("type", type) |
| 190 | .add("tableName", tableName) | 192 | .add("tableName", tableName) |
| 191 | .add("key", key) | 193 | .add("key", key) |
| 192 | - .add("newValue", newValue != null ? "[" + newValue.length + " bytes]" : newValue) | 194 | + .add("newValue", ByteArraySizeHashPrinter.orNull(newValue)) |
| 193 | .add("previousVersion", previousVersion) | 195 | .add("previousVersion", previousVersion) |
| 194 | - .add("oldValue", oldValue != null ? "[" + oldValue.length + " bytes]" : oldValue) | 196 | + .add("oldValue", ByteArraySizeHashPrinter.orNull(oldValue)) |
| 195 | .toString(); | 197 | .toString(); |
| 196 | } | 198 | } |
| 197 | 199 | ... | ... |
| 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 | + */ | ||
| 16 | + | ||
| 17 | +package org.onlab.util; | ||
| 18 | + | ||
| 19 | +import java.util.Arrays; | ||
| 20 | + | ||
| 21 | +import com.google.common.base.MoreObjects; | ||
| 22 | +import com.google.common.base.MoreObjects.ToStringHelper; | ||
| 23 | + | ||
| 24 | +/** | ||
| 25 | + * Helper to print byte[] length and hashCode. | ||
| 26 | + */ | ||
| 27 | +public final class ByteArraySizeHashPrinter { | ||
| 28 | + | ||
| 29 | + private final byte[] bytes; | ||
| 30 | + | ||
| 31 | + /** | ||
| 32 | + * Returns ByteArraySizeHashPrinter wrapping given byte[]. | ||
| 33 | + * | ||
| 34 | + * @param bytes bytes to wrap around | ||
| 35 | + * @return ByteArraySizeHashPrinter | ||
| 36 | + */ | ||
| 37 | + public static ByteArraySizeHashPrinter of(byte[] bytes) { | ||
| 38 | + return new ByteArraySizeHashPrinter(bytes); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + /** | ||
| 42 | + * Returns ByteArraySizeHashPrinter wrapping given byte[]. | ||
| 43 | + * | ||
| 44 | + * @param bytes bytes to wrap around | ||
| 45 | + * @return null if {@code bytes == null}, ByteArraySizeHashPrinter otherwise | ||
| 46 | + */ | ||
| 47 | + public static ByteArraySizeHashPrinter orNull(byte[] bytes) { | ||
| 48 | + if (bytes == null) { | ||
| 49 | + return null; | ||
| 50 | + } | ||
| 51 | + return new ByteArraySizeHashPrinter(bytes); | ||
| 52 | + } | ||
| 53 | + | ||
| 54 | + public ByteArraySizeHashPrinter(byte[] bytes) { | ||
| 55 | + this.bytes = bytes; | ||
| 56 | + } | ||
| 57 | + | ||
| 58 | + @Override | ||
| 59 | + public String toString() { | ||
| 60 | + ToStringHelper helper = MoreObjects.toStringHelper("byte[]"); | ||
| 61 | + if (bytes != null) { | ||
| 62 | + helper.add("length", bytes.length) | ||
| 63 | + .add("hash", Arrays.hashCode(bytes)); | ||
| 64 | + } else { | ||
| 65 | + helper.addValue(bytes); | ||
| 66 | + } | ||
| 67 | + return helper.toString(); | ||
| 68 | + } | ||
| 69 | +} |
| 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 | + */ | ||
| 16 | + | ||
| 17 | +package org.onlab.util; | ||
| 18 | + | ||
| 19 | +import static org.junit.Assert.*; | ||
| 20 | + | ||
| 21 | +import java.util.Arrays; | ||
| 22 | + | ||
| 23 | +import org.junit.Test; | ||
| 24 | + | ||
| 25 | +/** | ||
| 26 | + * Test cases for byte[] pretty printer. | ||
| 27 | + */ | ||
| 28 | +public class ByteArraySizeHashPrinterTest { | ||
| 29 | + | ||
| 30 | + /** | ||
| 31 | + * Test method for {@link org.onlab.util.ByteArraySizeHashPrinter#toString()}. | ||
| 32 | + */ | ||
| 33 | + @Test | ||
| 34 | + public void testToStringNull() { | ||
| 35 | + final byte[] none = null; | ||
| 36 | + | ||
| 37 | + assertEquals("byte[]{null}", String.valueOf(ByteArraySizeHashPrinter.of(none))); | ||
| 38 | + assertNull(ByteArraySizeHashPrinter.orNull(none)); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + /** | ||
| 42 | + * Test method for {@link org.onlab.util.ByteArraySizeHashPrinter#toString()}. | ||
| 43 | + */ | ||
| 44 | + @Test | ||
| 45 | + public void testToString() { | ||
| 46 | + final byte[] some = new byte[] {2, 5, 0, 1 }; | ||
| 47 | + final String expected = "byte[]{length=" + some.length + ", hash=" + Arrays.hashCode(some) + "}"; | ||
| 48 | + | ||
| 49 | + assertEquals(expected, String.valueOf(ByteArraySizeHashPrinter.of(some))); | ||
| 50 | + assertNotNull(ByteArraySizeHashPrinter.orNull(some)); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | +} |
-
Please register or login to post a comment