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

Class Class, % Method, % Line, %
Platform 100% (1/1) 53.3% (8/15) 47.8% (11/23)


1 /* 2  * Copyright (C) 2008 The Guava Authors 3  * 4  * Licensed under the Apache License, Version 2.0 (the "License"); 5  * you may not use this file except in compliance with the License. 6  * You may obtain a copy of the License at 7  * 8  * http://www.apache.org/licenses/LICENSE-2.0 9  * 10  * Unless required by applicable law or agreed to in writing, software 11  * distributed under the License is distributed on an "AS IS" BASIS, 12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13  * See the License for the specific language governing permissions and 14  * limitations under the License. 15  */ 16  17 package com.google.common.collect; 18  19 import com.google.common.annotations.GwtCompatible; 20 import java.lang.reflect.Array; 21 import java.util.Arrays; 22 import java.util.Map; 23 import java.util.Set; 24 import java.util.concurrent.ConcurrentHashMap; 25  26 /** 27  * Methods factored out so that they can be emulated differently in GWT. 28  * 29  * @author Hayward Chan 30  */ 31 @GwtCompatible(emulated = true) 32 final class Platform { 33  private static final java.util.logging.Logger logger = 34  java.util.logging.Logger.getLogger(Platform.class.getName()); 35  36  /** Returns the platform preferred implementation of a map based on a hash table. */ 37  static <K, V> Map<K, V> newHashMapWithExpectedSize(int expectedSize) { 38  return Maps.newHashMapWithExpectedSize(expectedSize); 39  } 40  41  /** 42  * Returns the platform preferred implementation of an insertion ordered map based on a hash 43  * table. 44  */ 45  static <K, V> Map<K, V> newLinkedHashMapWithExpectedSize(int expectedSize) { 46  return Maps.newLinkedHashMapWithExpectedSize(expectedSize); 47  } 48  49  /** Returns the platform preferred implementation of a set based on a hash table. */ 50  static <E> Set<E> newHashSetWithExpectedSize(int expectedSize) { 51  return Sets.newHashSetWithExpectedSize(expectedSize); 52  } 53  54  /** Returns the platform preferred implementation of a thread-safe hash set. */ 55  static <E> Set<E> newConcurrentHashSet() { 56  return ConcurrentHashMap.newKeySet(); 57  } 58  59  /** 60  * Returns the platform preferred implementation of an insertion ordered set based on a hash 61  * table. 62  */ 63  static <E> Set<E> newLinkedHashSetWithExpectedSize(int expectedSize) { 64  return Sets.newLinkedHashSetWithExpectedSize(expectedSize); 65  } 66  67  /** 68  * Returns the platform preferred map implementation that preserves insertion order when used only 69  * for insertions. 70  */ 71  static <K, V> Map<K, V> preservesInsertionOrderOnPutsMap() { 72  return Maps.newLinkedHashMap(); 73  } 74  75  /** 76  * Returns the platform preferred set implementation that preserves insertion order when used only 77  * for insertions. 78  */ 79  static <E> Set<E> preservesInsertionOrderOnAddsSet() { 80  return Sets.newLinkedHashSet(); 81  } 82  83  /** 84  * Returns a new array of the given length with the same type as a reference array. 85  * 86  * @param reference any array of the desired type 87  * @param length the length of the new array 88  */ 89  static <T> T[] newArray(T[] reference, int length) { 90  Class<?> type = reference.getClass().getComponentType(); 91  92  // the cast is safe because 93  // result.getClass() == reference.getClass().getComponentType() 94  @SuppressWarnings("unchecked") 95  T[] result = (T[]) Array.newInstance(type, length); 96  return result; 97  } 98  99  /** Equivalent to Arrays.copyOfRange(source, from, to, arrayOfType.getClass()). */ 100  static <T> T[] copy(Object[] source, int from, int to, T[] arrayOfType) { 101  return Arrays.copyOfRange(source, from, to, (Class<? extends T[]>) arrayOfType.getClass()); 102  } 103  104  /** 105  * Configures the given map maker to use weak keys, if possible; does nothing otherwise (i.e., in 106  * GWT). This is sometimes acceptable, when only server-side code could generate enough volume 107  * that reclamation becomes important. 108  */ 109  static MapMaker tryWeakKeys(MapMaker mapMaker) { 110  return mapMaker.weakKeys(); 111  } 112  113  static int reduceIterationsIfGwt(int iterations) { 114  return iterations; 115  } 116  117  static int reduceExponentIfGwt(int exponent) { 118  return exponent; 119  } 120  121  static void checkGwtRpcEnabled() { 122  String propertyName = "guava.gwt.emergency_reenable_rpc"; 123  124  if (!Boolean.parseBoolean(System.getProperty(propertyName, "false"))) { 125  throw new UnsupportedOperationException( 126  com.google.common.base.Strings.lenientFormat( 127  "We are removing GWT-RPC support for Guava types. You can temporarily reenable" 128  + " support by setting the system property %s to true. For more about system" 129  + " properties, see %s. For more about Guava's GWT-RPC support, see %s.", 130  propertyName, 131  "https://stackoverflow.com/q/5189914/28465", 132  "https://groups.google.com/d/msg/guava-announce/zHZTFg7YF3o/rQNnwdHeEwAJ")); 133  } 134  logger.log( 135  java.util.logging.Level.WARNING, 136  "Later in 2020, we will remove GWT-RPC support for Guava types. You are seeing this" 137  + " warning because you are sending a Guava type over GWT-RPC, which will break. You" 138  + " can identify which type by looking at the class name in the attached stack trace.", 139  new Throwable()); 140  } 141  142  private Platform() {} 143 }