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
17package com.google.common.collect;
18
19import com.google.common.annotations.GwtCompatible;
20import com.google.common.annotations.GwtIncompatible;
21
22import java.lang.reflect.Array;
23
24/**
25 * Methods factored out so that they can be emulated differently in GWT.
26 *
27 * @author Hayward Chan
28 */
29@GwtCompatible(emulated = true)
30class Platform {
31  /**
32   * Clone the given array using {@link Object#clone()}.  It is factored out so
33   * that it can be emulated in GWT.
34   */
35  static <T> T[] clone(T[] array) {
36    return array.clone();
37  }
38
39  /**
40   * Wrapper around {@link System#arraycopy} so that it can be emulated
41   * correctly in GWT.
42   *
43   * <p>It is only intended for the case {@code src} and {@code dest} are
44   * different.  It also doesn't validate the types and indices.
45   *
46   * <p>As of GWT 2.0, The built-in {@link System#arraycopy} doesn't work
47   * in general case.  See
48   * http://code.google.com/p/google-web-toolkit/issues/detail?id=3621
49   * for more details.
50   */
51  static void unsafeArrayCopy(
52      Object[] src, int srcPos, Object[] dest, int destPos, int length) {
53    System.arraycopy(src, srcPos, dest, destPos, length);
54  }
55
56  /**
57   * Returns a new array of the given length with the specified component type.
58   *
59   * @param type the component type
60   * @param length the length of the new array
61   */
62  @GwtIncompatible("Array.newInstance(Class, int)")
63  @SuppressWarnings("unchecked")
64  static <T> T[] newArray(Class<T> type, int length) {
65    return (T[]) Array.newInstance(type, length);
66  }
67
68  /**
69   * Returns a new array of the given length with the same type as a reference
70   * array.
71   *
72   * @param reference any array of the desired type
73   * @param length the length of the new array
74   */
75  static <T> T[] newArray(T[] reference, int length) {
76    Class<?> type = reference.getClass().getComponentType();
77
78    // the cast is safe because
79    // result.getClass() == reference.getClass().getComponentType()
80    @SuppressWarnings("unchecked")
81    T[] result = (T[]) Array.newInstance(type, length);
82    return result;
83  }
84
85  /**
86   * Configures the given map maker to use weak keys, if possible; does nothing
87   * otherwise (i.e., in GWT). This is sometimes acceptable, when only
88   * server-side code could generate enough volume that reclamation becomes
89   * important.
90   */
91  static MapMaker tryWeakKeys(MapMaker mapMaker) {
92    return mapMaker.weakKeys();
93  }
94
95  private Platform() {}
96}
97