美文网首页
commons-collections 之 LazyMap

commons-collections 之 LazyMap

作者: 躺在家里干活 | 来源:发表于2019-10-08 09:48 被阅读0次

作用:只有真正调用的时候才去加载

注:关注 public V get(final Object key) 方法

  • 内部实现:通过 Transformer<? super K, ? extends V> factory; 映射key到value,通过key查找的时候,如果有就直接返回value,如果没有就会调用Transformer去读取相关数据
    下一章会说,Transformer是如何实现获取数据的
package org.apache.commons.collections4.map;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Map;

import org.apache.commons.collections4.Factory;
import org.apache.commons.collections4.Transformer;
import org.apache.commons.collections4.functors.FactoryTransformer;


public class LazyMap<K, V> extends AbstractMapDecorator<K, V> implements Serializable {

    /** Serialization version */
    private static final long serialVersionUID = 7990956402564206740L;

    /** The factory to use to construct elements */
    protected final Transformer<? super K, ? extends V> factory;

    /**
     * Factory method to create a lazily instantiated map.
     *
     * @param <K>  the key type
     * @param <V>  the value type
     * @param map  the map to decorate, must not be null
     * @param factory  the factory to use, must not be null
     * @return a new lazy map
     * @throws NullPointerException if map or factory is null
     * @since 4.0
     */
    public static <K, V> LazyMap<K, V> lazyMap(final Map<K, V> map, final Factory< ? extends V> factory) {
        return new LazyMap<K,V>(map, factory);
    }

    /**
     * Factory method to create a lazily instantiated map.
     *
     * @param <K>  the key type
     * @param <V>  the value type
     * @param map  the map to decorate, must not be null
     * @param factory  the factory to use, must not be null
     * @return a new lazy map
     * @throws NullPointerException if map or factory is null
     * @since 4.0
     */
    public static <V, K> LazyMap<K, V> lazyMap(final Map<K, V> map, final Transformer<? super K, ? extends V> factory) {
        return new LazyMap<K,V>(map, factory);
    }

    //-----------------------------------------------------------------------
    /**
     * Constructor that wraps (not copies).
     *
     * @param map  the map to decorate, must not be null
     * @param factory  the factory to use, must not be null
     * @throws NullPointerException if map or factory is null
     */
    protected LazyMap(final Map<K,V> map, final Factory<? extends V> factory) {
        super(map);
        if (factory == null) {
            throw new NullPointerException("Factory must not be null");
        }
        this.factory = FactoryTransformer.factoryTransformer(factory);
    }

    /**
     * Constructor that wraps (not copies).
     *
     * @param map  the map to decorate, must not be null
     * @param factory  the factory to use, must not be null
     * @throws NullPointerException if map or factory is null
     */
    protected LazyMap(final Map<K,V> map, final Transformer<? super K, ? extends V> factory) {
        super(map);
        if (factory == null) {
            throw new NullPointerException("Factory must not be null");
        }
        this.factory = factory;
    }


    @Override
    public V get(final Object key) {
        // create value for key if key is not currently in the map
        if (map.containsKey(key) == false) {
            @SuppressWarnings("unchecked")
            final K castKey = (K) key;
            final V value = factory.transform(castKey);
            map.put(castKey, value);
            return value;
        }
        return map.get(key);
    }

    // no need to wrap keySet, entrySet or values as they are views of
    // existing map entries - you can't do a map-style get on them.
}

我的个人博客,有空来坐坐

相关文章

  • commons-collections 之 LazyMap

    作用:只有真正调用的时候才去加载 注:关注 public V get(final Object key) 方法 ...

  • commons-collections 之 BidiMap

    作用:双向MAP,参考AbstractDualBidiMap 内部通过两个Map实现 我的个人博客,有空来坐坐

  • commons-collections 之 MultiValue

    作用:可以为一个key,指定一个value集合 注:MultiMap具有同样的功能,但是4.1之后推荐使用Mult...

  • commons-collections 之 Transforme

    说明:Transformer是一个接口,它定义了将输入转化为输出的一个流程 之前记录的 commons-colle...

  • apache commons-collections 反序列漏洞

    在这里只记录下对jdk1.8可用的apache commons-collections中执行任意类任意执行方法链,...

  • commons-collections 常用方法

    写在前面的话 commons-collections 包诞生的原因 (和 guava 库一样) 是对 JDK 中的...

  • 十之

    博学之,审问之,慎思之,明辨之,笃行之。 励志之,健身之,涅槃之,弘毅之,自强之!

  • 读记|唐诗人:诗心煎红尘(二)

    愈之挫之 险之退之 借之济之 忠之犯之 勇之夺之 衰之立之 坚之韧之 载之言之 一代宗师 成之传之 字曰子厚 道解...

  • 《寄君归》

    思之念之 见之不忘 吾亦求之 求之不得 吾亦念之 兮之盼之 来之归之 欲予离之 得之兮之 心思念之 盼来归之 归之...

  • 飘零

    艾雪儿 难耐心中怦然之 抑之,控之,思之,忘之 能否任之,弃之,拥之,念之 山河为鉴 赴之,游之,悦之,相守之…

网友评论

      本文标题:commons-collections 之 LazyMap

      本文链接:https://www.haomeiwen.com/subject/mqbdyctx.html