1 22 package org.jboss.util.collection; 23 24 import java.util.List ; 25 import java.util.LinkedList ; 26 import java.util.AbstractList ; 27 28 import java.lang.ref.ReferenceQueue ; 29 30 import org.jboss.util.SoftObject; 31 import org.jboss.util.Objects; 32 33 41 public class CachedList 42 extends AbstractList 43 { 44 45 protected final ReferenceQueue queue = new ReferenceQueue (); 46 47 48 protected final List list; 49 50 55 public CachedList(final List list) { 56 this.list = list; 57 } 58 59 63 public CachedList() { 64 this(new LinkedList ()); 65 } 66 67 70 private Object getObject(final int index) { 71 Object obj = list.get(index); 72 73 return Objects.deref(obj); 74 } 75 76 82 public Object get(final int index) { 83 maintain(); 84 return getObject(index); 85 } 86 87 92 public int size() { 93 maintain(); 94 return list.size(); 95 } 96 97 105 public Object set(final int index, final Object obj) { 106 maintain(); 107 108 SoftObject soft = SoftObject.create(obj, queue); 109 soft = (SoftObject)list.set(index, soft); 110 111 return Objects.deref(soft); 112 } 113 114 123 public void add(final int index, final Object obj) { 124 maintain(); 125 126 SoftObject soft = SoftObject.create(obj, queue); 127 list.add(index, soft); 128 } 129 130 138 public Object remove(final int index) { 139 maintain(); 140 141 Object obj = list.remove(index); 142 return Objects.deref(obj); 143 } 144 145 148 private void maintain() { 149 SoftObject obj; 150 int count = 0; 151 152 while ((obj = (SoftObject)queue.poll()) != null) { 153 count++; 154 list.remove(obj); 155 } 156 157 if (count != 0) { 158 System.err.println("vm reclaimed " + count + " objects"); 160 } 161 } 162 } 163 | Popular Tags |