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    
015    package com.google.common.primitives;
016    
017    import static com.google.common.base.Preconditions.checkArgument;
018    import static com.google.common.base.Preconditions.checkNotNull;
019    
020    import java.util.Arrays;
021    import java.util.Comparator;
022    
023    import com.google.common.annotations.Beta;
024    import com.google.common.annotations.GwtCompatible;
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     * @author Louis Wasserman
041     * @since 11.0
042     */
043    @Beta
044    @GwtCompatible
045    public final class UnsignedInts {
046      static final long INT_MASK = 0xffffffffL;
047    
048      private UnsignedInts() {}
049    
050      static int flip(int value) {
051        return value ^ Integer.MIN_VALUE;
052      }
053    
054      /**
055       * Compares the two specified {@code int} values, treating them as unsigned values between
056       * {@code 0} and {@code 2^32 - 1} inclusive.
057       * 
058       * @param a the first unsigned {@code int} to compare
059       * @param b the second unsigned {@code int} to compare
060       * @return a negative value if {@code a} is less than {@code b}; a positive value if {@code a} is
061       *         greater than {@code b}; or zero if they are equal
062       */
063      public static int compare(int a, int b) {
064        return Ints.compare(flip(a), flip(b));
065      }
066    
067      /**
068       * Returns the value of the given {@code int} as a {@code long}, when treated as unsigned.
069       */
070      public static long toLong(int value) {
071        return value & INT_MASK;
072      }
073    
074      /**
075       * Returns the least value present in {@code array}, treating values as unsigned.
076       * 
077       * @param array a <i>nonempty</i> array of unsigned {@code int} values
078       * @return the value present in {@code array} that is less than or equal to every other value in
079       *         the array according to {@link #compare}
080       * @throws IllegalArgumentException if {@code array} is empty
081       */
082      public static int min(int... array) {
083        checkArgument(array.length > 0);
084        int min = flip(array[0]);
085        for (int i = 1; i < array.length; i++) {
086          int next = flip(array[i]);
087          if (next < min) {
088            min = next;
089          }
090        }
091        return flip(min);
092      }
093    
094      /**
095       * Returns the greatest value present in {@code array}, treating values as unsigned.
096       * 
097       * @param array a <i>nonempty</i> array of unsigned {@code int} values
098       * @return the value present in {@code array} that is greater than or equal to every other value
099       *         in the array according to {@link #compare}
100       * @throws IllegalArgumentException if {@code array} is empty
101       */
102      public static int max(int... array) {
103        checkArgument(array.length > 0);
104        int max = flip(array[0]);
105        for (int i = 1; i < array.length; i++) {
106          int next = flip(array[i]);
107          if (next > max) {
108            max = next;
109          }
110        }
111        return flip(max);
112      }
113    
114      /**
115       * Returns a string containing the supplied unsigned {@code int} values separated by
116       * {@code separator}. For example, {@code join("-", 1, 2, 3)} returns the string {@code "1-2-3"}.
117       * 
118       * @param separator the text that should appear between consecutive values in the resulting
119       *        string (but not at the start or end)
120       * @param array an array of unsigned {@code int} values, possibly empty
121       */
122      public static String join(String separator, int... array) {
123        checkNotNull(separator);
124        if (array.length == 0) {
125          return "";
126        }
127    
128        // For pre-sizing a builder, just get the right order of magnitude
129        StringBuilder builder = new StringBuilder(array.length * 5);
130        builder.append(array[0]);
131        for (int i = 1; i < array.length; i++) {
132          builder.append(separator).append(toString(array[i]));
133        }
134        return builder.toString();
135      }
136    
137      /**
138       * Returns a comparator that compares two arrays of unsigned {@code int} values lexicographically.
139       * That is, it compares, using {@link #compare(int, int)}), the first pair of values that follow
140       * any common prefix, or when one array is a prefix of the other, treats the shorter array as the
141       * lesser. For example, {@code [] < [1] < [1, 2] < [2] < [1 << 31]}.
142       * 
143       * <p>The returned comparator is inconsistent with {@link Object#equals(Object)} (since arrays
144       * support only identity equality), but it is consistent with {@link Arrays#equals(int[], int[])}.
145       * 
146       * @see <a href="http://en.wikipedia.org/wiki/Lexicographical_order"> Lexicographical order
147       *      article at Wikipedia</a>
148       */
149      public static Comparator<int[]> lexicographicalComparator() {
150        return LexicographicalComparator.INSTANCE;
151      }
152    
153      enum LexicographicalComparator implements Comparator<int[]> {
154        INSTANCE;
155    
156        @Override
157        public int compare(int[] left, int[] right) {
158          int minLength = Math.min(left.length, right.length);
159          for (int i = 0; i < minLength; i++) {
160            if (left[i] != right[i]) {
161              return UnsignedInts.compare(left[i], right[i]);
162            }
163          }
164          return left.length - right.length;
165        }
166      }
167    
168      /**
169       * Returns dividend / divisor, where the dividend and divisor are treated as unsigned 32-bit
170       * quantities.
171       * 
172       * @param dividend the dividend (numerator)
173       * @param divisor the divisor (denominator)
174       * @throws ArithmeticException if divisor is 0
175       */
176      public static int divide(int dividend, int divisor) {
177        return (int) (toLong(dividend) / toLong(divisor));
178      }
179    
180      /**
181       * Returns dividend % divisor, where the dividend and divisor are treated as unsigned 32-bit
182       * quantities.
183       * 
184       * @param dividend the dividend (numerator)
185       * @param divisor the divisor (denominator)
186       * @throws ArithmeticException if divisor is 0
187       */
188      public static int remainder(int dividend, int divisor) {
189        return (int) (toLong(dividend) % toLong(divisor));
190      }
191    
192      /**
193       * Returns the unsigned {@code int} value represented by the given decimal string.
194       * 
195       * @throws NumberFormatException if the string does not contain a valid unsigned integer, or if
196       *         the value represented is too large to fit in an unsigned {@code int}.
197       * @throws NullPointerException if {@code s} is null
198       */
199      public static int parseUnsignedInt(String s) {
200        return parseUnsignedInt(s, 10);
201      }
202    
203      /**
204       * Returns the unsigned {@code int} value represented by a string with the given radix.
205       * 
206       * @param string the string containing the unsigned integer representation to be parsed.
207       * @param radix the radix to use while parsing {@code s}; must be between
208       *        {@link Character#MIN_RADIX} and {@link Character#MAX_RADIX}.
209       * @throws NumberFormatException if the string does not contain a valid unsigned {@code int}, or
210       *         if supplied radix is invalid.
211       */
212      public static int parseUnsignedInt(String string, int radix) {
213        checkNotNull(string);
214        long result = Long.parseLong(string, radix);
215        if ((result & INT_MASK) != result) {
216          throw new NumberFormatException("Input " + string + " in base " + radix
217              + " is not in the range of an unsigned integer");
218        }
219        return (int) result;
220      }
221    
222      /**
223       * Returns a string representation of x, where x is treated as unsigned.
224       */
225      public static String toString(int x) {
226        return toString(x, 10);
227      }
228    
229      /**
230       * Returns a string representation of {@code x} for the given radix, where {@code x} is treated
231       * as unsigned.
232       * 
233       * @param x the value to convert to a string.
234       * @param radix the radix to use while working with {@code x}
235       * @throws IllegalArgumentException if {@code radix} is not between {@link Character#MIN_RADIX}
236       *         and {@link Character#MAX_RADIX}.
237       */
238      public static String toString(int x, int radix) {
239        long asLong = x & INT_MASK;
240        return Long.toString(asLong, radix);
241      }
242    }