1 | /* |
2 | Copyright (C) 2004 MySQL AB |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License version 2 as |
6 | published by the Free Software Foundation. |
7 | |
8 | This program is distributed in the hope that it will be useful, |
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | GNU General Public License for more details. |
12 | |
13 | You should have received a copy of the GNU General Public License |
14 | along with this program; if not, write to the Free Software |
15 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
16 | |
17 | */ |
18 | package com.mysql.management.util; |
19 | |
20 | import java.util.Map; |
21 | |
22 | /** |
23 | * Simple (and obvious) implementation of java.util.Map.Entry |
24 | * |
25 | * This class is final simply as a hint to the compiler, it may be un-finalized |
26 | * safely. |
27 | * |
28 | * @author Eric Herman <eric@mysql.com> |
29 | * @version $Id: MapEntry.java,v 1.5 2005/09/22 20:41:36 eherman Exp $ |
30 | */ |
31 | public final class MapEntry implements Map.Entry { |
32 | private Object key; |
33 | |
34 | private Object value; |
35 | |
36 | private Equals equals; |
37 | |
38 | public MapEntry(Object key, Object value) { |
39 | this.key = key; |
40 | this.value = value; |
41 | this.equals = new Equals(); |
42 | } |
43 | |
44 | public Object getKey() { |
45 | return key; |
46 | } |
47 | |
48 | public Object getValue() { |
49 | return value; |
50 | } |
51 | |
52 | public Object setValue(Object value) { |
53 | Object oldVal = this.value; |
54 | this.value = value; |
55 | return oldVal; |
56 | } |
57 | |
58 | public boolean equals(Object obj) { |
59 | if (!(obj instanceof Map.Entry)) { |
60 | return false; |
61 | } |
62 | return equals((Map.Entry) obj); |
63 | } |
64 | |
65 | public boolean equals(Map.Entry other) { |
66 | if (other == this) { |
67 | return true; |
68 | } |
69 | |
70 | if ((other == null) || (hashCode() != other.hashCode())) { |
71 | return false; |
72 | } |
73 | |
74 | return equals.nullSafe(key, other.getKey()) |
75 | && equals.nullSafe(value, other.getValue()); |
76 | } |
77 | |
78 | /** |
79 | * XOR of the key and value hashCodes. (Zero used for nulls) as defined by |
80 | * Map.Entry java doc. |
81 | */ |
82 | public int hashCode() { |
83 | int keyHashCode = (key == null) ? 0 : key.hashCode(); |
84 | int valHashCode = (value == null) ? 0 : value.hashCode(); |
85 | return keyHashCode ^ valHashCode; |
86 | } |
87 | |
88 | public String toString() { |
89 | return getKey() + "=" + getValue(); |
90 | } |
91 | } |