📄 maputils.java
字号:
return iterator.hasNext();
}
public Object next() {
Map.Entry entry = (Map.Entry)iterator.next();
return new PredicatedMapEntry(entry, predicate);
}
public void remove() {
iterator.remove();
}
};
}
}
static class PredicatedMapEntry
implements Map.Entry {
private final Map.Entry entry;
private final Predicate predicate;
public PredicatedMapEntry(Map.Entry entry, Predicate p) {
if (entry == null) {
throw new IllegalArgumentException("Map.Entry must not be null");
}
if (p == null) {
throw new IllegalArgumentException("Predicate must not be null");
}
this.entry = entry;
this.predicate = p;
}
public boolean equals(Object o) {
return entry.equals(o);
}
public int hashCode() {
return entry.hashCode();
}
public String toString() {
return entry.toString();
}
public Object getKey() {
return entry.getKey();
}
public Object getValue() {
return entry.getValue();
}
public Object setValue(Object o) {
if (!predicate.evaluate(o)) {
throw new IllegalArgumentException("Cannot set value - Predicate rejected it");
}
return entry.setValue(o);
}
}
static class FixedSizeMap
extends ProxyMap {
public FixedSizeMap(Map map) {
super(map);
if (map == null) {
throw new IllegalArgumentException("Map must not be null");
}
}
public Object put(Object key, Object value) {
if (!map.containsKey(key)) {
throw new IllegalArgumentException("Cannot put new key/value pair - List is fixed size");
}
return map.put(key, value);
}
public void putAll(Map m) {
for (Iterator iter = m.keySet().iterator(); iter.hasNext(); ) {
if (!map.containsKey(iter.next())) {
throw new IllegalArgumentException("Cannot put new key/value pair - List is fixed size");
}
}
map.putAll(m);
}
}
static class LazyMap
extends ProxyMap {
protected final Factory factory;
public LazyMap(Map map, Factory factory) {
super(map);
if (map == null) {
throw new IllegalArgumentException("Map must not be null");
}
if (factory == null) {
throw new IllegalArgumentException("Factory must not be null");
}
this.factory = factory;
}
public Object get(Object key) {
if (!map.containsKey(key)) {
Object value = factory.create();
map.put(key, value);
return value;
}
return map.get(key);
}
}
static class PredicatedSortedMap
extends PredicatedMap
implements SortedMap {
public PredicatedSortedMap(SortedMap map, Predicate k, Predicate v) {
super(map, k, v);
}
public Object firstKey() {
return getSortedMap().firstKey();
}
public Object lastKey() {
return getSortedMap().lastKey();
}
public Comparator comparator() {
return getSortedMap().comparator();
}
public SortedMap subMap(Object o1, Object o2) {
SortedMap sub = getSortedMap().subMap(o1, o2);
return new PredicatedSortedMap(sub, keyPredicate, valuePredicate);
}
public SortedMap headMap(Object o1) {
SortedMap sub = getSortedMap().headMap(o1);
return new PredicatedSortedMap(sub, keyPredicate, valuePredicate);
}
public SortedMap tailMap(Object o1) {
SortedMap sub = getSortedMap().tailMap(o1);
return new PredicatedSortedMap(sub, keyPredicate, valuePredicate);
}
private SortedMap getSortedMap() {
return (SortedMap)map;
}
}
static class FixedSizeSortedMap
extends FixedSizeMap
implements SortedMap {
public FixedSizeSortedMap(SortedMap m) {
super(m);
}
public Object firstKey() {
return getSortedMap().firstKey();
}
public Object lastKey() {
return getSortedMap().lastKey();
}
public Comparator comparator() {
return getSortedMap().comparator();
}
public SortedMap subMap(Object o1, Object o2) {
return new FixedSizeSortedMap(getSortedMap().subMap(o1, o2));
}
public SortedMap headMap(Object o1) {
return new FixedSizeSortedMap(getSortedMap().headMap(o1));
}
public SortedMap tailMap(Object o1) {
return new FixedSizeSortedMap(getSortedMap().tailMap(o1));
}
private SortedMap getSortedMap() {
return (SortedMap)map;
}
}
static class LazySortedMap
extends LazyMap
implements SortedMap {
public LazySortedMap(SortedMap m, Factory factory) {
super(m, factory);
}
public Object firstKey() {
return getSortedMap().firstKey();
}
public Object lastKey() {
return getSortedMap().lastKey();
}
public Comparator comparator() {
return getSortedMap().comparator();
}
public SortedMap subMap(Object o1, Object o2) {
return new LazySortedMap(getSortedMap().subMap(o1, o2), factory);
}
public SortedMap headMap(Object o1) {
return new LazySortedMap(getSortedMap().headMap(o1), factory);
}
public SortedMap tailMap(Object o1) {
return new LazySortedMap(getSortedMap().tailMap(o1), factory);
}
private SortedMap getSortedMap() {
return (SortedMap)map;
}
}
/**
* Returns a predicated map backed by the given map. Only keys and
* values that pass the given predicates can be added to the map.
* It is important not to use the original map after invoking this
* method, as it is a backdoor for adding unvalidated objects.
*
* @param map the map to predicate, must not be null
* @param keyPred the predicate for keys, must not be null
* @param valuePred the predicate for values, must not be null
* @return a predicated map backed by the given map
* @throws IllegalArgumentException if the Map or Predicates are null
*/
public static Map predicatedMap(Map map, Predicate keyPred, Predicate valuePred) {
return new PredicatedMap(map, keyPred, valuePred);
}
/**
* Returns a fixed-sized map backed by the given map.
* Elements may not be added or removed from the returned map, but
* existing elements can be changed (for instance, via the
* {@link Map#put(Object,Object)} method).
*
* @param map the map whose size to fix, must not be null
* @return a fixed-size map backed by that map
* @throws IllegalArgumentException if the Map is null
*/
public static Map fixedSizeMap(Map map) {
return new FixedSizeMap(map);
}
/**
* Returns a "lazy" map whose values will be created on demand.<P>
* <p>
* When the key passed to the returned map's {@link Map#get(Object)}
* method is not present in the map, then the factory will be used
* to create a new object and that object will become the value
* associated with that key.
* <p>
* For instance:
*
* <pre>
* Factory factory = new Factory() {
* public Object create() {
* return new Date();
* }
* }
* Map lazy = MapUtils.lazyMap(new HashMap(), factory);
* Object obj = lazy.get("test");
* </pre>
*
* After the above code is executed, <code>obj</code> will contain
* a new <code>Date</code> instance. Furthermore, that <code>Date</code>
* instance is the value for the <code>test</code> key.
*
* @param map the map to make lazy, must not be null
* @param factory the factory for creating new objects, must not be null
* @return a lazy map backed by the given map
* @throws IllegalArgumentException if the Map or Factory is null
*/
public static Map lazyMap(Map map, Factory factory) {
return new LazyMap(map, factory);
}
/**
* Returns a predicated sorted map backed by the given map. Only keys and
* values that pass the given predicates can be added to the map.
* It is important not to use the original map after invoking this
* method, as it is a backdoor for adding unvalidated objects.
*
* @param map the map to predicate, must not be null
* @param keyPred the predicate for keys, must not be null
* @param valuePred the predicate for values, must not be null
* @return a predicated map backed by the given map
* @throws IllegalArgumentException if the SortedMap or Predicates are null
*/
public static SortedMap predicatedSortedMap(SortedMap map, Predicate keyPred, Predicate valuePred) {
return new PredicatedSortedMap(map, keyPred, valuePred);
}
/**
* Returns a fixed-sized sorted map backed by the given sorted map.
* Elements may not be added or removed from the returned map, but
* existing elements can be changed (for instance, via the
* {@link Map#put(Object,Object)} method).
*
* @param map the map whose size to fix, must not be null
* @return a fixed-size map backed by that map
* @throws IllegalArgumentException if the SortedMap is null
*/
public static SortedMap fixedSizeSortedMap(SortedMap map) {
return new FixedSizeSortedMap(map);
}
/**
* Returns a "lazy" sorted map whose values will be created on demand.
* <p>
* When the key passed to the returned map's {@link Map#get(Object)}
* method is not present in the map, then the factory will be used
* to create a new object and that object will become the value
* associated with that key.
* <p>
* For instance:
*
* <pre>
* Factory factory = new Factory() {
* public Object create() {
* return new Date();
* }
* }
* SortedMap lazy = MapUtils.lazySortedMap(new TreeMap(), factory);
* Object obj = lazy.get("test");
* </pre>
*
* After the above code is executed, <code>obj</code> will contain
* a new <code>Date</code> instance. Furthermore, that <code>Date</code>
* instance is the value for the <code>test</code> key.
*
* @param map the map to make lazy, must not be null
* @param factory the factory for creating new objects, must not be null
* @return a lazy map backed by the given map
* @throws IllegalArgumentException if the SortedMap or Factory is null
*/
public static SortedMap lazySortedMap(SortedMap map, Factory factory) {
return new LazySortedMap(map, factory);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -