Source Code Cross Referenced for AbstractMap.java in  » 6.0-JDK-Core » Collections-Jar-Zip-Logging-regex » java » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » Collections Jar Zip Logging regex » java.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025
026        package java.util;
027
028        import java.util.Map.Entry;
029
030        /**
031         * This class provides a skeletal implementation of the <tt>Map</tt>
032         * interface, to minimize the effort required to implement this interface.
033         *
034         * <p>To implement an unmodifiable map, the programmer needs only to extend this
035         * class and provide an implementation for the <tt>entrySet</tt> method, which
036         * returns a set-view of the map's mappings.  Typically, the returned set
037         * will, in turn, be implemented atop <tt>AbstractSet</tt>.  This set should
038         * not support the <tt>add</tt> or <tt>remove</tt> methods, and its iterator
039         * should not support the <tt>remove</tt> method.
040         *
041         * <p>To implement a modifiable map, the programmer must additionally override
042         * this class's <tt>put</tt> method (which otherwise throws an
043         * <tt>UnsupportedOperationException</tt>), and the iterator returned by
044         * <tt>entrySet().iterator()</tt> must additionally implement its
045         * <tt>remove</tt> method.
046         *
047         * <p>The programmer should generally provide a void (no argument) and map
048         * constructor, as per the recommendation in the <tt>Map</tt> interface
049         * specification.
050         *
051         * <p>The documentation for each non-abstract method in this class describes its
052         * implementation in detail.  Each of these methods may be overridden if the
053         * map being implemented admits a more efficient implementation.
054         *
055         * <p>This class is a member of the
056         * <a href="{@docRoot}/../technotes/guides/collections/index.html">
057         * Java Collections Framework</a>.
058         *
059         * @param <K> the type of keys maintained by this map
060         * @param <V> the type of mapped values
061         *
062         * @author  Josh Bloch
063         * @author  Neal Gafter
064         * @version 1.57, 06/12/07
065         * @see Map
066         * @see Collection
067         * @since 1.2
068         */
069
070        public abstract class AbstractMap<K, V> implements  Map<K, V> {
071            /**
072             * Sole constructor.  (For invocation by subclass constructors, typically
073             * implicit.)
074             */
075            protected AbstractMap() {
076            }
077
078            // Query Operations
079
080            /**
081             * {@inheritDoc}
082             *
083             * <p>This implementation returns <tt>entrySet().size()</tt>.
084             */
085            public int size() {
086                return entrySet().size();
087            }
088
089            /**
090             * {@inheritDoc}
091             *
092             * <p>This implementation returns <tt>size() == 0</tt>.
093             */
094            public boolean isEmpty() {
095                return size() == 0;
096            }
097
098            /**
099             * {@inheritDoc}
100             *
101             * <p>This implementation iterates over <tt>entrySet()</tt> searching
102             * for an entry with the specified value.  If such an entry is found,
103             * <tt>true</tt> is returned.  If the iteration terminates without
104             * finding such an entry, <tt>false</tt> is returned.  Note that this
105             * implementation requires linear time in the size of the map.
106             *
107             * @throws ClassCastException   {@inheritDoc}
108             * @throws NullPointerException {@inheritDoc}
109             */
110            public boolean containsValue(Object value) {
111                Iterator<Entry<K, V>> i = entrySet().iterator();
112                if (value == null) {
113                    while (i.hasNext()) {
114                        Entry<K, V> e = i.next();
115                        if (e.getValue() == null)
116                            return true;
117                    }
118                } else {
119                    while (i.hasNext()) {
120                        Entry<K, V> e = i.next();
121                        if (value.equals(e.getValue()))
122                            return true;
123                    }
124                }
125                return false;
126            }
127
128            /**
129             * {@inheritDoc}
130             *
131             * <p>This implementation iterates over <tt>entrySet()</tt> searching
132             * for an entry with the specified key.  If such an entry is found,
133             * <tt>true</tt> is returned.  If the iteration terminates without
134             * finding such an entry, <tt>false</tt> is returned.  Note that this
135             * implementation requires linear time in the size of the map; many
136             * implementations will override this method.
137             *
138             * @throws ClassCastException   {@inheritDoc}
139             * @throws NullPointerException {@inheritDoc}
140             */
141            public boolean containsKey(Object key) {
142                Iterator<Map.Entry<K, V>> i = entrySet().iterator();
143                if (key == null) {
144                    while (i.hasNext()) {
145                        Entry<K, V> e = i.next();
146                        if (e.getKey() == null)
147                            return true;
148                    }
149                } else {
150                    while (i.hasNext()) {
151                        Entry<K, V> e = i.next();
152                        if (key.equals(e.getKey()))
153                            return true;
154                    }
155                }
156                return false;
157            }
158
159            /**
160             * {@inheritDoc}
161             *
162             * <p>This implementation iterates over <tt>entrySet()</tt> searching
163             * for an entry with the specified key.  If such an entry is found,
164             * the entry's value is returned.  If the iteration terminates without
165             * finding such an entry, <tt>null</tt> is returned.  Note that this
166             * implementation requires linear time in the size of the map; many
167             * implementations will override this method.
168             *
169             * @throws ClassCastException            {@inheritDoc}
170             * @throws NullPointerException          {@inheritDoc}
171             */
172            public V get(Object key) {
173                Iterator<Entry<K, V>> i = entrySet().iterator();
174                if (key == null) {
175                    while (i.hasNext()) {
176                        Entry<K, V> e = i.next();
177                        if (e.getKey() == null)
178                            return e.getValue();
179                    }
180                } else {
181                    while (i.hasNext()) {
182                        Entry<K, V> e = i.next();
183                        if (key.equals(e.getKey()))
184                            return e.getValue();
185                    }
186                }
187                return null;
188            }
189
190            // Modification Operations
191
192            /**
193             * {@inheritDoc}
194             *
195             * <p>This implementation always throws an
196             * <tt>UnsupportedOperationException</tt>.
197             *
198             * @throws UnsupportedOperationException {@inheritDoc}
199             * @throws ClassCastException            {@inheritDoc}
200             * @throws NullPointerException          {@inheritDoc}
201             * @throws IllegalArgumentException      {@inheritDoc}
202             */
203            public V put(K key, V value) {
204                throw new UnsupportedOperationException();
205            }
206
207            /**
208             * {@inheritDoc}
209             *
210             * <p>This implementation iterates over <tt>entrySet()</tt> searching for an
211             * entry with the specified key.  If such an entry is found, its value is
212             * obtained with its <tt>getValue</tt> operation, the entry is removed
213             * from the collection (and the backing map) with the iterator's
214             * <tt>remove</tt> operation, and the saved value is returned.  If the
215             * iteration terminates without finding such an entry, <tt>null</tt> is
216             * returned.  Note that this implementation requires linear time in the
217             * size of the map; many implementations will override this method.
218             *
219             * <p>Note that this implementation throws an
220             * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt>
221             * iterator does not support the <tt>remove</tt> method and this map
222             * contains a mapping for the specified key.
223             *
224             * @throws UnsupportedOperationException {@inheritDoc}
225             * @throws ClassCastException            {@inheritDoc}
226             * @throws NullPointerException          {@inheritDoc}
227             */
228            public V remove(Object key) {
229                Iterator<Entry<K, V>> i = entrySet().iterator();
230                Entry<K, V> correctEntry = null;
231                if (key == null) {
232                    while (correctEntry == null && i.hasNext()) {
233                        Entry<K, V> e = i.next();
234                        if (e.getKey() == null)
235                            correctEntry = e;
236                    }
237                } else {
238                    while (correctEntry == null && i.hasNext()) {
239                        Entry<K, V> e = i.next();
240                        if (key.equals(e.getKey()))
241                            correctEntry = e;
242                    }
243                }
244
245                V oldValue = null;
246                if (correctEntry != null) {
247                    oldValue = correctEntry.getValue();
248                    i.remove();
249                }
250                return oldValue;
251            }
252
253            // Bulk Operations
254
255            /**
256             * {@inheritDoc}
257             *
258             * <p>This implementation iterates over the specified map's
259             * <tt>entrySet()</tt> collection, and calls this map's <tt>put</tt>
260             * operation once for each entry returned by the iteration.
261             *
262             * <p>Note that this implementation throws an
263             * <tt>UnsupportedOperationException</tt> if this map does not support
264             * the <tt>put</tt> operation and the specified map is nonempty.
265             *
266             * @throws UnsupportedOperationException {@inheritDoc}
267             * @throws ClassCastException            {@inheritDoc}
268             * @throws NullPointerException          {@inheritDoc}
269             * @throws IllegalArgumentException      {@inheritDoc}
270             */
271            public void putAll(Map<? extends K, ? extends V> m) {
272                for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
273                    put(e.getKey(), e.getValue());
274            }
275
276            /**
277             * {@inheritDoc}
278             *
279             * <p>This implementation calls <tt>entrySet().clear()</tt>.
280             *
281             * <p>Note that this implementation throws an
282             * <tt>UnsupportedOperationException</tt> if the <tt>entrySet</tt>
283             * does not support the <tt>clear</tt> operation.
284             *
285             * @throws UnsupportedOperationException {@inheritDoc}
286             */
287            public void clear() {
288                entrySet().clear();
289            }
290
291            // Views
292
293            /**
294             * Each of these fields are initialized to contain an instance of the
295             * appropriate view the first time this view is requested.  The views are
296             * stateless, so there's no reason to create more than one of each.
297             */
298            transient volatile Set<K> keySet = null;
299            transient volatile Collection<V> values = null;
300
301            /**
302             * {@inheritDoc}
303             *
304             * <p>This implementation returns a set that subclasses {@link AbstractSet}.
305             * The subclass's iterator method returns a "wrapper object" over this
306             * map's <tt>entrySet()</tt> iterator.  The <tt>size</tt> method
307             * delegates to this map's <tt>size</tt> method and the
308             * <tt>contains</tt> method delegates to this map's
309             * <tt>containsKey</tt> method.
310             *
311             * <p>The set is created the first time this method is called,
312             * and returned in response to all subsequent calls.  No synchronization
313             * is performed, so there is a slight chance that multiple calls to this
314             * method will not all return the same set.
315             */
316            public Set<K> keySet() {
317                if (keySet == null) {
318                    keySet = new AbstractSet<K>() {
319                        public Iterator<K> iterator() {
320                            return new Iterator<K>() {
321                                private Iterator<Entry<K, V>> i = entrySet()
322                                        .iterator();
323
324                                public boolean hasNext() {
325                                    return i.hasNext();
326                                }
327
328                                public K next() {
329                                    return i.next().getKey();
330                                }
331
332                                public void remove() {
333                                    i.remove();
334                                }
335                            };
336                        }
337
338                        public int size() {
339                            return AbstractMap.this .size();
340                        }
341
342                        public boolean isEmpty() {
343                            return AbstractMap.this .isEmpty();
344                        }
345
346                        public void clear() {
347                            AbstractMap.this .clear();
348                        }
349
350                        public boolean contains(Object k) {
351                            return AbstractMap.this .containsKey(k);
352                        }
353                    };
354                }
355                return keySet;
356            }
357
358            /**
359             * {@inheritDoc}
360             *
361             * <p>This implementation returns a collection that subclasses {@link
362             * AbstractCollection}.  The subclass's iterator method returns a
363             * "wrapper object" over this map's <tt>entrySet()</tt> iterator.
364             * The <tt>size</tt> method delegates to this map's <tt>size</tt>
365             * method and the <tt>contains</tt> method delegates to this map's
366             * <tt>containsValue</tt> method.
367             *
368             * <p>The collection is created the first time this method is called, and
369             * returned in response to all subsequent calls.  No synchronization is
370             * performed, so there is a slight chance that multiple calls to this
371             * method will not all return the same collection.
372             */
373            public Collection<V> values() {
374                if (values == null) {
375                    values = new AbstractCollection<V>() {
376                        public Iterator<V> iterator() {
377                            return new Iterator<V>() {
378                                private Iterator<Entry<K, V>> i = entrySet()
379                                        .iterator();
380
381                                public boolean hasNext() {
382                                    return i.hasNext();
383                                }
384
385                                public V next() {
386                                    return i.next().getValue();
387                                }
388
389                                public void remove() {
390                                    i.remove();
391                                }
392                            };
393                        }
394
395                        public int size() {
396                            return AbstractMap.this .size();
397                        }
398
399                        public boolean isEmpty() {
400                            return AbstractMap.this .isEmpty();
401                        }
402
403                        public void clear() {
404                            AbstractMap.this .clear();
405                        }
406
407                        public boolean contains(Object v) {
408                            return AbstractMap.this .containsValue(v);
409                        }
410                    };
411                }
412                return values;
413            }
414
415            public abstract Set<Entry<K, V>> entrySet();
416
417            // Comparison and hashing
418
419            /**
420             * Compares the specified object with this map for equality.  Returns
421             * <tt>true</tt> if the given object is also a map and the two maps
422             * represent the same mappings.  More formally, two maps <tt>m1</tt> and
423             * <tt>m2</tt> represent the same mappings if
424             * <tt>m1.entrySet().equals(m2.entrySet())</tt>.  This ensures that the
425             * <tt>equals</tt> method works properly across different implementations
426             * of the <tt>Map</tt> interface.
427             *
428             * <p>This implementation first checks if the specified object is this map;
429             * if so it returns <tt>true</tt>.  Then, it checks if the specified
430             * object is a map whose size is identical to the size of this map; if
431             * not, it returns <tt>false</tt>.  If so, it iterates over this map's
432             * <tt>entrySet</tt> collection, and checks that the specified map
433             * contains each mapping that this map contains.  If the specified map
434             * fails to contain such a mapping, <tt>false</tt> is returned.  If the
435             * iteration completes, <tt>true</tt> is returned.
436             *
437             * @param o object to be compared for equality with this map
438             * @return <tt>true</tt> if the specified object is equal to this map
439             */
440            public boolean equals(Object o) {
441                if (o == this )
442                    return true;
443
444                if (!(o instanceof  Map))
445                    return false;
446                Map<K, V> m = (Map<K, V>) o;
447                if (m.size() != size())
448                    return false;
449
450                try {
451                    Iterator<Entry<K, V>> i = entrySet().iterator();
452                    while (i.hasNext()) {
453                        Entry<K, V> e = i.next();
454                        K key = e.getKey();
455                        V value = e.getValue();
456                        if (value == null) {
457                            if (!(m.get(key) == null && m.containsKey(key)))
458                                return false;
459                        } else {
460                            if (!value.equals(m.get(key)))
461                                return false;
462                        }
463                    }
464                } catch (ClassCastException unused) {
465                    return false;
466                } catch (NullPointerException unused) {
467                    return false;
468                }
469
470                return true;
471            }
472
473            /**
474             * Returns the hash code value for this map.  The hash code of a map is
475             * defined to be the sum of the hash codes of each entry in the map's
476             * <tt>entrySet()</tt> view.  This ensures that <tt>m1.equals(m2)</tt>
477             * implies that <tt>m1.hashCode()==m2.hashCode()</tt> for any two maps
478             * <tt>m1</tt> and <tt>m2</tt>, as required by the general contract of
479             * {@link Object#hashCode}.
480             *
481             * <p>This implementation iterates over <tt>entrySet()</tt>, calling
482             * {@link Map.Entry#hashCode hashCode()} on each element (entry) in the
483             * set, and adding up the results.
484             *
485             * @return the hash code value for this map
486             * @see Map.Entry#hashCode()
487             * @see Object#equals(Object)
488             * @see Set#equals(Object)
489             */
490            public int hashCode() {
491                int h = 0;
492                Iterator<Entry<K, V>> i = entrySet().iterator();
493                while (i.hasNext())
494                    h += i.next().hashCode();
495                return h;
496            }
497
498            /**
499             * Returns a string representation of this map.  The string representation
500             * consists of a list of key-value mappings in the order returned by the
501             * map's <tt>entrySet</tt> view's iterator, enclosed in braces
502             * (<tt>"{}"</tt>).  Adjacent mappings are separated by the characters
503             * <tt>", "</tt> (comma and space).  Each key-value mapping is rendered as
504             * the key followed by an equals sign (<tt>"="</tt>) followed by the
505             * associated value.  Keys and values are converted to strings as by
506             * {@link String#valueOf(Object)}.
507             *
508             * @return a string representation of this map
509             */
510            public String toString() {
511                Iterator<Entry<K, V>> i = entrySet().iterator();
512                if (!i.hasNext())
513                    return "{}";
514
515                StringBuilder sb = new StringBuilder();
516                sb.append('{');
517                for (;;) {
518                    Entry<K, V> e = i.next();
519                    K key = e.getKey();
520                    V value = e.getValue();
521                    sb.append(key == this  ? "(this Map)" : key);
522                    sb.append('=');
523                    sb.append(value == this  ? "(this Map)" : value);
524                    if (!i.hasNext())
525                        return sb.append('}').toString();
526                    sb.append(", ");
527                }
528            }
529
530            /**
531             * Returns a shallow copy of this <tt>AbstractMap</tt> instance: the keys
532             * and values themselves are not cloned.
533             *
534             * @return a shallow copy of this map
535             */
536            protected Object clone() throws CloneNotSupportedException {
537                AbstractMap<K, V> result = (AbstractMap<K, V>) super .clone();
538                result.keySet = null;
539                result.values = null;
540                return result;
541            }
542
543            /**
544             * Utility method for SimpleEntry and SimpleImmutableEntry.
545             * Test for equality, checking for nulls.
546             */
547            private static boolean eq(Object o1, Object o2) {
548                return o1 == null ? o2 == null : o1.equals(o2);
549            }
550
551            // Implementation Note: SimpleEntry and SimpleImmutableEntry
552            // are distinct unrelated classes, even though they share
553            // some code. Since you can't add or subtract final-ness
554            // of a field in a subclass, they can't share representations,
555            // and the amount of duplicated code is too small to warrant
556            // exposing a common abstract class.
557
558            /**
559             * An Entry maintaining a key and a value.  The value may be
560             * changed using the <tt>setValue</tt> method.  This class
561             * facilitates the process of building custom map
562             * implementations. For example, it may be convenient to return
563             * arrays of <tt>SimpleEntry</tt> instances in method
564             * <tt>Map.entrySet().toArray</tt>.
565             *
566             * @since 1.6
567             */
568            public static class SimpleEntry<K, V> implements  Entry<K, V>,
569                    java.io.Serializable {
570                private static final long serialVersionUID = -8499721149061103585L;
571
572                private final K key;
573                private V value;
574
575                /**
576                 * Creates an entry representing a mapping from the specified
577                 * key to the specified value.
578                 *
579                 * @param key the key represented by this entry
580                 * @param value the value represented by this entry
581                 */
582                public SimpleEntry(K key, V value) {
583                    this .key = key;
584                    this .value = value;
585                }
586
587                /**
588                 * Creates an entry representing the same mapping as the
589                 * specified entry.
590                 *
591                 * @param entry the entry to copy
592                 */
593                public SimpleEntry(Entry<? extends K, ? extends V> entry) {
594                    this .key = entry.getKey();
595                    this .value = entry.getValue();
596                }
597
598                /**
599                 * Returns the key corresponding to this entry.
600                 *
601                 * @return the key corresponding to this entry
602                 */
603                public K getKey() {
604                    return key;
605                }
606
607                /**
608                 * Returns the value corresponding to this entry.
609                 *
610                 * @return the value corresponding to this entry
611                 */
612                public V getValue() {
613                    return value;
614                }
615
616                /**
617                 * Replaces the value corresponding to this entry with the specified
618                 * value.
619                 *
620                 * @param value new value to be stored in this entry
621                 * @return the old value corresponding to the entry
622                 */
623                public V setValue(V value) {
624                    V oldValue = this .value;
625                    this .value = value;
626                    return oldValue;
627                }
628
629                /**
630                 * Compares the specified object with this entry for equality.
631                 * Returns {@code true} if the given object is also a map entry and
632                 * the two entries represent the same mapping.	More formally, two
633                 * entries {@code e1} and {@code e2} represent the same mapping
634                 * if<pre>
635                 *   (e1.getKey()==null ?
636                 *    e2.getKey()==null :
637                 *    e1.getKey().equals(e2.getKey()))
638                 *   &amp;&amp;
639                 *   (e1.getValue()==null ?
640                 *    e2.getValue()==null :
641                 *    e1.getValue().equals(e2.getValue()))</pre>
642                 * This ensures that the {@code equals} method works properly across
643                 * different implementations of the {@code Map.Entry} interface.
644                 *
645                 * @param o object to be compared for equality with this map entry
646                 * @return {@code true} if the specified object is equal to this map
647                 *	   entry
648                 * @see    #hashCode
649                 */
650                public boolean equals(Object o) {
651                    if (!(o instanceof  Map.Entry))
652                        return false;
653                    Map.Entry e = (Map.Entry) o;
654                    return eq(key, e.getKey()) && eq(value, e.getValue());
655                }
656
657                /**
658                 * Returns the hash code value for this map entry.  The hash code
659                 * of a map entry {@code e} is defined to be: <pre>
660                 *   (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
661                 *   (e.getValue()==null ? 0 : e.getValue().hashCode())</pre>
662                 * This ensures that {@code e1.equals(e2)} implies that
663                 * {@code e1.hashCode()==e2.hashCode()} for any two Entries
664                 * {@code e1} and {@code e2}, as required by the general
665                 * contract of {@link Object#hashCode}.
666                 *
667                 * @return the hash code value for this map entry
668                 * @see    #equals
669                 */
670                public int hashCode() {
671                    return (key == null ? 0 : key.hashCode())
672                            ^ (value == null ? 0 : value.hashCode());
673                }
674
675                /**
676                 * Returns a String representation of this map entry.  This
677                 * implementation returns the string representation of this
678                 * entry's key followed by the equals character ("<tt>=</tt>")
679                 * followed by the string representation of this entry's value.
680                 *
681                 * @return a String representation of this map entry
682                 */
683                public String toString() {
684                    return key + "=" + value;
685                }
686
687            }
688
689            /**
690             * An Entry maintaining an immutable key and value.  This class
691             * does not support method <tt>setValue</tt>.  This class may be
692             * convenient in methods that return thread-safe snapshots of
693             * key-value mappings.
694             *
695             * @since 1.6
696             */
697            public static class SimpleImmutableEntry<K, V> implements 
698                    Entry<K, V>, java.io.Serializable {
699                private static final long serialVersionUID = 7138329143949025153L;
700
701                private final K key;
702                private final V value;
703
704                /**
705                 * Creates an entry representing a mapping from the specified
706                 * key to the specified value.
707                 *
708                 * @param key the key represented by this entry
709                 * @param value the value represented by this entry
710                 */
711                public SimpleImmutableEntry(K key, V value) {
712                    this .key = key;
713                    this .value = value;
714                }
715
716                /**
717                 * Creates an entry representing the same mapping as the
718                 * specified entry.
719                 *
720                 * @param entry the entry to copy
721                 */
722                public SimpleImmutableEntry(
723                        Entry<? extends K, ? extends V> entry) {
724                    this .key = entry.getKey();
725                    this .value = entry.getValue();
726                }
727
728                /**
729                 * Returns the key corresponding to this entry.
730                 *
731                 * @return the key corresponding to this entry
732                 */
733                public K getKey() {
734                    return key;
735                }
736
737                /**
738                 * Returns the value corresponding to this entry.
739                 *
740                 * @return the value corresponding to this entry
741                 */
742                public V getValue() {
743                    return value;
744                }
745
746                /**
747                 * Replaces the value corresponding to this entry with the specified
748                 * value (optional operation).  This implementation simply throws
749                 * <tt>UnsupportedOperationException</tt>, as this class implements
750                 * an <i>immutable</i> map entry.
751                 *
752                 * @param value new value to be stored in this entry
753                 * @return (Does not return)
754                 * @throws UnsupportedOperationException always
755                 */
756                public V setValue(V value) {
757                    throw new UnsupportedOperationException();
758                }
759
760                /**
761                 * Compares the specified object with this entry for equality.
762                 * Returns {@code true} if the given object is also a map entry and
763                 * the two entries represent the same mapping.	More formally, two
764                 * entries {@code e1} and {@code e2} represent the same mapping
765                 * if<pre>
766                 *   (e1.getKey()==null ?
767                 *    e2.getKey()==null :
768                 *    e1.getKey().equals(e2.getKey()))
769                 *   &amp;&amp;
770                 *   (e1.getValue()==null ?
771                 *    e2.getValue()==null :
772                 *    e1.getValue().equals(e2.getValue()))</pre>
773                 * This ensures that the {@code equals} method works properly across
774                 * different implementations of the {@code Map.Entry} interface.
775                 *
776                 * @param o object to be compared for equality with this map entry
777                 * @return {@code true} if the specified object is equal to this map
778                 *	   entry
779                 * @see    #hashCode
780                 */
781                public boolean equals(Object o) {
782                    if (!(o instanceof  Map.Entry))
783                        return false;
784                    Map.Entry e = (Map.Entry) o;
785                    return eq(key, e.getKey()) && eq(value, e.getValue());
786                }
787
788                /**
789                 * Returns the hash code value for this map entry.  The hash code
790                 * of a map entry {@code e} is defined to be: <pre>
791                 *   (e.getKey()==null   ? 0 : e.getKey().hashCode()) ^
792                 *   (e.getValue()==null ? 0 : e.getValue().hashCode())</pre>
793                 * This ensures that {@code e1.equals(e2)} implies that
794                 * {@code e1.hashCode()==e2.hashCode()} for any two Entries
795                 * {@code e1} and {@code e2}, as required by the general
796                 * contract of {@link Object#hashCode}.
797                 *
798                 * @return the hash code value for this map entry
799                 * @see    #equals
800                 */
801                public int hashCode() {
802                    return (key == null ? 0 : key.hashCode())
803                            ^ (value == null ? 0 : value.hashCode());
804                }
805
806                /**
807                 * Returns a String representation of this map entry.  This
808                 * implementation returns the string representation of this
809                 * entry's key followed by the equals character ("<tt>=</tt>")
810                 * followed by the string representation of this entry's value.
811                 *
812                 * @return a String representation of this map entry
813                 */
814                public String toString() {
815                    return key + "=" + value;
816                }
817
818            }
819
820        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.