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 25 /** 26 * Methods factored out so that they can be emulated differently in GWT. 27 * 28 * @author Hayward Chan 29 */ 30 @GwtCompatible(emulated = true) 31 final class Platform { 32 /** Returns the platform preferred implementation of a map based on a hash table. */ newHashMapWithExpectedSize(int expectedSize)33 static <K, V> Map<K, V> newHashMapWithExpectedSize(int expectedSize) { 34 return CompactHashMap.createWithExpectedSize(expectedSize); 35 } 36 37 /** 38 * Returns the platform preferred implementation of an insertion ordered map based on a hash 39 * table. 40 */ newLinkedHashMapWithExpectedSize(int expectedSize)41 static <K, V> Map<K, V> newLinkedHashMapWithExpectedSize(int expectedSize) { 42 return CompactLinkedHashMap.createWithExpectedSize(expectedSize); 43 } 44 45 /** Returns the platform preferred implementation of a set based on a hash table. */ newHashSetWithExpectedSize(int expectedSize)46 static <E> Set<E> newHashSetWithExpectedSize(int expectedSize) { 47 return CompactHashSet.createWithExpectedSize(expectedSize); 48 } 49 50 /** 51 * Returns the platform preferred implementation of an insertion ordered set based on a hash 52 * table. 53 */ newLinkedHashSetWithExpectedSize(int expectedSize)54 static <E> Set<E> newLinkedHashSetWithExpectedSize(int expectedSize) { 55 return CompactLinkedHashSet.createWithExpectedSize(expectedSize); 56 } 57 58 /** 59 * Returns the platform preferred map implementation that preserves insertion order when used only 60 * for insertions. 61 */ preservesInsertionOrderOnPutsMap()62 static <K, V> Map<K, V> preservesInsertionOrderOnPutsMap() { 63 return CompactHashMap.create(); 64 } 65 66 /** 67 * Returns the platform preferred set implementation that preserves insertion order when used only 68 * for insertions. 69 */ preservesInsertionOrderOnAddsSet()70 static <E> Set<E> preservesInsertionOrderOnAddsSet() { 71 return CompactHashSet.create(); 72 } 73 74 /** 75 * Returns a new array of the given length with the same type as a reference array. 76 * 77 * @param reference any array of the desired type 78 * @param length the length of the new array 79 */ newArray(T[] reference, int length)80 static <T> T[] newArray(T[] reference, int length) { 81 Class<?> type = reference.getClass().getComponentType(); 82 83 // the cast is safe because 84 // result.getClass() == reference.getClass().getComponentType() 85 @SuppressWarnings("unchecked") 86 T[] result = (T[]) Array.newInstance(type, length); 87 return result; 88 } 89 90 /** Equivalent to Arrays.copyOfRange(source, from, to, arrayOfType.getClass()). */ copy(Object[] source, int from, int to, T[] arrayOfType)91 static <T> T[] copy(Object[] source, int from, int to, T[] arrayOfType) { 92 return Arrays.copyOfRange(source, from, to, (Class<? extends T[]>) arrayOfType.getClass()); 93 } 94 95 /** 96 * Configures the given map maker to use weak keys, if possible; does nothing otherwise (i.e., in 97 * GWT). This is sometimes acceptable, when only server-side code could generate enough volume 98 * that reclamation becomes important. 99 */ tryWeakKeys(MapMaker mapMaker)100 static MapMaker tryWeakKeys(MapMaker mapMaker) { 101 return mapMaker.weakKeys(); 102 } 103 reduceIterationsIfGwt(int iterations)104 static int reduceIterationsIfGwt(int iterations) { 105 return iterations; 106 } 107 reduceExponentIfGwt(int exponent)108 static int reduceExponentIfGwt(int exponent) { 109 return exponent; 110 } 111 checkGwtRpcEnabled()112 static void checkGwtRpcEnabled() {} 113 Platform()114 private Platform() {} 115 } 116