You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SoftHashMap.java 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* *******************************************************************
  2. * Copyright (c) 2017 Contributors
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * 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. * ******************************************************************/package org.aspectj.util;
  9. import java.lang.ref.ReferenceQueue;
  10. import java.lang.ref.SoftReference;
  11. import java.util.AbstractMap;
  12. import java.util.Collections;
  13. import java.util.HashMap;
  14. import java.util.Map;
  15. public class SoftHashMap<K,V> extends AbstractMap<K,V> {
  16. private Map<K, SpecialValue> map;
  17. private ReferenceQueue<? super V> rq = new ReferenceQueue();
  18. public SoftHashMap() {
  19. this.map = new HashMap<K,SpecialValue>();
  20. }
  21. class SpecialValue extends SoftReference<V> {
  22. private final K key;
  23. SpecialValue(K k, V v) {
  24. super(v, rq);
  25. this.key = k;
  26. }
  27. }
  28. @SuppressWarnings("unchecked")
  29. private void processQueue() {
  30. SpecialValue sv = null;
  31. while ((sv = (SpecialValue)rq.poll()) != null) {
  32. map.remove(sv.key);
  33. }
  34. }
  35. @Override
  36. public V get(Object key) {
  37. SpecialValue ref = map.get(key);
  38. if (ref == null) {
  39. map.remove(key);
  40. return null;
  41. }
  42. V value = ref.get();
  43. if (value == null) {
  44. map.remove(ref.key);
  45. return null;
  46. }
  47. return value;
  48. }
  49. @Override
  50. public V put(K k, V v) {
  51. processQueue();
  52. SpecialValue sv = new SpecialValue(k, v);
  53. SpecialValue result = map.put(k, sv);
  54. return (result == null ? null : result.get());
  55. }
  56. @Override
  57. public java.util.Set<Map.Entry<K,V>> entrySet() {
  58. if (map.isEmpty()) { return Collections.<K,V>emptyMap().entrySet(); }
  59. Map<K,V> currentContents = new HashMap<K,V>();
  60. for (Map.Entry<K,SpecialValue> entry: map.entrySet()) {
  61. V currentValueForEntry = entry.getValue().get();
  62. if (currentValueForEntry != null) {
  63. currentContents.put(entry.getKey(), currentValueForEntry);
  64. }
  65. }
  66. return currentContents.entrySet();
  67. }
  68. @Override
  69. public void clear() {
  70. processQueue();
  71. map.clear();
  72. }
  73. @Override
  74. public int size() {
  75. processQueue();
  76. return map.size();
  77. }
  78. @Override
  79. public V remove(Object k) {
  80. processQueue();
  81. SpecialValue ref = map.remove(k);
  82. if (ref == null) {
  83. return null;
  84. }
  85. return ref.get();
  86. }
  87. }