1 8 package org.ozoneDB.core.storage; 9 10 import java.lang.ref.ReferenceQueue ; 11 import java.lang.ref.SoftReference ; 12 import java.util.Properties ; 13 14 43 public class SoftReferenceCache extends AbstractReferenceCache { 44 45 private static class ObjectReference extends SoftReference implements KeyedReference { 46 47 50 private Object key; 51 52 ObjectReference(Object key, Object value, ReferenceQueue q) { 53 super(value, q); 54 this.key = key; 55 } 56 57 public Object getKey() { 58 return key; 59 } 60 61 public String toString() { 62 return key.toString(); 63 } 64 65 public int hashCode() { 66 return key.hashCode(); 67 } 68 69 public boolean equals(Object o) { 70 if (!(o instanceof ObjectReference)) { 71 return false; 72 } else { 73 ObjectReference otherRef = (ObjectReference) o; 74 return otherRef.getKey().equals(getKey()); 75 } 76 } 77 78 } 80 public SoftReferenceCache(Properties properties, String prefix) { 81 super(properties, prefix); 82 } 83 84 protected KeyedReference createKeyedReference(Object key, Object value, ReferenceQueue referenceQueue) { 85 return new ObjectReference(key, value, referenceQueue); 86 } 87 88 90 public SoftReferenceCache() { 91 } 92 93 private static class Item { 94 95 public Object key; 96 public byte[] filling = new byte[1000]; 97 98 Item(Object key) { 99 this.key = key; 100 } 101 102 public String toString() { 103 return key.toString(); 104 } 105 } 106 107 public static void main(String args[]) { 108 SoftReferenceCache cache = new SoftReferenceCache(); 109 int last = 0; 110 for (int i = 0; i < 10000000; i++) { 111 Object key = new Integer (i); 112 Item item = new Item(key); 113 cache.put(key, item); 114 if (cache.size() != last + 1) { 118 System.out.println(cache.size() + ", last: " + last); 119 } 120 last = cache.size(); 121 122 } 123 System.out.println("completed successfully"); 124 } 125 126 } 127 | Popular Tags |