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