Coverage Summary for Class: Platform (com.google.common.base)

Class Method, % Line, %
Platform 42.9% (6/14) 34.8% (8/23)
Platform$JdkPatternCompiler 66.7% (2/3) 66.7% (2/3)
Total 47.1% (8/17) 38.5% (10/26)


1 /* 2  * Copyright (C) 2009 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5  * in compliance with the License. You may obtain a copy of the License at 6  * 7  * http://www.apache.org/licenses/LICENSE-2.0 8  * 9  * Unless required by applicable law or agreed to in writing, software distributed under the License 10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11  * or implied. See the License for the specific language governing permissions and limitations under 12  * the License. 13  */ 14  15 package com.google.common.base; 16  17 import com.google.common.annotations.GwtCompatible; 18 import java.lang.ref.WeakReference; 19 import java.util.Locale; 20 import java.util.ServiceConfigurationError; 21 import java.util.logging.Level; 22 import java.util.logging.Logger; 23 import java.util.regex.Pattern; 24 import javax.annotation.CheckForNull; 25  26 /** 27  * Methods factored out so that they can be emulated differently in GWT. 28  * 29  * @author Jesse Wilson 30  */ 31 @GwtCompatible(emulated = true) 32 @ElementTypesAreNonnullByDefault 33 final class Platform { 34  private static final Logger logger = Logger.getLogger(Platform.class.getName()); 35  private static final PatternCompiler patternCompiler = loadPatternCompiler(); 36  37  private Platform() {} 38  39  /** Calls {@link System#nanoTime()}. */ 40  @SuppressWarnings("GoodTime") // reading system time without TimeSource 41  static long systemNanoTime() { 42  return System.nanoTime(); 43  } 44  45  static CharMatcher precomputeCharMatcher(CharMatcher matcher) { 46  return matcher.precomputedInternal(); 47  } 48  49  static <T extends Enum<T>> Optional<T> getEnumIfPresent(Class<T> enumClass, String value) { 50  WeakReference<? extends Enum<?>> ref = Enums.getEnumConstants(enumClass).get(value); 51  return ref == null ? Optional.<T>absent() : Optional.of(enumClass.cast(ref.get())); 52  } 53  54  static String formatCompact4Digits(double value) { 55  return String.format(Locale.ROOT, "%.4g", value); 56  } 57  58  static boolean stringIsNullOrEmpty(@CheckForNull String string) { 59  return string == null || string.isEmpty(); 60  } 61  62  /** 63  * Returns the string if it is not null, or an empty string otherwise. 64  * 65  * @param string the string to test and possibly return 66  * @return {@code string} if it is not null; {@code ""} otherwise 67  */ 68  static String nullToEmpty(@CheckForNull String string) { 69  return (string == null) ? "" : string; 70  } 71  72  /** 73  * Returns the string if it is not empty, or a null string otherwise. 74  * 75  * @param string the string to test and possibly return 76  * @return {@code string} if it is not empty; {@code null} otherwise 77  */ 78  @CheckForNull 79  static String emptyToNull(@CheckForNull String string) { 80  return stringIsNullOrEmpty(string) ? null : string; 81  } 82  83  static CommonPattern compilePattern(String pattern) { 84  Preconditions.checkNotNull(pattern); 85  return patternCompiler.compile(pattern); 86  } 87  88  static boolean patternCompilerIsPcreLike() { 89  return patternCompiler.isPcreLike(); 90  } 91  92  private static PatternCompiler loadPatternCompiler() { 93  return new JdkPatternCompiler(); 94  } 95  96  private static void logPatternCompilerError(ServiceConfigurationError e) { 97  logger.log(Level.WARNING, "Error loading regex compiler, falling back to next option", e); 98  } 99  100  private static final class JdkPatternCompiler implements PatternCompiler { 101  @Override 102  public CommonPattern compile(String pattern) { 103  return new JdkPattern(Pattern.compile(pattern)); 104  } 105  106  @Override 107  public boolean isPcreLike() { 108  return true; 109  } 110  } 111  112  static void checkGwtRpcEnabled() { 113  String propertyName = "guava.gwt.emergency_reenable_rpc"; 114  115  if (!Boolean.parseBoolean(System.getProperty(propertyName, "false"))) { 116  throw new UnsupportedOperationException( 117  Strings.lenientFormat( 118  "We are removing GWT-RPC support for Guava types. You can temporarily reenable" 119  + " support by setting the system property %s to true. For more about system" 120  + " properties, see %s. For more about Guava's GWT-RPC support, see %s.", 121  propertyName, 122  "https://stackoverflow.com/q/5189914/28465", 123  "https://groups.google.com/d/msg/guava-announce/zHZTFg7YF3o/rQNnwdHeEwAJ")); 124  } 125  logger.log( 126  java.util.logging.Level.WARNING, 127  "Later in 2020, we will remove GWT-RPC support for Guava types. You are seeing this" 128  + " warning because you are sending a Guava type over GWT-RPC, which will break. You" 129  + " can identify which type by looking at the class name in the attached stack trace.", 130  new Throwable()); 131  } 132 }