|
| 1 | +/* |
| 2 | + Copyright (C) 2021 Deephaven Data Labs (https://deephaven.io). |
| 3 | +
|
| 4 | + This program is free software: you can redistribute it and/or modify it under the terms of the |
| 5 | + GNU Lesser General Public License as published by the Free Software Foundation, either version 3 |
| 6 | + of the License, or (at your option) any later version. |
| 7 | +
|
| 8 | + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 9 | + without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + GNU Lesser General Public License for more details. |
| 11 | +*/ |
| 12 | +package io.deephaven.hash; |
| 13 | + |
| 14 | +/** Test class. */ |
| 15 | +class KeyedDoubleTestObject { |
| 16 | + private final double id; |
| 17 | + |
| 18 | + public KeyedDoubleTestObject(double id) { |
| 19 | + this.id = id; |
| 20 | + } |
| 21 | + |
| 22 | + public double getId() { |
| 23 | + return id; |
| 24 | + } |
| 25 | + |
| 26 | + public boolean equals(Object other) { |
| 27 | + return other instanceof KeyedDoubleTestObject && id == ((KeyedDoubleTestObject) other).id; |
| 28 | + } |
| 29 | + |
| 30 | + public int hashCode() { |
| 31 | + return ~Double.hashCode(id); // do something different that gnu.trove.HashFunctions.hash(double) |
| 32 | + } |
| 33 | + |
| 34 | + public String toString() { |
| 35 | + return "[KeyedDoubleTestObject:" + id + "]"; |
| 36 | + } |
| 37 | + |
| 38 | + public static final KeyedDoubleObjectKey<KeyedDoubleTestObject> keyDef = |
| 39 | + new KeyedDoubleObjectKey<KeyedDoubleTestObject>() { |
| 40 | + public Double getKey(KeyedDoubleTestObject v) { |
| 41 | + return v.getId(); |
| 42 | + } |
| 43 | + |
| 44 | + public double getDoubleKey(KeyedDoubleTestObject v) { |
| 45 | + return v.getId(); |
| 46 | + } |
| 47 | + |
| 48 | + public int hashKey(Double k) { |
| 49 | + return k.hashCode(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public boolean equalKey(Double k, KeyedDoubleTestObject v) { |
| 54 | + return v.getId() == k; |
| 55 | + } |
| 56 | + |
| 57 | + public int hashDoubleKey(double k) { |
| 58 | + return Double.hashCode(k); |
| 59 | + } |
| 60 | + |
| 61 | + public boolean equalDoubleKey(double k, KeyedDoubleTestObject v) { |
| 62 | + return v.getId() == k; |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + public static final KeyedIntObjectHash.ValueFactory<KeyedDoubleTestObject> factory = |
| 67 | + new KeyedIntObjectHash.ValueFactory<KeyedDoubleTestObject>() { |
| 68 | + public KeyedDoubleTestObject newValue(Integer key) { |
| 69 | + return new KeyedDoubleTestObject(key); |
| 70 | + } |
| 71 | + |
| 72 | + public KeyedDoubleTestObject newValue(int key) { |
| 73 | + return new KeyedDoubleTestObject(key); |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + // for intrusive chained maps |
| 78 | + |
| 79 | + private KeyedDoubleTestObject next; |
| 80 | + |
| 81 | + public static final IntrusiveChainedHashAdapter<KeyedDoubleTestObject> adapter = |
| 82 | + new IntrusiveChainedHashAdapter<KeyedDoubleTestObject>() { |
| 83 | + @Override |
| 84 | + public KeyedDoubleTestObject getNext(KeyedDoubleTestObject self) { |
| 85 | + return self.next; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void setNext(KeyedDoubleTestObject self, KeyedDoubleTestObject next) { |
| 90 | + self.next = next; |
| 91 | + } |
| 92 | + }; |
| 93 | +} |
0 commit comments