View Javadoc

1   /*
2    * File: ImmutableMap.java
3    * Created: 11-čen-06 8:22:56 
4    *
5    * Copyright 2006 Michal Burda.
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20   */
21  package net.sf.webmancer.util.immutable;
22  
23  import java.util.Collection;
24  import java.util.HashSet;
25  import java.util.Map;
26  import java.util.Set;
27  
28  /**
29   * @author Michal Burda
30   * @param <K>
31   * @param <V>
32   */
33  public final class ImmutableMap<K, V> implements Map<K, V> {
34      /**
35       * 
36       */
37      private Map<? extends K, ? extends V> map;
38  
39      /**
40       * @param map
41       */
42      public ImmutableMap(final Map<? extends K, ? extends V> map) {
43          super();
44          this.map = map;
45      }
46  
47      /**
48       * @see java.util.Map#containsKey(java.lang.Object)
49       */
50      public boolean containsKey(final Object key) {
51          return this.map.containsKey(key);
52      }
53  
54      /**
55       * @see java.util.Map#containsValue(java.lang.Object)
56       */
57      public boolean containsValue(final Object value) {
58          return this.map.containsValue(value);
59      }
60  
61      /**
62       * @see java.util.Map#entrySet()
63       */
64      @SuppressWarnings("unchecked")
65      public Set<Map.Entry<K, V>> entrySet() {
66          Set set = this.map.entrySet();
67          Set<Map.Entry<K, V>> result = new HashSet<Map.Entry<K, V>>(set.size());
68          for (Object object : set) {
69              Map.Entry<K, V> entry = new ImmutableMapEntry((Map.Entry<K, V>) object);
70              result.add(entry);
71          }
72          return result;
73      }
74  
75      /**
76       * @see java.util.Map#equals(java.lang.Object)
77       */
78      @Override
79      public boolean equals(final Object o) {
80          return this.map.equals(o);
81      }
82  
83      /**
84       * @see java.util.Map#get(java.lang.Object)
85       */
86      public V get(final Object key) {
87          return this.map.get(key);
88      }
89  
90      /**
91       * @see java.util.Map#hashCode()
92       */
93      @Override
94      public int hashCode() {
95          return this.map.hashCode();
96      }
97  
98      /**
99       * @see java.util.Map#isEmpty()
100      */
101     public boolean isEmpty() {
102         return this.map.isEmpty();
103     }
104 
105     /**
106      * @see java.util.Map#keySet()
107      */
108     public Set<K> keySet() {
109         return new ImmutableSet<K>(this.map.keySet());
110     }
111 
112     /**
113      * @see java.util.Map#size()
114      */
115     public int size() {
116         return this.map.size();
117     }
118 
119     /**
120      * @see java.util.Map#values()
121      */
122     public Collection<V> values() {
123         return new ImmutableCollection<V>(this.map.values());
124     }
125 
126     /**
127      * @see java.util.Map#put(K, V)
128      */
129     public V put(final K key, final V value) {
130         throw new UnsupportedOperationException("ImmutableMap.put");
131     }
132 
133     /**
134      * @see java.util.Map#remove(java.lang.Object)
135      */
136     public V remove(final Object key) {
137         throw new UnsupportedOperationException("ImmutableMap.remove");
138     }
139 
140     /**
141      * @see java.util.Map#putAll(java.util.Map)
142      */
143     public void putAll(final Map<? extends K, ? extends V> t) {
144         throw new UnsupportedOperationException("ImmutableMap.putAll");
145     }
146 
147     /**
148      * @see java.util.Map#clear()
149      */
150     public void clear() {
151         throw new UnsupportedOperationException("ImmutableMap.clear");
152     }
153 
154 }