2 * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.uint24.netty;
10 import com.google.common.annotations.Beta;
11 import io.netty.buffer.ByteBuf;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.eclipse.jdt.annotation.Nullable;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.uint24.rev200104.Uint24;
15 import org.opendaylight.yangtools.yang.common.Uint32;
18 public final class Uint24ByteBufUtils {
19 private Uint24ByteBufUtils() {
24 * Read a {@link Uint24} from specified buffer.
27 * @return A {@link Uint24}
28 * @throws NullPointerException if {@code buf} is null
29 * @throws IndexOutOfBoundsException if {@code buf} does not have enough data
31 public static @NonNull Uint24 readUint24(final ByteBuf buf) {
32 return new Uint24(Uint32.fromIntBits(buf.readMedium()));
36 * Write a {@link Uint24} to specified buffer.
39 * @param value A {@link Uint24}
40 * @throws NullPointerException if any argument is null
42 public static void writeUint24(final ByteBuf buf, final Uint24 value) {
43 buf.writeMedium(value.getValue().intValue());
47 * Write a {@link Uint24} property to specified buffer. If the {@code value} is known to be non-null, prefer to use
48 * {@link #writeUint24(ByteBuf, Uint24)} instead of this method.
51 * @param value A {@link Uint24}
52 * @param name Property name for error reporting purposes
53 * @throws NullPointerException if {@code buf} is null
54 * @throws IllegalArgumentException if {@code value} is null
56 public static void writeMandatoryUint24(final ByteBuf buf, final Uint24 value, final String name) {
58 throw new IllegalArgumentException(name + " is mandatory");
60 writeUint24(buf, value);
64 * Write a {@link Uint24} value to specified buffer if it is not null.
67 * @param value A {@link Uint24}
68 * @throws NullPointerException if {@code buf} is null
70 public static void writeOptionalUint24(final ByteBuf buf, final @Nullable Uint24 value) {
72 writeUint24(buf, value);
77 * Write a {@link Uint24} value to specified buffer if it is not null, otherwise write three zero bytes.
80 * @param value A {@link Uint24}
81 * @throws NullPointerException if {@code buf} is null
83 public static void writeUint24OrZero(final ByteBuf buf, final @Nullable Uint24 value) {
84 buf.writeMedium(value != null ? value.getValue().intValue() : 0);