Collections.unmodifiableMap()分析

来源:互联网 发布:java传感器数据 编辑:程序博客网 时间:2024/06/16 23:30
public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K,? extends V> m)返回指定映射的不可修改视图。此方法允许模块为用户提供对内部映射的“只读”访问。在返回的映射上执行的查询操作将“读完”指定的映射。试图修改返回的映射(不管是直接修改还是通过其 collection 视图进行修改)将导致抛出 UnsupportedOperationException。 
如果指定映射是可序列化的,则返回的映射也将是可序列化的。 


参数: 
m - 将为其返回一个不可修改视图的映射。 
返回: 
指定映射的不可修改视图。 

// 实现原是是包装了下map 不支持改变大小的操作  
        // 仅仅返回的Map不能put remove 操作,  

        // 但可以对里的对象进行操作


Java代码  收藏代码
  1.  private static class UnmodifiableMap  
  2.         implements Map, Serializable  
  3.     {  
  4.         static class UnmodifiableEntrySet extends UnmodifiableSet  
  5.         {  
  6.             private static class UnmodifiableEntry  
  7.                 implements Map.Entry  
  8.             {  
  9.   
  10.                 public Object getKey()  
  11.                 {  
  12.                     return e.getKey();  
  13.                 }  
  14.   
  15.                 public Object getValue()  
  16.                 {  
  17.                     return e.getValue();  
  18.                 }  
  19.   
  20.                 public Object setValue(Object obj)  
  21.                 {  
  22.                     throw new UnsupportedOperationException();  
  23.                 }  
  24.   
  25.                 public int hashCode()  
  26.                 {  
  27.                     return e.hashCode();  
  28.                 }  
  29.   
  30.                 public boolean equals(Object obj)  
  31.                 {  
  32.                     if(!(obj instanceof Map.Entry))  
  33.                     {  
  34.                         return false;  
  35.                     } else  
  36.                     {  
  37.                         Map.Entry entry = (Map.Entry)obj;  
  38.                         return Collections.eq(e.getKey(), entry.getKey()) && Collections.eq(e.getValue(), entry.getValue());  
  39.                     }  
  40.                 }  
  41.   
  42.                 public String toString()  
  43.                 {  
  44.                     return e.toString();  
  45.                 }  
  46.   
  47.                 private Map.Entry e;  
  48.   
  49.                 UnmodifiableEntry(Map.Entry entry)  
  50.                 {  
  51.                     e = entry;  
  52.                 }  
  53.             }  
  54.   
  55.   
  56.             public Iterator iterator()  
  57.             {  
  58.                 return new Iterator() {  
  59.   
  60.                     public boolean hasNext()  
  61.                     {  
  62.                         return i.hasNext();  
  63.                     }  
  64.   
  65.                     public Map.Entry next()  
  66.                     {  
  67.                         return new UnmodifiableEntry((Map.Entry)i.next());  
  68.                     }  
  69.   
  70.                     public void remove()  
  71.                     {  
  72.                         throw new UnsupportedOperationException();  
  73.                     }  
  74.   
  75.                     public volatile Object next()  
  76.                     {  
  77.                         return next();  
  78.                     }  
  79.   
  80.                     private final Iterator i;  
  81.                     final UnmodifiableEntrySet this$0;  
  82.   
  83.                       
  84.                     {  
  85.                         this$0 = UnmodifiableEntrySet.this;  
  86.                         super();  
  87.                         i = c.iterator();  
  88.                     }  
  89.                 }  
  90. ;  
  91.             }  
  92.   
  93.             public Object[] toArray()  
  94.             {  
  95.                 Object aobj[] = c.toArray();  
  96.                 for(int i = 0; i < aobj.length; i++)  
  97.                     aobj[i] = new UnmodifiableEntry((Map.Entry)aobj[i]);  
  98.   
  99.                 return aobj;  
  100.             }  
  101.   
  102.             public Object[] toArray(Object aobj[])  
  103.             {  
  104.                 Object aobj1[] = c.toArray(aobj.length != 0 ? Arrays.copyOf(aobj, 0) : aobj);  
  105.                 for(int i = 0; i < aobj1.length; i++)  
  106.                     aobj1[i] = new UnmodifiableEntry((Map.Entry)aobj1[i]);  
  107.   
  108.                 if(aobj1.length > aobj.length)  
  109.                     return (Object[])aobj1;  
  110.                 System.arraycopy(((Object) (aobj1)), 0, ((Object) (aobj)), 0, aobj1.length);  
  111.                 if(aobj.length > aobj1.length)  
  112.                     aobj[aobj1.length] = null;  
  113.                 return aobj;  
  114.             }  
  115.   
  116.             public boolean contains(Object obj)  
  117.             {  
  118.                 if(!(obj instanceof Map.Entry))  
  119.                     return false;  
  120.                 else  
  121.                     return c.contains(new UnmodifiableEntry((Map.Entry)obj));  
  122.             }  
  123.   
  124.             public boolean containsAll(Collection collection)  
  125.             {  
  126.                 for(Iterator iterator1 = collection.iterator(); iterator1.hasNext();)  
  127.                 {  
  128.                     Object obj = iterator1.next();  
  129.                     if(!contains(obj))  
  130.                         return false;  
  131.                 }  
  132.   
  133.                 return true;  
  134.             }  
  135.   
  136.             public boolean equals(Object obj)  
  137.             {  
  138.                 if(obj == this)  
  139.                     return true;  
  140.                 if(!(obj instanceof Set))  
  141.                     return false;  
  142.                 Set set = (Set)obj;  
  143.                 if(set.size() != c.size())  
  144.                     return false;  
  145.                 else  
  146.                     return containsAll(set);  
  147.             }  
  148.   
  149.             private static final long serialVersionUID = 7854390611657943733L;  
  150.   
  151.             UnmodifiableEntrySet(Set set)  
  152.             {  
  153.                 super(set);  
  154.             }  
  155.         }  
  156.   
  157.   
  158.         public int size()  
  159.         {  
  160.             return m.size();  
  161.         }  
  162.   
  163.         public boolean isEmpty()  
  164.         {  
  165.             return m.isEmpty();  
  166.         }  
  167.   
  168.         public boolean containsKey(Object obj)  
  169.         {  
  170.             return m.containsKey(obj);  
  171.         }  
  172.   
  173.         public boolean containsValue(Object obj)  
  174.         {  
  175.             return m.containsValue(obj);  
  176.         }  
  177.   
  178.         public Object get(Object obj)  
  179.         {  
  180.             return m.get(obj);  
  181.         }  
  182.   
  183.         public Object put(Object obj, Object obj1)  
  184.         {  
  185.             throw new UnsupportedOperationException();  
  186.         }  
  187.   
  188.         public Object remove(Object obj)  
  189.         {  
  190.             throw new UnsupportedOperationException();  
  191.         }  
  192.   
  193.         public void putAll(Map map)  
  194.         {  
  195.             throw new UnsupportedOperationException();  
  196.         }  
  197.   
  198.         public void clear()  
  199.         {  
  200.             throw new UnsupportedOperationException();  
  201.         }  
  202.   
  203.         public Set keySet()  
  204.         {  
  205.             if(keySet == null)  
  206.                 keySet = Collections.unmodifiableSet(m.keySet());  
  207.             return keySet;  
  208.         }  
  209.   
  210.         public Set entrySet()  
  211.         {  
  212.             if(entrySet == null)  
  213.                 entrySet = new UnmodifiableEntrySet(m.entrySet());  
  214.             return entrySet;  
  215.         }  
  216.   
  217.         public Collection values()  
  218.         {  
  219.             if(values == null)  
  220.                 values = Collections.unmodifiableCollection(m.values());  
  221.             return values;  
  222.         }  
  223.   
  224.         public boolean equals(Object obj)  
  225.         {  
  226.             return obj == this || m.equals(obj);  
  227.         }  
  228.   
  229.         public int hashCode()  
  230.         {  
  231.             return m.hashCode();  
  232.         }  
  233.   
  234.         public String toString()  
  235.         {  
  236.             return m.toString();  
  237.         }  
  238.   
  239.         private static final long serialVersionUID = -1034234728574286014L;  
  240.         private final Map m;  
  241.         private transient Set keySet;  
  242.         private transient Set entrySet;  
  243.         private transient Collection values;  
  244.   
  245.         UnmodifiableMap(Map map)  
  246.         {  
  247.             keySet = null;  
  248.             entrySet = null;  
  249.             values = null;  
  250.             if(map == null)  
  251.             {  
  252.                 throw new NullPointerException();  
  253.             } else  
  254.             {  
  255.                 m = map;  
  256.                 return;  
  257.             }  
  258.         }  
  259.     }