*/
package org.opendaylight.yangtools.concepts;
+import static com.google.common.base.Preconditions.checkArgument;
+
import com.google.common.annotations.Beta;
-import com.google.common.base.Preconditions;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
-import javax.annotation.Nonnull;
+import org.eclipse.jdt.annotation.NonNullByDefault;
/**
* Utility methods for working with {@link WritableObject}s.
* @author Robert Varga
*/
@Beta
+@NonNullByDefault
public final class WritableObjects {
private WritableObjects() {
- throw new UnsupportedOperationException();
+ // Hidden on purpose
}
/**
* serializing counters and similar, which have a wide range, but typically do not use it. The value provided is
* treated as unsigned.
*
- * This methods writes the number of trailing non-zero in the value. It then writes the minimum required bytes
+ * <p>This methods writes the number of trailing non-zero in the value. It then writes the minimum required bytes
* to reconstruct the value by left-padding zeroes. Inverse operation is performed by {@link #readLong(DataInput)}
* or a combination of {@link #readLongHeader(DataInput)} and {@link #readLongBody(DataInput, byte)}.
*
- * Additionally the caller can use the top four bits (i.e. 0xF0) for caller-specific flags. These will be ignored
- * by {@link #readLong(DataInput)}, but can be extracted via {@link #readLongHeader(DataInput)}.
+ * <p>Additionally the caller can use the top four bits (i.e. 0xF0) for caller-specific flags. These will be
+ * ignored by {@link #readLong(DataInput)}, but can be extracted via {@link #readLongHeader(DataInput)}.
*
* @param out Data output
* @param value long value to write
* @throws NullPointerException if output is null
*/
public static void writeLong(final DataOutput out, final long value, final int flags) throws IOException {
- Preconditions.checkArgument((flags & 0xFFFFFF0F) == 0, "Invalid flags {}", flags);
+ checkArgument((flags & 0xFFFFFF0F) == 0, "Invalid flags %s", flags);
final int bytes = valueBytes(value);
out.writeByte(bytes | flags);
writeValue(out, value, bytes);
* @throws IOException if an I/O error occurs
* @throws NullPointerException if input is null
*/
- public static long readLong(final @Nonnull DataInput in) throws IOException {
+ public static long readLong(final DataInput in) throws IOException {
return readLongBody(in, readLongHeader(in));
}
* @throws IOException if an I/O error occurs
* @throws NullPointerException if input is null
*/
- public static byte readLongHeader(final @Nonnull DataInput in) throws IOException {
+ public static byte readLongHeader(final DataInput in) throws IOException {
return in.readByte();
}
*
* @param in Data input
* @param header Value header, as returned by {@link #readLongHeader(DataInput)}
+ * @return long value
* @throws IOException if an I/O error occurs
* @throws NullPointerException if input is null
*/
- public static long readLongBody(final @Nonnull DataInput in, final byte header) throws IOException {
+ public static long readLongBody(final DataInput in, final byte header) throws IOException {
int bytes = header & 0xF;
- if (bytes < 8) {
- if (bytes > 0) {
- long value = 0;
- if (bytes >= 4) {
- bytes -= 4;
- value = (in.readInt() & 0xFFFFFFFFL) << (bytes * Byte.SIZE);
- }
- if (bytes >= 2) {
- bytes -= 2;
- value |= in.readUnsignedShort() << (bytes * Byte.SIZE);
- }
- if (bytes > 0) {
- value |= in.readUnsignedByte();
- }
- return value;
- } else {
- return 0;
- }
- } else {
+ if (bytes >= 8) {
return in.readLong();
}
+
+ if (bytes <= 0) {
+ return 0;
+ }
+
+ long value = 0;
+ if (bytes >= 4) {
+ bytes -= 4;
+ value = (in.readInt() & 0xFFFFFFFFL) << bytes * Byte.SIZE;
+ }
+ if (bytes >= 2) {
+ bytes -= 2;
+ value |= in.readUnsignedShort() << bytes * Byte.SIZE;
+ }
+ if (bytes > 0) {
+ value |= in.readUnsignedByte();
+ }
+ return value;
}
/**
* Write two consecutive long values. These values can be read back using {@link #readLongHeader(DataInput)},
* {@link #readFirstLong(DataInput, byte)} and {@link #readSecondLong(DataInput, byte)}.
*
- * This is a more efficient way of serializing two longs than {@link #writeLong(DataOutput, long)}. This is achieved
- * by using the flags field to hold the length of the second long -- hence saving one byte.
+ * <p>This is a more efficient way of serializing two longs than {@link #writeLong(DataOutput, long)}. This is
+ * achieved by using the flags field to hold the length of the second long -- hence saving one byte.
*
* @param out Data output
* @param value0 first long value to write
* @throws IOException if an I/O error occurs
* @throws NullPointerException if output is null
*/
- public static void writeLongs(final @Nonnull DataOutput out, final long value0, final long value1) throws IOException {
+ public static void writeLongs(final DataOutput out, final long value0, final long value1) throws IOException {
final int clen = WritableObjects.valueBytes(value1);
writeLong(out, value0, clen << 4);
WritableObjects.writeValue(out, value1, clen);
* @throws IOException if an I/O error occurs
* @throws NullPointerException if input is null
*/
- public static long readFirstLong(final @Nonnull DataInput in, final byte header) throws IOException {
+ public static long readFirstLong(final DataInput in, final byte header) throws IOException {
return WritableObjects.readLongBody(in, header);
}
* @throws IOException if an I/O error occurs
* @throws NullPointerException if input is null
*/
- public static long readSecondLong(final @Nonnull DataInput in, final byte header) throws IOException {
- return WritableObjects.readLongBody(in, (byte)(header >>> 4));
+ public static long readSecondLong(final DataInput in, final byte header) throws IOException {
+ return WritableObjects.readLongBody(in, (byte)(header >> 4));
}
private static void writeValue(final DataOutput out, final long value, final int bytes) throws IOException {
int left = bytes;
if (left >= 4) {
left -= 4;
- out.writeInt((int)(value >>> (left * Byte.SIZE)));
+ out.writeInt((int)(value >>> left * Byte.SIZE));
}
if (left >= 2) {
left -= 2;
- out.writeShort((int)(value >>> (left * Byte.SIZE)));
+ out.writeShort((int)(value >>> left * Byte.SIZE));
}
if (left > 0) {
out.writeByte((int)(value & 0xFF));
if ((value & 0xFFFFFFFF00000000L) != 0) {
if ((value & 0xFFFF000000000000L) != 0) {
return (value & 0xFF00000000000000L) != 0 ? 8 : 7;
- } else {
- return (value & 0x0000FF0000000000L) != 0 ? 6 : 5;
}
+ return (value & 0x0000FF0000000000L) != 0 ? 6 : 5;
} else if ((value & 0x00000000FFFFFFFFL) != 0) {
if ((value & 0x00000000FFFF0000L) != 0) {
return (value & 0x00000000FF000000L) != 0 ? 4 : 3;
- } else {
- return (value & 0x000000000000FF00L) != 0 ? 2 : 1;
}
+ return (value & 0x000000000000FF00L) != 0 ? 2 : 1;
} else {
return 0;
}