001/** 002 * 003 * Copyright © 2015-2020 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 TODO javadoc me please 025 * @deprecated use {@link #requireUInt32(long)} instead. 026 */ 027 @Deprecated 028 // TODO: Remove in Smack 4.5. 029 public static void checkIfInUInt32Range(long value) { 030 requireUInt32(value); 031 } 032 033 /** 034 * Checks if the given long is within the range of an unsigned 32-bit integer, the XML type "xs:unsignedInt". 035 * 036 * @param value the long to check. 037 * @return the input value. 038 */ 039 public static long requireUInt32(long value) { 040 if (value < 0) { 041 throw new IllegalArgumentException("unsigned 32-bit integers can't be negative: " + value); 042 } 043 if (value > ((1L << 32) - 1)) { 044 throw new IllegalArgumentException("unsigned 32-bit integers can't be greater than 2^32 - 1: " + value); 045 } 046 return value; 047 } 048 049 /** 050 * Checks if the given int is within the range of an unsigned 16-bit integer, the XML type "xs:unsignedShort". 051 * 052 * @param value the int to check. 053 * @return the input value. 054 */ 055 public static int requireUShort16(int value) { 056 if (value < 0) { 057 throw new IllegalArgumentException("unsigned 16-bit integers can't be negative: " + value); 058 } 059 if (value > ((1 << 16) - 1)) { 060 throw new IllegalArgumentException("unsigned 16-bit integers can't be greater than 2^16 - 1: " + value); 061 } 062 return value; 063 } 064}