]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.datastructures/src/org/simantics/utils/datastructures/ArrayMapEntry.java
Merge commit '53059ca'
[simantics/platform.git] / bundles / org.simantics.utils.datastructures / src / org / simantics / utils / datastructures / ArrayMapEntry.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2016 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     Semantum Oy - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.utils.datastructures;\r
13 \r
14 import java.util.Map;\r
15 \r
16 /**\r
17  * A common {@link Entry} implementation for both {@link ArrayMap}\r
18  * and {@link InterlacedArrayMap}.\r
19  * \r
20  * @author Tuukka Lehtonen\r
21  *\r
22  * @param <K> key class\r
23  * @param <V> value class\r
24  */\r
25 class ArrayMapEntry<K, V> implements Map.Entry<K, V> {\r
26     final K key;\r
27     V value;\r
28 \r
29     /**\r
30      * Creates new entry.\r
31      */\r
32     ArrayMapEntry(int h, K k, V v) {\r
33         value = v;\r
34         key = k;\r
35     }\r
36 \r
37     @Override\r
38     public final K getKey() {\r
39         return key;\r
40     }\r
41 \r
42     @Override\r
43     public final V getValue() {\r
44         return value;\r
45     }\r
46 \r
47     @Override\r
48     public final V setValue(V newValue) {\r
49         V oldValue = value;\r
50         value = newValue;\r
51         return oldValue;\r
52     }\r
53 \r
54     @Override\r
55     public final boolean equals(Object o) {\r
56         if (!(o instanceof Map.Entry<?, ?>))\r
57             return false;\r
58         Map.Entry<?, ?> e = (Map.Entry<?, ?>)o;\r
59         Object k1 = getKey();\r
60         Object k2 = e.getKey();\r
61         if (k1 == k2 || (k1 != null && k1.equals(k2))) {\r
62             Object v1 = getValue();\r
63             Object v2 = e.getValue();\r
64             if (v1 == v2 || (v1 != null && v1.equals(v2)))\r
65                 return true;\r
66         }\r
67         return false;\r
68     }\r
69 \r
70     @Override\r
71     public final int hashCode() {\r
72         return (key==null   ? 0 : key.hashCode()) ^\r
73         (value==null ? 0 : value.hashCode());\r
74     }\r
75 \r
76     @Override\r
77     public final String toString() {\r
78         return getKey() + "=" + getValue();\r
79     }\r
80 }