Add uint24.yang
[mdsal.git] / common / mdsal-uint24-netty / src / main / java / org / opendaylight / mdsal / uint24 / netty / Uint24ByteBufUtils.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.uint24.netty;
9
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;
16
17 @Beta
18 public final class Uint24ByteBufUtils {
19     private Uint24ByteBufUtils() {
20         // Hidden on purpose
21     }
22
23     /**
24      * Read a {@link Uint24} from specified buffer.
25      *
26      * @param buf 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
30      */
31     public static @NonNull Uint24 readUint24(final ByteBuf buf) {
32         return new Uint24(Uint32.fromIntBits(buf.readMedium()));
33     }
34
35     /**
36      * Write a {@link Uint24} to specified buffer.
37      *
38      * @param buf buffer
39      * @param value A {@link Uint24}
40      * @throws NullPointerException if any argument is null
41      */
42     public static void writeUint24(final ByteBuf buf, final Uint24 value) {
43         buf.writeMedium(value.getValue().intValue());
44     }
45
46     /**
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.
49      *
50      * @param buf buffer
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
55      */
56     public static void writeMandatoryUint24(final ByteBuf buf, final Uint24 value, final String name) {
57         if (value == null) {
58             throw new IllegalArgumentException(name + " is mandatory");
59         }
60         writeUint24(buf, value);
61     }
62
63     /**
64      * Write a {@link Uint24} value to specified buffer if it is not null.
65      *
66      * @param buf buffer
67      * @param value A {@link Uint24}
68      * @throws NullPointerException if {@code buf} is null
69      */
70     public static void writeOptionalUint24(final ByteBuf buf, final @Nullable Uint24 value) {
71         if (value != null) {
72             writeUint24(buf, value);
73         }
74     }
75
76     /**
77      * Write a {@link Uint24} value to specified buffer if it is not null, otherwise write three zero bytes.
78      *
79      * @param buf buffer
80      * @param value A {@link Uint24}
81      * @throws NullPointerException if {@code buf} is null
82      */
83     public static void writeUint24OrZero(final ByteBuf buf, final @Nullable Uint24 value) {
84         buf.writeMedium(value != null ? value.getValue().intValue() : 0);
85     }
86 }