Google Guava Collections 中 MapMaker

来源:互联网 发布:qt tcp客户端发送数据 编辑:程序博客网 时间:2024/06/17 23:49

MapMaker: final类,继承了 GenericMapMaker<Object, Object>,是 ConcurrentMap的 builder。它的构造方法是公开的,可以直接通过 new获取一个实例。


成员方法:

          initialCapacity(int initialCapacity):如果 this的初始容量已经设置过了,则抛出异常,否则将其设为参数中的 initialCapacity,并返回 this。


          maximumSize(int size):如果 this的 maximumSize已经设置过了,则抛出异常,否则将其设为 size,并返回 this。


          concurrencyLevel(int concurrencyLevel):如果 this的 concurrencyLevel 已经设置过了,则抛出异常,否则将其设为参数中的 concurrencyLevel,并返回 this。


          weakKeys():如果 this的 keyStrength 已经设置过了,则抛出异常,否则将其设为 Strength.WEAK,将useCustomMap设为 true,并返回 this。


          softKeys():如果 this的 keyStrength 已经设置过了,则抛出异常,否则将其设为 Strength.SOFT,将useCustomMap设为 true,并返回 this。


          weakValues():如果 this的 valueStrength 已经设置过了,则抛出异常,否则将其设为 Strength.WEAK,将useCustomMap设为 true,并返回 this。


          softValues():如果 this的 valueStrength 已经设置过了,则抛出异常,否则将其设为 Strength.SOFT,将useCustomMap设为 true,并返回 this。


          expireAfterWrite(long duration, TimeUnit unit):如果 this的 expireAfterWriteNanos 已经设置过了,则抛出异常,否则将其设为 unit.toNanos(duration),将 useCustomMap设为 true,并返回 this。


          expireAfterAccess(long duration, TimeUnit unit):如果 this的 expireAfterAccessNanos 已经设置过了,则抛出异常,否则将其设为 unit.toNanos(duration),将 useCustomMap设为 true,并返回 this。


          evictionListener(MapEvictionListener<K, V> listener):如果 this的 evictionLisener已经设置过了,则抛出异常,否则将其设为 listener,将 useCustomMap设为 true,并返回 this。


          makeMap():如果 useCustomMap为 false,返回一个 ConcurrentHashMap,否则判断 useNullMap,如果为 true,返回一个 NullConcurrentMap,否则返回一个 CustomConcurrentHashMap。 NullConcurrentMap是内部类,不包含任何键值对。


          makeComputingMap(Funcion<? super K, ? extends V> computingFunction):返回一个ConcurrentMap。根据 this,创建一个 NullComputingConcurrentMap或 ComputingConcurrentMap。NullComputingConcurrentMap是继承 NullConcurrentMap实现 Cache接口 (Cache继承 Function接口,定义了一个方法: asMap():ConcurrentMap)的内部类,它的 apply(K key)方法返回 computingFunction计算 key的结果,asMap()方法返回 this。


0 0