001/*
002 * Copyright (C) 2010 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS-IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.collect;
018
019import com.google.common.annotations.Beta;
020import com.google.common.annotations.GwtCompatible;
021import com.google.common.annotations.GwtIncompatible;
022import com.google.common.base.Equivalence;
023import com.google.common.base.Function;
024import com.google.common.base.Objects;
025import com.google.common.collect.MapMaker.RemovalListener;
026import com.google.common.collect.MapMaker.RemovalNotification;
027
028import java.util.concurrent.ConcurrentMap;
029import java.util.concurrent.TimeUnit;
030
031/**
032 * A class exactly like {@link MapMaker}, except restricted in the types of maps it can build.
033 * For the most part, you should probably just ignore the existence of this class.
034 *
035 * @param <K0> the base type for all key types of maps built by this map maker
036 * @param <V0> the base type for all value types of maps built by this map maker
037 * @author Kevin Bourrillion
038 * @since 7.0
039 */
040@Beta
041@GwtCompatible(emulated = true)
042public abstract class GenericMapMaker<K0, V0> {
043  @GwtIncompatible("To be supported")
044  enum NullListener implements RemovalListener<Object, Object> {
045    INSTANCE;
046
047    @Override
048    public void onRemoval(RemovalNotification<Object, Object> notification) {}
049  }
050
051  // Set by MapMaker, but sits in this class to preserve the type relationship
052  @GwtIncompatible("To be supported")
053  RemovalListener<K0, V0> removalListener;
054
055  // No subclasses but our own
056  GenericMapMaker() {}
057
058  /**
059   * See {@link MapMaker#keyEquivalence}.
060   */
061  @GwtIncompatible("To be supported")
062  abstract GenericMapMaker<K0, V0> keyEquivalence(Equivalence<Object> equivalence);
063
064  /**
065   * See {@link MapMaker#initialCapacity}.
066   */
067  public abstract GenericMapMaker<K0, V0> initialCapacity(int initialCapacity);
068
069  /**
070   * See {@link MapMaker#maximumSize}.
071   */
072  abstract GenericMapMaker<K0, V0> maximumSize(int maximumSize);
073
074  /**
075   * See {@link MapMaker#concurrencyLevel}.
076   */
077  public abstract GenericMapMaker<K0, V0> concurrencyLevel(int concurrencyLevel);
078
079  /**
080   * See {@link MapMaker#weakKeys}.
081   */
082  @GwtIncompatible("java.lang.ref.WeakReference")
083  public abstract GenericMapMaker<K0, V0> weakKeys();
084
085  /**
086   * See {@link MapMaker#weakValues}.
087   */
088  @GwtIncompatible("java.lang.ref.WeakReference")
089  public abstract GenericMapMaker<K0, V0> weakValues();
090
091  /**
092   * See {@link MapMaker#softValues}.
093   */
094  @GwtIncompatible("java.lang.ref.SoftReference")
095  public abstract GenericMapMaker<K0, V0> softValues();
096
097  /**
098   * See {@link MapMaker#expireAfterWrite}.
099   */
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}