From a7147d16c31d9028c6b5dc557264433de0f11c91 Mon Sep 17 00:00:00 2001 From: Jan Srnicek Date: Fri, 23 Sep 2016 16:39:09 +0200 Subject: HONEYCOMB-145 - Utility Class Refactoring problematic mockito-all changed to mockito-core( https://github.com/mockito/mockito/issues/324) Translate Utils Splitted to multiple Trait Interfaces Ipv4Translator - Logic for translation of ipv4-based data Ipv6Translator - Logic for translation of ipv6-based data MacTranslator - Logic for translation of mac-based data AddressTranslator - Aggregation trait for Ipv4/Ipv6/Mac JvppReplyConsumer - Logic for extracting replies from jvpp calls ByteDataTranslator - any byte-based conversions Plus some existing utility classes changed to traits Change-Id: I342b625954223966802e65dca0fabf8456c89345 Signed-off-by: Jan Srnicek --- .../translate/v3po/util/ByteDataTranslator.java | 79 ++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/ByteDataTranslator.java (limited to 'vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/ByteDataTranslator.java') diff --git a/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/ByteDataTranslator.java b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/ByteDataTranslator.java new file mode 100644 index 000000000..fedc3e5d2 --- /dev/null +++ b/vpp-common/vpp-translate-utils/src/main/java/io/fd/honeycomb/translate/v3po/util/ByteDataTranslator.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2016 Cisco and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.fd.honeycomb.translate.v3po.util; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +/** + * Trait providing logic for working with binary-based data + */ +public interface ByteDataTranslator { + + /** + * Returns 0 if argument is null or false, 1 otherwise. + * + * @param value Boolean value to be converted + * @return byte value equal to 0 or 1 + */ + default byte booleanToByte(@Nullable final Boolean value) { + return value != null && value + ? (byte) 1 + : (byte) 0; + } + + /** + * Returns Boolean.TRUE if argument is 0, Boolean.FALSE otherwise. + * + * @param value byte value to be converted + * @return Boolean value + * @throws IllegalArgumentException if argument is neither 0 nor 1 + */ + @Nonnull + default Boolean byteToBoolean(final byte value) { + if (value == 0) { + return Boolean.FALSE; + } else if (value == 1) { + return Boolean.TRUE; + } + throw new IllegalArgumentException(String.format("0 or 1 was expected but was %d", value)); + } + + /** + * Reverses bytes in the byte array + * + * @param bytes input array + * @return reversed array + */ + default byte[] reverseBytes(final byte[] bytes) { + final byte[] reversed = new byte[bytes.length]; + int i = 1; + for (byte aByte : bytes) { + reversed[bytes.length - i++] = aByte; + } + + return reversed; + } + + /** + * Return (interned) string from byte array while removing \u0000. Strings represented as fixed length byte[] from + * vpp contain \u0000. + */ + default String toString(final byte[] cString) { + return new String(cString).replaceAll("\\u0000", "").intern(); + } +} -- cgit 1.2.3-korg