赞
踩
- 内部实现:通过 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. }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。