001/* 002 * Copyright (C) 2011 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the 010 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either 011 * express or implied. See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014 015package com.google.common.primitives; 016 017import static com.google.common.base.Preconditions.checkArgument; 018import static com.google.common.base.Preconditions.checkNotNull; 019 020import com.google.common.annotations.Beta; 021import com.google.common.annotations.GwtCompatible; 022 023import java.util.Arrays; 024import java.util.Comparator; 025 026/** 027 * Static utility methods pertaining to {@code int} primitives that interpret values as 028 * <i>unsigned</i> (that is, any negative value {@code x} is treated as the positive value 029 * {@code 2^32 + x}). The methods for which signedness is not an issue are in {@link Ints}, as well 030 * as signed versions of methods for which signedness is an issue. 031 * 032 * <p>In addition, this class provides several static methods for converting an {@code int} to a 033 * {@code String} and a {@code String} to an {@code int} that treat the {@code int} as an unsigned 034 * number. 035 * 036 * <p>Users of these utilities must be <i>extremely careful</i> not to mix up signed and unsigned 037 * {@code int} values. When possible, it is recommended that the {@link UnsignedInteger} wrapper 038 * class be used, at a small efficiency penalty, to enforce the distinction in the type system. 039 * 040 * <p>See the Guava User Guide article on <a href= 041 * "http://code.google.com/p/guava-libraries/wiki/PrimitivesExplained#Unsigned_support"> 042 * unsigned primitive utilities</a>. 043 * 044 * @author Louis Wasserman 045 * @since 11.0 046 */ 047@Beta 048@GwtCompatible 049public final class UnsignedInts { 050 static final long INT_MASK = 0xffffffffL; 051 052 private UnsignedInts() {} 053 054 static int flip(int value) { 055 return value ^ Integer.MIN_VALUE; 056 } 057 058 /** 059 * Compares the two specified {@code int} values, treating them as unsigned values between 060 * {@code 0} and {@code 2^32 - 1} inclusive. 061 * 062 * @param a the first unsigned {@code int} to compare 063 * @param b the second unsigned {@code int} to compare 064 * @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is 065 * greater than {@code b}; or zero if they are equal 066 */ 067 public static int compare(int a, int b) { 068 return Ints.compare(flip(a), flip(b)); 069 } 070 071 /** 072 * Returns the value of the given {@code int} as a {@code long}, when treated as unsigned. 073 */ 074 public static long toLong(int value) { 075 return value & INT_MASK; 076 } 077 078 /** 079 * Returns the least value present in {@code array}, treating values as unsigned. 080 * 081 * @param array a <i>nonempty</i> array of unsigned {@code int} values 082 * @return the value present in {@code array} that is less than or equal to every other value in 083 * the array according to {@link #compare} 084 * @throws IllegalArgumentException if {@code array} is empty 085 */ 086 public static int min(int... array) { 087 checkArgument(array.length > 0); 088 int min = flip(array[0]); 089 for (int i = 1; i < array.length; i++) { 090 int next = flip(array[i]); 091 if (next < min) { 092 min = next; 093 } 094 } 095 return flip(min); 096 } 097 098 /** 099 * Returns the greatest value present in {@code array}, treating values as unsigned. 100 * 101 * @param array a <i>nonempty</i> array of unsigned {@code int} values 102 * @return the value present in {@code array} that is greater than or equal to every other value 103 * in the array according to {@link #compare} 104 * @throws IllegalArgumentException if {@code array} is empty 105 */ 106 public static int max(int... array) { 107 checkArgument(array.length > 0); 108 int max = flip(array[0]); 109 for (int i = 1; i < array.length; i++) { 110 int next = flip(array[i]); 111 if (next > max) { 112 max = next; 113 } 114 } 115 return flip(max); 116 } 117 118 /** 119 * Returns a string containing the supplied unsigned {@code int} values separated by 120 * {@code separator}. For example, {@code join("-", 1, 2, 3)} returns the string {@code "1-2-3"}. 121 * 122 * @param separator the text that should appear between consecutive values in the resulting 123 * string (but not at the start or end) 124 * @param array an array of unsigned {@code int} values, possibly empty 125 */ 126 public static String join(String separator, int... array) { 127 checkNotNull(separator); 128 if (array.length == 0) { 129 return ""; 130 } 131 132 // For pre-sizing a builder, just get the right order of magnitude 133 StringBuilder builder = new StringBuilder(array.length * 5); 134 builder.append(toString(array[0])); 135 for (int i = 1; i < array.length; i++) { 136 builder.append(separator).append(toString(array[i])); 137 } 138 return builder.toString(); 139 } 140 141 /** 142 * Returns a comparator that compares two arrays of unsigned {@code int} values lexicographically. 143 * That is, it compares, using {@link #compare(int, int)}), the first pair of values that follow 144 * any common prefix, or when one array is a prefix of the other, treats the shorter array as the 145 * lesser. For example, {@code [] < [1] < [1, 2] < [2] < [1 << 31]}. 146 * 147 * <p>The returned comparator is inconsistent with {@link Object#equals(Object)} (since arrays 148 * support only identity equality), but it is consistent with {@link Arrays#equals(int[], int[])}. 149 * 150 * @see <a href="http://en.wikipedia.org/wiki/Lexicographical_order"> Lexicographical order 151 * article at Wikipedia</a> 152 */ 153 public static Comparator<int[]> lexicographicalComparator() { 154 return LexicographicalComparator.INSTANCE; 155 } 156 157 enum LexicographicalComparator implements Comparator<int[]> { 158 INSTANCE; 159 160 @Override 161 public int compare(int[] left, int[] right) { 162 int minLength = Math.min(left.length, right.length); 163 for (int i = 0; i < minLength; i++) { 164 if (left[i] != right[i]) { 165 return UnsignedInts.compare(left[i], right[i]); 166 } 167 } 168 return left.length - right.length; 169 } 170 } 171 172 /** 173 * Returns dividend / divisor, where the dividend and divisor are treated as unsigned 32-bit 174 * quantities. 175 * 176 * @param dividend the dividend (numerator) 177 * @param divisor the divisor (denominator) 178 * @throws ArithmeticException if divisor is 0 179 */ 180 public static int divide(int dividend, int divisor) { 181 return (int) (toLong(dividend) / toLong(divisor)); 182 } 183 184 /** 185 * Returns dividend % divisor, where the dividend and divisor are treated as unsigned 32-bit 186 * quantities. 187 * 188 * @param dividend the dividend (numerator) 189 * @param divisor the divisor (denominator) 190 * @throws ArithmeticException if divisor is 0 191 */ 192 public static int remainder(int dividend, int divisor) { 193 return (int) (toLong(dividend) % toLong(divisor)); 194 } 195 196 /** 197 * Returns the unsigned {@code int} value represented by the given string. 198 * 199 * Accepts a decimal, hexadecimal, or octal number given by specifying the following prefix: 200 * 201 * <ul> 202 * <li>{@code 0x}<i>HexDigits</i> 203 * <li>{@code 0X}<i>HexDigits</i> 204 * <li>{@code #}<i>HexDigits</i> 205 * <li>{@code 0}<i>OctalDigits</i> 206 * </ul> 207 * 208 * @throws NumberFormatException if the string does not contain a valid unsigned {@code int} value 209 * @since 13.0 210 */ 211 public static int decode(String stringValue) { 212 ParseRequest request = ParseRequest.fromString(stringValue); 213 214 try { 215 return parseUnsignedInt(request.rawValue, request.radix); 216 } catch (NumberFormatException e) { 217 NumberFormatException decodeException = 218 new NumberFormatException("Error parsing value: " + stringValue); 219 decodeException.initCause(e); 220 throw decodeException; 221 } 222 } 223 224 /** 225 * Returns the unsigned {@code int} value represented by the given decimal string. 226 * 227 * @throws NumberFormatException if the string does not contain a valid unsigned {@code int} value 228 * @throws NullPointerException if {@code s} is null 229 * (in contrast to {@link Integer#parseInt(String)}) 230 */ 231 public static int parseUnsignedInt(String s) { 232 return parseUnsignedInt(s, 10); 233 } 234 235 /** 236 * Returns the unsigned {@code int} value represented by a string with the given radix. 237 * 238 * @param string the string containing the unsigned integer representation to be parsed. 239 * @param radix the radix to use while parsing {@code s}; must be between 240 * {@link Character#MIN_RADIX} and {@link Character#MAX_RADIX}. 241 * @throws NumberFormatException if the string does not contain a valid unsigned {@code int}, or 242 * if supplied radix is invalid. 243 * @throws NullPointerException if {@code s} is null 244 * (in contrast to {@link Integer#parseInt(String)}) 245 */ 246 public static int parseUnsignedInt(String string, int radix) { 247 checkNotNull(string); 248 long result = Long.parseLong(string, radix); 249 if ((result & INT_MASK) != result) { 250 throw new NumberFormatException("Input " + string + " in base " + radix 251 + " is not in the range of an unsigned integer"); 252 } 253 return (int) result; 254 } 255 256 /** 257 * Returns a string representation of x, where x is treated as unsigned. 258 */ 259 public static String toString(int x) { 260 return toString(x, 10); 261 } 262 263 /** 264 * Returns a string representation of {@code x} for the given radix, where {@code x} is treated 265 * as unsigned. 266 * 267 * @param x the value to convert to a string. 268 * @param radix the radix to use while working with {@code x} 269 * @throws IllegalArgumentException if {@code radix} is not between {@link Character#MIN_RADIX} 270 * and {@link Character#MAX_RADIX}. 271 */ 272 public static String toString(int x, int radix) { 273 long asLong = x & INT_MASK; 274 return Long.toString(asLong, radix); 275 } 276}