1 /* 2 * Copyright (C) 2008 Google Inc. 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 com.google.common.annotations.GwtIncompatible; 21 22 import java.lang.reflect.Array; 23 import java.util.List; 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 class Platform { 32 33 /** 34 * Calls {@link List#subList(int, int)}. Factored out so that it can be 35 * emulated in GWT. 36 * 37 * <p>This method is not supported in GWT yet. See <a 38 * href="http://code.google.com/p/google-web-toolkit/issues/detail?id=1791"> 39 * GWT issue 1791</a> 40 */ 41 @GwtIncompatible("List.subList") 42 static <T> List<T> subList(List<T> list, int fromIndex, int toIndex) { 43 return list.subList(fromIndex, toIndex); 44 } 45 46 /** 47 * Calls {@link Class#isInstance(Object)}. Factored out so that it can be 48 * emulated in GWT. 49 */ 50 @GwtIncompatible("Class.isInstance") 51 static boolean isInstance(Class<?> clazz, Object obj) { 52 return clazz.isInstance(obj); 53 } 54 55 /** 56 * Clone the given array using {@link Object#clone()}. It is factored out so 57 * that it can be emulated in GWT. 58 */ 59 static <T> T[] clone(T[] array) { 60 return array.clone(); 61 } 62 63 /** 64 * Wrapper around {@link System#arraycopy} so that it can be emulated 65 * correctly in GWT. 66 * 67 * <p>It is only intended for the case {@code src} and {@code dest} are 68 * different. It also doesn't validate the types and indices. 69 * 70 * <p>As of GWT 2.0, The built-in {@link System#arraycopy} doesn't work 71 * in general case. See 72 * http://code.google.com/p/google-web-toolkit/issues/detail?id=3621 73 * for more details. 74 */ 75 static void unsafeArrayCopy( 76 Object[] src, int srcPos, Object[] dest, int destPos, int length) { 77 System.arraycopy(src, srcPos, dest, destPos, length); 78 } 79 80 /** 81 * Returns a new array of the given length with the specified component type. 82 * 83 * @param type the component type 84 * @param length the length of the new array 85 */ 86 @GwtIncompatible("Array.newInstance(Class, int)") 87 @SuppressWarnings("unchecked") 88 static <T> T[] newArray(Class<T> type, int length) { 89 return (T[]) Array.newInstance(type, length); 90 } 91 92 /** 93 * Returns a new array of the given length with the same type as a reference 94 * array. 95 * 96 * @param reference any array of the desired type 97 * @param length the length of the new array 98 */ 99 static <T> T[] newArray(T[] reference, int length) { 100 Class<?> type = reference.getClass().getComponentType(); 101 102 // the cast is safe because 103 // result.getClass() == reference.getClass().getComponentType() 104 @SuppressWarnings("unchecked") 105 T[] result = (T[]) Array.newInstance(type, length); 106 return result; 107 } 108 109 /** 110 * Configures the given map maker to use weak keys, if possible; does nothing 111 * otherwise (i.e., in GWT). This is sometimes acceptable, when only 112 * server-side code could generate enough volume that reclamation becomes 113 * important. 114 */ 115 static MapMaker tryWeakKeys(MapMaker mapMaker) { 116 return mapMaker.weakKeys(); 117 } 118 119 private Platform() {} 120 } 121