GenericMapMaker.java revision 3c77433663281544363151bf284b0240dfd22a42
1/* 2 * Copyright (C) 2010 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.Beta; 20import com.google.common.annotations.GwtCompatible; 21import com.google.common.annotations.GwtIncompatible; 22import com.google.common.base.Equivalence; 23import com.google.common.base.Function; 24import com.google.common.base.Objects; 25import com.google.common.collect.MapMaker.RemovalListener; 26import com.google.common.collect.MapMaker.RemovalNotification; 27 28import java.util.concurrent.ConcurrentMap; 29import java.util.concurrent.TimeUnit; 30 31/** 32 * A class exactly like {@link MapMaker}, except restricted in the types of maps it can build. 33 * For the most part, you should probably just ignore the existence of this class. 34 * 35 * @paramthe base type for all key types of maps built by this map maker 36 * @param the base type for all value types of maps built by this map maker 37 * @author Kevin Bourrillion 38 * @since 7.0 39 */ 40@Beta 41@GwtCompatible(emulated = true) 42public abstract class GenericMapMaker<K0, V0> { 43 @GwtIncompatible("To be supported") 44 enum NullListener implements RemovalListener<Object, Object> { 45 INSTANCE; 46 47 @Override 48 public void onRemoval(RemovalNotification<Object, Object> notification) {} 49 } 50 51 // Set by MapMaker, but sits in this class to preserve the type relationship 52 @GwtIncompatible("To be supported") 53 RemovalListener<K0, V0> removalListener; 54 55 // No subclasses but our own 56 GenericMapMaker() {} 57 58 /** 59 * See {@link MapMaker#keyEquivalence}. 60 */ 61 @GwtIncompatible("To be supported") 62 abstract GenericMapMaker<K0, V0> keyEquivalence(Equivalence<Object> equivalence); 63 64 /** 65 * See {@link MapMaker#initialCapacity}. 66 */ 67 public abstract GenericMapMaker<K0, V0> initialCapacity(int initialCapacity); 68 69 /** 70 * See {@link MapMaker#maximumSize}. 71 */ 72 abstract GenericMapMaker<K0, V0> maximumSize(int maximumSize); 73 74 /** 75 * See {@link MapMaker#concurrencyLevel}. 76 */ 77 public abstract GenericMapMaker<K0, V0> concurrencyLevel(int concurrencyLevel); 78 79 /** 80 * See {@link MapMaker#weakKeys}. 81 */ 82 @GwtIncompatible("java.lang.ref.WeakReference") 83 public abstract GenericMapMaker<K0, V0> weakKeys(); 84 85 /** 86 * See {@link MapMaker#weakValues}. 87 */ 88 @GwtIncompatible("java.lang.ref.WeakReference") 89 public abstract GenericMapMaker<K0, V0> weakValues(); 90 91 /** 92 * See {@link MapMaker#softValues}. 93 */ 94 @GwtIncompatible("java.lang.ref.SoftReference") 95 public abstract GenericMapMaker<K0, V0> softValues(); 96 97 /** 98 * See {@link MapMaker#expireAfterWrite}. 99 */ 100 abstract GenericMapMaker<K0, V0> expireAfterWrite(long duration, TimeUnit unit); 101 102 /** 103 * See {@link MapMaker#expireAfterAccess}. 104 */ 105 @GwtIncompatible("To be supported") 106 abstract GenericMapMaker<K0, V0> expireAfterAccess(long duration, TimeUnit unit); 107 108 /* 109 * Note that MapMaker's removalListener() is not here, because once you're interacting with a 110 * GenericMapMaker you've already called that, and shouldn't be calling it again. 111 */ 112 113 @SuppressWarnings("unchecked") // safe covariant cast 114 @GwtIncompatible("To be supported") 115 <K extends K0, V extends V0> RemovalListener<K, V> getRemovalListener() { 116 return (RemovalListener<K, V>) Objects.firstNonNull(removalListener, NullListener.INSTANCE); 117 } 118 119 /** 120 * See {@link MapMaker#makeMap}. 121 */ 122 public abstract <K extends K0, V extends V0> ConcurrentMap<K, V> makeMap(); 123 124 /** 125 * See {@link MapMaker#makeCustomMap}. 126 */ 127 @GwtIncompatible("MapMakerInternalMap") 128 abstract <K, V> MapMakerInternalMap<K, V> makeCustomMap(); 129 130 /** 131 * See {@link MapMaker#makeComputingMap}. 132 */ 133 @Deprecated 134 public abstract <K extends K0, V extends V0> ConcurrentMap<K, V> makeComputingMap( 135 Function<? super K, ? extends V> computingFunction); 136} 137