首页

基于datanucleus的MultiMap自定义HashMap源码实现分享说明

标签:datanucleus-core,MultiMap,自定义HashMap     发布时间:2018-02-28   

一、前言

关于datanucleus-core源码包中通过org.datanucleus.util.MultiMap来基础java.util.HashMap实现多集合应用场景的Map,从而实现支持特性Map功能。

二、源码说明

package org.datanucleus.util;@b@@b@import java.util.AbstractCollection;@b@import java.util.ArrayList;@b@import java.util.Collection;@b@import java.util.HashMap;@b@import java.util.Iterator;@b@import java.util.List;@b@import java.util.Map.Entry;@b@import java.util.NoSuchElementException;@b@import java.util.Set;@b@@b@public class MultiMap extends HashMap@b@{@b@  private transient Collection values = null;@b@@b@  public MultiMap()@b@  {@b@  }@b@@b@  public MultiMap(int initialCapacity)@b@  {@b@    super(initialCapacity);@b@  }@b@@b@  public MultiMap(int initialCapacity, float loadFactor)@b@  {@b@    super(initialCapacity, loadFactor);@b@  }@b@@b@  public MultiMap(MultiMap map)@b@  {@b@    if (map != null)@b@    {@b@      Iterator it = map.entrySet().iterator();@b@      while (it.hasNext())@b@      {@b@        Map.Entry entry = (Map.Entry)it.next();@b@        super.put(entry.getKey(), new ArrayList((List)entry.getValue()));@b@      }@b@    }@b@  }@b@@b@  public boolean containsValue(Object value)@b@  {@b@    Set pairs = super.entrySet();@b@@b@    if (pairs == null)@b@    {@b@      return false;@b@    }@b@    Iterator pairsIterator = pairs.iterator();@b@    while (pairsIterator.hasNext())@b@    {@b@      Map.Entry keyValuePair = (Map.Entry)pairsIterator.next();@b@      Collection coll = (Collection)keyValuePair.getValue();@b@      if (coll.contains(value))@b@      {@b@        return true;@b@      }@b@    }@b@    return false;@b@  }@b@@b@  public Object put(Object key, Object value)@b@  {@b@    Collection c = (Collection)super.get(key);@b@    if (c == null)@b@    {@b@      c = createCollection(null);@b@      super.put(key, c);@b@    }@b@    boolean results = c.add(value);@b@@b@    return ((results) ? value : null);@b@  }@b@@b@  public Object remove(Object key, Object item)@b@  {@b@    Collection valuesForKey = (Collection)super.get(key);@b@    if (valuesForKey == null)@b@    {@b@      return null;@b@    }@b@    valuesForKey.remove(item);@b@@b@    if (valuesForKey.isEmpty())@b@    {@b@      remove(key);@b@    }@b@    return item;@b@  }@b@@b@  public void clear()@b@  {@b@    Set pairs = super.entrySet();@b@    Iterator pairsIterator = pairs.iterator();@b@    while (pairsIterator.hasNext())@b@    {@b@      Map.Entry keyValuePair = (Map.Entry)pairsIterator.next();@b@      Collection coll = (Collection)keyValuePair.getValue();@b@      coll.clear();@b@    }@b@    super.clear();@b@  }@b@@b@  public Collection values()@b@  {@b@    Collection vs = this.values;@b@    return (this.values = new ValueElement(this, null));@b@  }@b@@b@  public Object clone()@b@  {@b@    MultiMap obj = (MultiMap)super.clone();@b@@b@    for (Iterator it = entrySet().iterator(); it.hasNext(); )@b@    {@b@      Map.Entry entry = (Map.Entry)it.next();@b@      Collection coll = (Collection)entry.getValue();@b@      Collection newColl = createCollection(coll);@b@      entry.setValue(newColl);@b@    }@b@    return obj;@b@  }@b@@b@  protected Collection createCollection(Collection c)@b@  {@b@    if (c == null)@b@    {@b@      return new ArrayList();@b@    }@b@@b@    return new ArrayList(c);@b@  }@b@@b@  private class ValueElementIter@b@    implements Iterator@b@  {@b@    private Iterator backing;@b@    private Iterator temp;@b@@b@    private ValueElementIter()@b@    {@b@      this.backing = MultiMap.access$201(paramMultiMap).iterator();@b@    }@b@@b@    private boolean searchNextIterator()@b@    {@b@      while ((this.temp == null) || (!(this.temp.hasNext())))@b@      {@b@        if (!(this.backing.hasNext()))@b@        {@b@          return false;@b@        }@b@        this.temp = ((Collection)this.backing.next()).iterator();@b@      }@b@      return true;@b@    }@b@@b@    public boolean hasNext()@b@    {@b@      return searchNextIterator();@b@    }@b@@b@    public Object next()@b@    {@b@      if (!(searchNextIterator()))@b@      {@b@        throw new NoSuchElementException();@b@      }@b@      return this.temp.next();@b@    }@b@@b@    public void remove()@b@    {@b@      if (this.temp == null)@b@      {@b@        throw new IllegalStateException();@b@      }@b@      this.temp.remove();@b@    }@b@  }@b@@b@  private class ValueElement extends AbstractCollection@b@  {@b@    public Iterator iterator()@b@    {@b@      return new MultiMap.ValueElementIter(this.this$0, null);@b@    }@b@@b@    public int size()@b@    {@b@      int i = 0;@b@      Iterator iter = iterator();@b@      while (iter.hasNext())@b@      {@b@        iter.next();@b@        ++i;@b@      }@b@      return i;@b@    }@b@@b@    public void clear()@b@    {@b@      this.this$0.clear();@b@    }@b@  }@b@}