View Javadoc
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.Iterator;
20  import java.util.Locale;
21  import java.util.ServiceConfigurationError;
22  import java.util.ServiceLoader;
23  import java.util.logging.Level;
24  import java.util.logging.Logger;
25  import java.util.regex.Pattern;
26  import javax.annotation.Nullable;
27  
28  /**
29   * Methods factored out so that they can be emulated differently in GWT.
30   *
31   * @author Jesse Wilson
32   */
33  @GwtCompatible(emulated = true)
34  final class Platform {
35    private static final Logger logger = Logger.getLogger(Platform.class.getName());
36    private static final PatternCompiler patternCompiler = loadPatternCompiler();
37  
38    private Platform() {}
39  
40    /** Calls {@link System#nanoTime()}. */
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
52          ? Optional.<T>absent()
53          : Optional.of(enumClass.cast(ref.get()));
54    }
55  
56    static String formatCompact4Digits(double value) {
57      return String.format(Locale.ROOT, "%.4g", value);
58    }
59  
60    static boolean stringIsNullOrEmpty(@Nullable String string) {
61      return string == null || string.isEmpty();
62    }
63  
64    static CommonPattern compilePattern(String pattern) {
65      Preconditions.checkNotNull(pattern);
66      return patternCompiler.compile(pattern);
67    }
68  
69    static boolean usingJdkPatternCompiler() {
70      return patternCompiler instanceof JdkPatternCompiler;
71    }
72  
73    private static PatternCompiler loadPatternCompiler() {
74      ServiceLoader<PatternCompiler> loader = ServiceLoader.load(PatternCompiler.class);
75      // Returns the first PatternCompiler that loads successfully.
76      try {
77        for (Iterator<PatternCompiler> it = loader.iterator(); it.hasNext();) {
78          try {
79            return it.next();
80          } catch (ServiceConfigurationError e) {
81            logPatternCompilerError(e);
82          }
83        }
84      } catch (ServiceConfigurationError e) { // from hasNext()
85        logPatternCompilerError(e);
86      }
87      // Fall back to the JDK regex library.
88      return new JdkPatternCompiler();
89    }
90  
91    private static void logPatternCompilerError(ServiceConfigurationError e) {
92      logger.log(Level.WARNING, "Error loading regex compiler, falling back to next option", e);
93    }
94  
95    private static final class JdkPatternCompiler implements PatternCompiler {
96      @Override
97      public CommonPattern compile(String pattern) {
98        return new JdkPattern(Pattern.compile(pattern));
99      }
100   }
101 }