content: package backport.java.util.function;
  
  import java.util.Collection;
  import java.util.Map;
  
  import static java.lang.Integer.parseInt;
  
  /**
   * @author Saeed Masoumi (s-masoumi@live.com)
   */
  
  public final class BackportFuncs {
      public static <K, V> V computeIfAbsent(Map<K, V> map, K key,
                                             Function<? super K, ? extends V> mappingFunction) {
          Objects.requireNonNull(mappingFunction);
          V v;
          if ((v = map.get(key)) == null) {
              V newValue;
              if ((newValue = mappingFunction.apply(key)) != null) {
                  map.put(key, newValue);
                  return newValue;
              }
          }
  
          return v;
      }
  
      public static <T> void forEach(Collection<T> collection, Consumer<T> action) {
          Objects.requireNonNull(action);
          for (T t : collection) {
              action.accept(t);
          }
      }
  
      /**
       * Parses the string argument as an unsigned integer in the radix
       * specified by the second argument.  An unsigned integer maps the
       * values usually associated with negative numbers to positive
       * numbers larger than {@code MAX_VALUE}.
       * <p>
       * The characters in the string must all be digits of the
       * specified radix (as determined by whether {@link
       * java.lang.Character#digit(char, int)} returns a nonnegative
       * value), except that the first character may be an ASCII plus
       * sign {@code '+'} ({@code '\u005Cu002B'}). The resulting
       * integer value is returned.
       * <p>
       * <p>An exception of type {@code NumberFormatException} is
       * thrown if any of the following situations occurs:
       * <ul>
       * <li>The first argument is {@code null} or is a string of
       * length zero.
       * <p>
       * <li>The radix is either smaller than
       * {@link java.lang.Character#MIN_RADIX} or
       * larger than {@link java.lang.Character#MAX_RADIX}.
       * <p>
       * <li>Any character of the string is not a digit of the specified
       * radix, except that the first character may be a plus sign
       * {@code '+'} ({@code '\u005Cu002B'}) provided that the
       * string is longer than length 1.
       * <p>
       * <li>The value represented by the string is larger than the
       * largest unsigned {@code int}, 2<sup>32</sup>-1.
       * <p>
       * </ul>
       *
       * @param s     the {@code String} containing the unsigned integer representation to be parsed
       * @param radix the radix to be used while parsing {@code s}.
       * @return the integer represented by the string argument in the specified radix.
       * @throws NumberFormatException if the {@code String} does not contain a parsable {@code int}.
       */
      public static int parseUnsignedInt(String s, int radix)
              throws NumberFormatException {
          if (s == null) {
              throw new NumberFormatException("null");
          }
  
          int len = s.length();
          if (len > 0) {
              char firstChar = s.charAt(0);
              if (firstChar == '-') {
                  throw new
                          NumberFormatException(String.format("Illegal leading minus sign " +
                          "on unsigned string %s.", s));
              } else {
                  if (len <= 5 || // Integer.MAX_VALUE in Character.MAX_RADIX is 6 digits
                          (radix == 10 && len <= 9)) { // Integer.MAX_VALUE in base 10 is 10 digits
                      return parseInt(s, radix);
                  } else {
                      long ell = Long.parseLong(s, radix);
                      if ((ell & 0xffff_ffff_0000_0000L) == 0) {
                          return (int) ell;
                      } else {
                          throw new
                                  NumberFormatException(String.format("String value %s exceeds " +
                                  "range of unsigned int.", s));
                      }
                  }
              }
          } else {
              throw new NumberFormatException("For input string: \"" + s + "\"");
          }
      }
  
  }
  