Home | History | Annotate | Download | only in collect
      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 com.google.common.base.Function;
     21 import com.google.common.base.Predicate;
     22 import com.google.common.collect.Maps.EntryTransformer;
     23 
     24 import java.lang.reflect.Array;
     25 import java.util.Collections;
     26 import java.util.Map;
     27 import java.util.NavigableMap;
     28 import java.util.NavigableSet;
     29 import java.util.Set;
     30 import java.util.SortedMap;
     31 import java.util.SortedSet;
     32 
     33 /**
     34  * Methods factored out so that they can be emulated differently in GWT.
     35  *
     36  * @author Hayward Chan
     37  */
     38 @GwtCompatible(emulated = true)
     39 final class Platform {
     40   /**
     41    * Returns a new array of the given length with the same type as a reference
     42    * array.
     43    *
     44    * @param reference any array of the desired type
     45    * @param length the length of the new array
     46    */
     47   static <T> T[] newArray(T[] reference, int length) {
     48     Class<?> type = reference.getClass().getComponentType();
     49 
     50     // the cast is safe because
     51     // result.getClass() == reference.getClass().getComponentType()
     52     @SuppressWarnings("unchecked")
     53     T[] result = (T[]) Array.newInstance(type, length);
     54     return result;
     55   }
     56 
     57   static <E> Set<E> newSetFromMap(Map<E, Boolean> map) {
     58     return Collections.newSetFromMap(map);
     59   }
     60 
     61   /**
     62    * Configures the given map maker to use weak keys, if possible; does nothing
     63    * otherwise (i.e., in GWT). This is sometimes acceptable, when only
     64    * server-side code could generate enough volume that reclamation becomes
     65    * important.
     66    */
     67   static MapMaker tryWeakKeys(MapMaker mapMaker) {
     68     return mapMaker.weakKeys();
     69   }
     70 
     71   static <K, V1, V2> SortedMap<K, V2> mapsTransformEntriesSortedMap(
     72       SortedMap<K, V1> fromMap,
     73       EntryTransformer<? super K, ? super V1, V2> transformer) {
     74     return (fromMap instanceof NavigableMap)
     75         ? Maps.transformEntries((NavigableMap<K, V1>) fromMap, transformer)
     76         : Maps.transformEntriesIgnoreNavigable(fromMap, transformer);
     77   }
     78 
     79   static <K, V> SortedMap<K, V> mapsAsMapSortedSet(SortedSet<K> set,
     80       Function<? super K, V> function) {
     81     return (set instanceof NavigableSet)
     82         ? Maps.asMap((NavigableSet<K>) set, function)
     83         : Maps.asMapSortedIgnoreNavigable(set, function);
     84   }
     85 
     86   static <E> SortedSet<E> setsFilterSortedSet(SortedSet<E> set,
     87       Predicate<? super E> predicate) {
     88     return (set instanceof NavigableSet)
     89         ? Sets.filter((NavigableSet<E>) set, predicate)
     90         : Sets.filterSortedIgnoreNavigable(set, predicate);
     91   }
     92 
     93   static <K, V> SortedMap<K, V> mapsFilterSortedMap(SortedMap<K, V> map,
     94       Predicate<? super Map.Entry<K, V>> predicate) {
     95     return (map instanceof NavigableMap)
     96         ? Maps.filterEntries((NavigableMap<K, V>) map, predicate)
     97         : Maps.filterSortedIgnoreNavigable(map, predicate);
     98   }
     99 
    100   private Platform() {}
    101 }
    102