From b834df65e9b6ac4226c321553e358421e107c53c Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 10 Jun 2019 17:37:20 +0200 Subject: [PATCH] Add NumberUtil.requireUShort16(int) --- .../org/jivesoftware/smack/util/NumberUtil.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/smack-core/src/main/java/org/jivesoftware/smack/util/NumberUtil.java b/smack-core/src/main/java/org/jivesoftware/smack/util/NumberUtil.java index 4261ca097..9f5040f52 100644 --- a/smack-core/src/main/java/org/jivesoftware/smack/util/NumberUtil.java +++ b/smack-core/src/main/java/org/jivesoftware/smack/util/NumberUtil.java @@ -43,4 +43,19 @@ public class NumberUtil { throw new IllegalArgumentException("unsigned 32-bit integers can't be greater then 2^32 - 1"); } } + + /** + * Checks if the given int is within the range of an unsigned 16-bit integer, the XML type "xs:unsignedShort". + * + * @param value + */ + public static int requireUShort16(int value) { + if (value < 0) { + throw new IllegalArgumentException("unsigned 16-bit integers can't be negative"); + } + if (value > ((1 << 16) - 1)) { + throw new IllegalArgumentException("unsigned 16-bit integers can't be greater than 2^16 - 1"); + } + return value; + } }