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.regex.Pattern; 21 import javax.annotation.CheckForNull; 22 23 /** 24 * Methods factored out so that they can be emulated differently in GWT. 25 * 26 * @author Jesse Wilson 27 */ 28 @GwtCompatible(emulated = true) 29 @ElementTypesAreNonnullByDefault 30 final class Platform { 31 private static final PatternCompiler patternCompiler = loadPatternCompiler(); 32 Platform()33 private Platform() {} 34 precomputeCharMatcher(CharMatcher matcher)35 static CharMatcher precomputeCharMatcher(CharMatcher matcher) { 36 return matcher.precomputedInternal(); 37 } 38 getEnumIfPresent(Class<T> enumClass, String value)39 static <T extends Enum<T>> Optional<T> getEnumIfPresent(Class<T> enumClass, String value) { 40 WeakReference<? extends Enum<?>> ref = Enums.getEnumConstants(enumClass).get(value); 41 /* 42 * We use `fromNullable` instead of `of` because `WeakReference.get()` has a nullable return 43 * type. 44 * 45 * In practice, we are very unlikely to see `null`: The `WeakReference` to the enum constant 46 * won't be cleared as long as the enum constant is referenced somewhere, and the enum constant 47 * is referenced somewhere for as long as the enum class is loaded. *Maybe in theory* the enum 48 * class could be unloaded after the above call to `getEnumConstants` but before we call 49 * `get()`, but that is vanishingly unlikely. 50 */ 51 return ref == null ? Optional.absent() : Optional.fromNullable(enumClass.cast(ref.get())); 52 } 53 formatCompact4Digits(double value)54 static String formatCompact4Digits(double value) { 55 return String.format(Locale.ROOT, "%.4g", value); 56 } 57 stringIsNullOrEmpty(@heckForNull String string)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 */ nullToEmpty(@heckForNull String string)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 emptyToNull(@heckForNull String string)79 static String emptyToNull(@CheckForNull String string) { 80 return stringIsNullOrEmpty(string) ? null : string; 81 } 82 compilePattern(String pattern)83 static CommonPattern compilePattern(String pattern) { 84 Preconditions.checkNotNull(pattern); 85 return patternCompiler.compile(pattern); 86 } 87 patternCompilerIsPcreLike()88 static boolean patternCompilerIsPcreLike() { 89 return patternCompiler.isPcreLike(); 90 } 91 loadPatternCompiler()92 private static PatternCompiler loadPatternCompiler() { 93 /* 94 * We'd normally use ServiceLoader here, but it hurts Android startup performance. To avoid 95 * that, we hardcode the JDK Pattern compiler on Android (and, inadvertently, on App Engine and 96 * in Guava, at least for now). 97 */ 98 return new JdkPatternCompiler(); 99 } 100 101 private static final class JdkPatternCompiler implements PatternCompiler { 102 @Override compile(String pattern)103 public CommonPattern compile(String pattern) { 104 return new JdkPattern(Pattern.compile(pattern)); 105 } 106 107 @Override isPcreLike()108 public boolean isPcreLike() { 109 return true; 110 } 111 } 112 } 113