AndroidInteger.java revision 1d580d0f6ee4f21eb309ba7b509d2c6d671c4044
1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17
18package com.google.common.primitives;
19
20import static com.google.common.base.Preconditions.checkArgument;
21import static com.google.common.base.Preconditions.checkNotNull;
22
23import javax.annotation.CheckForNull;
24
25/**
26 * Static utility methods derived from Android's {@code Integer.java}.
27 */
28final class AndroidInteger {
29  /**
30   * See {@link Ints#tryParse(String)} for the public interface.
31   */
32  @CheckForNull
33  static Integer tryParse(String string) {
34    return tryParse(string, 10);
35  }
36
37  /**
38   * See {@link Ints#tryParse(String, int)} for the public interface.
39   */
40  @CheckForNull
41  static Integer tryParse(String string, int radix) {
42    checkNotNull(string);
43    checkArgument(radix >= Character.MIN_RADIX,
44        "Invalid radix %s, min radix is %s", radix, Character.MIN_RADIX);
45    checkArgument(radix <= Character.MAX_RADIX,
46        "Invalid radix %s, max radix is %s", radix, Character.MAX_RADIX);
47    int length = string.length(), i = 0;
48    if (length == 0) {
49      return null;
50    }
51    boolean negative = string.charAt(i) == '-';
52    if (negative && ++i == length) {
53      return null;
54    }
55    return tryParse(string, i, radix, negative);
56  }
57
58  @CheckForNull
59  private static Integer tryParse(String string, int offset, int radix,
60      boolean negative) {
61    int max = Integer.MIN_VALUE / radix;
62    int result = 0, length = string.length();
63    while (offset < length) {
64      int digit = Character.digit(string.charAt(offset++), radix);
65      if (digit == -1) {
66        return null;
67      }
68      if (max > result) {
69        return null;
70      }
71      int next = result * radix - digit;
72      if (next > result) {
73        return null;
74      }
75      result = next;
76    }
77    if (!negative) {
78      result = -result;
79      if (result < 0) {
80        return null;
81      }
82    }
83    // For GWT where ints do not overflow
84    if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) {
85      return null;
86    }
87    return result;
88  }
89
90  private AndroidInteger() {}
91}
92