| 1 | package org.jtoolkit.essence.utils.impl; |
| 2 | /* |
| 3 | Copyright 2006 Peter Lawrey |
| 4 | |
| 5 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | you may not use this file except in compliance with the License. |
| 7 | You may obtain a copy of the License at |
| 8 | |
| 9 | http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | |
| 11 | Unless required by applicable law or agreed to in writing, software |
| 12 | distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | See the License for the specific language governing permissions and |
| 15 | limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | import org.jtoolkit.essence.utils.ImmutableUtils; |
| 19 | import org.jetbrains.annotations.Nullable; |
| 20 | |
| 21 | import java.util.*; |
| 22 | |
| 23 | /** |
| 24 | * This map treats immutable objects as equals() and mutable objects with == |
| 25 | * |
| 26 | * @author Peter Lawrey |
| 27 | * @since 03-Dec-2006 |
| 28 | */ |
| 29 | public final class SameHashMap<K, V> { |
| 30 | // Needs LHM so that prune works as expected. (oldest first); |
| 31 | private final Map<Object, V> map = new LinkedHashMap<Object, V>(256, 0.5f); |
| 32 | |
| 33 | public V get(K key) { |
| 34 | return map.get(makeKey(key)); |
| 35 | } |
| 36 | |
| 37 | public void put(K key, V value) { |
| 38 | map.put(makeKey(key), value); |
| 39 | } |
| 40 | |
| 41 | /* |
| 42 | public V remove(K key) { |
| 43 | return map.remove(makeKey(key)); |
| 44 | } |
| 45 | */ |
| 46 | |
| 47 | private Object makeKey(K key) { |
| 48 | if (key == null) //noinspection ConstantConditions |
| 49 | return null; |
| 50 | if (ImmutableUtils.isImmutable(key.getClass())) |
| 51 | return key; |
| 52 | // System.out.println("~~~~ Mutable "+key.getClass()); |
| 53 | return new IdentityKey(key); |
| 54 | } |
| 55 | |
| 56 | public int size() { |
| 57 | return map.size(); |
| 58 | } |
| 59 | |
| 60 | @Nullable public V prune(int pruneSize) { |
| 61 | List<Object> keysToRemove = new ArrayList<Object>(); |
| 62 | int toRemove = map.size() - pruneSize; |
| 63 | Iterator<Map.Entry<Object, V>> iterator = map.entrySet().iterator(); |
| 64 | V last = null; |
| 65 | for (int i = 0; i < toRemove; i++) { |
| 66 | Map.Entry<Object, V> entry = iterator.next(); |
| 67 | keysToRemove.add(entry.getKey()); |
| 68 | last = entry.getValue(); |
| 69 | } |
| 70 | map.keySet().removeAll(keysToRemove); |
| 71 | return last; |
| 72 | } |
| 73 | |
| 74 | static class IdentityKey { |
| 75 | private final Object key; |
| 76 | private final int hashCode; |
| 77 | |
| 78 | IdentityKey(Object key) { |
| 79 | this.key = key; |
| 80 | hashCode = System.identityHashCode(key); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public int hashCode() { |
| 85 | return hashCode; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean equals(Object obj) { |
| 90 | return obj != null && obj instanceof IdentityKey && key == ((IdentityKey) obj).key; |
| 91 | } |
| 92 | } |
| 93 | } |