1090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/*
21d580d0f6ee4f21eb309ba7b509d2c6d671c4044Bjorn Bringert * Copyright (C) 2008 The Guava Authors
3090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
4090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Licensed under the Apache License, Version 2.0 (the "License");
5090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * you may not use this file except in compliance with the License.
6090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * You may obtain a copy of the License at
7090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
8090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * http://www.apache.org/licenses/LICENSE-2.0
9090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
10090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Unless required by applicable law or agreed to in writing, software
11090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * distributed under the License is distributed on an "AS IS" BASIS,
12090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * See the License for the specific language governing permissions and
14090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * limitations under the License.
15090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
16090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
17090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonpackage com.google.common.collect;
18090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
19090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport com.google.common.annotations.GwtCompatible;
200888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport com.google.common.base.Function;
210888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport com.google.common.base.Predicate;
220888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport com.google.common.collect.Maps.EntryTransformer;
23090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
24090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilsonimport java.lang.reflect.Array;
250888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport java.util.Collections;
260888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport java.util.Map;
270888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport java.util.Set;
280888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport java.util.SortedMap;
290888a09821a98ac0680fad765217302858e70fa4Paul Duffinimport java.util.SortedSet;
30090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
31090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson/**
32090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * Methods factored out so that they can be emulated differently in GWT.
33090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson *
34090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson * @author Hayward Chan
35090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson */
36090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson@GwtCompatible(emulated = true)
370888a09821a98ac0680fad765217302858e70fa4Paul Duffinfinal class Platform {
38090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  /**
39090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * Returns a new array of the given length with the same type as a reference
40090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * array.
41090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   *
42090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param reference any array of the desired type
43090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   * @param length the length of the new array
44090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson   */
45090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  static <T> T[] newArray(T[] reference, int length) {
46090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    Class<?> type = reference.getClass().getComponentType();
47090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
48090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    // the cast is safe because
49090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    // result.getClass() == reference.getClass().getComponentType()
50090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    @SuppressWarnings("unchecked")
51090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    T[] result = (T[]) Array.newInstance(type, length);
52090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson    return result;
53090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  }
540888a09821a98ac0680fad765217302858e70fa4Paul Duffin
550888a09821a98ac0680fad765217302858e70fa4Paul Duffin  static <E> Set<E> newSetFromMap(Map<E, Boolean> map) {
560888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Sets.newSetFromMap(map);
570888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
58090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson
59bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  /**
60bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * Configures the given map maker to use weak keys, if possible; does nothing
61bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * otherwise (i.e., in GWT). This is sometimes acceptable, when only
62bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * server-side code could generate enough volume that reclamation becomes
63bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   * important.
64bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor   */
65bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  static MapMaker tryWeakKeys(MapMaker mapMaker) {
66bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor    return mapMaker.weakKeys();
67bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor  }
68bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor
690888a09821a98ac0680fad765217302858e70fa4Paul Duffin  static <K, V1, V2> SortedMap<K, V2> mapsTransformEntriesSortedMap(
700888a09821a98ac0680fad765217302858e70fa4Paul Duffin      SortedMap<K, V1> fromMap,
710888a09821a98ac0680fad765217302858e70fa4Paul Duffin      EntryTransformer<? super K, ? super V1, V2> transformer) {
720888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Maps.transformEntriesIgnoreNavigable(fromMap, transformer);
730888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
740888a09821a98ac0680fad765217302858e70fa4Paul Duffin
750888a09821a98ac0680fad765217302858e70fa4Paul Duffin  static <K, V> SortedMap<K, V> mapsAsMapSortedSet(SortedSet<K> set,
760888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Function<? super K, V> function) {
770888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Maps.asMapSortedIgnoreNavigable(set, function);
780888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
790888a09821a98ac0680fad765217302858e70fa4Paul Duffin
800888a09821a98ac0680fad765217302858e70fa4Paul Duffin  static <E> SortedSet<E> setsFilterSortedSet(SortedSet<E> set,
810888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Predicate<? super E> predicate) {
820888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Sets.filterSortedIgnoreNavigable(set, predicate);
830888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
840888a09821a98ac0680fad765217302858e70fa4Paul Duffin
850888a09821a98ac0680fad765217302858e70fa4Paul Duffin  static <K, V> SortedMap<K, V> mapsFilterSortedMap(SortedMap<K, V> map,
860888a09821a98ac0680fad765217302858e70fa4Paul Duffin      Predicate<? super Map.Entry<K, V>> predicate) {
870888a09821a98ac0680fad765217302858e70fa4Paul Duffin    return Maps.filterSortedIgnoreNavigable(map, predicate);
880888a09821a98ac0680fad765217302858e70fa4Paul Duffin  }
890888a09821a98ac0680fad765217302858e70fa4Paul Duffin
90090f9b4c879985bc747c214f82c62471e60c7742Jesse Wilson  private Platform() {}
91bfe2dd089341dcb4c1fb65a5b6b077ad0ebbf6dcDan Egnor}
92