001/**
002 *
003 * Copyright © 2015-2024 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack.util;
018
019public class NumberUtil {
020
021    /**
022     * Checks if the given long is within the range of an unsigned 32-bit integer, the XML type "xs:unsignedInt".
023     *
024     * @param value the long to check.
025     * @return the input value.
026     */
027    public static long requireUInt32(long value) {
028        if (value < 0) {
029            throw new IllegalArgumentException("unsigned 32-bit integers can't be negative: " + value);
030        }
031        if (value > ((1L << 32) - 1)) {
032            throw new IllegalArgumentException("unsigned 32-bit integers can't be greater than 2^32 - 1: " + value);
033        }
034        return value;
035    }
036
037    /**
038     * Checks if the given int is within the range of an unsigned 16-bit integer, the XML type "xs:unsignedShort".
039     *
040     * @param value the int to check.
041     * @return the input value.
042     */
043    public static int requireUShort16(int value) {
044        if (value < 0) {
045            throw new IllegalArgumentException("unsigned 16-bit integers can't be negative: " + value);
046        }
047        if (value > ((1 << 16) - 1)) {
048            throw new IllegalArgumentException("unsigned 16-bit integers can't be greater than 2^16 - 1: " + value);
049        }
050        return value;
051    }
052}