/* * The contents of this file are subject to the Mozilla Public License Version 1.1 * (the "License"); you may not use this file except in compliance with the License. * You may obtain a copy of the License at . * * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT * WARRANTY OF ANY KIND, either express or implied. See the License for the specific * language governing rights and limitations under the License. * * The Original Code is the Venice Web Communities System. * * The Initial Developer of the Original Code is Eric J. Bowersox , * for Silverwrist Design Studios. Portions created by Eric J. Bowersox are * Copyright (C) 2001 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved. * * Contributor(s): */ package com.silverwrist.util.rcache; import java.util.*; public class ReferenceCache { /*-------------------------------------------------------------------------------- * Attributes *-------------------------------------------------------------------------------- */ private HashMap the_data; /*-------------------------------------------------------------------------------- * Constructor *-------------------------------------------------------------------------------- */ public ReferenceCache() { the_data = new HashMap(); } // end constructor /*-------------------------------------------------------------------------------- * External operations *-------------------------------------------------------------------------------- */ public synchronized ReferencedData get(Object key) { ReferencedData rc = (ReferencedData)(the_data.get(key)); if (rc!=null) rc.rd_addRef(); return rc; } // end get public synchronized ReferencedData getOrCreate(Object key, ReferencedDataBuilder builder) throws ReferencedDataBuilderException { ReferencedData rc = (ReferencedData)(the_data.get(key)); if (rc==null) { // use the builder to build one rc = builder.build(key); if (rc!=null) the_data.put(key,rc); } // end if else // just add a reference rc.rd_addRef(); return rc; } // end getOrCreate public void register(ReferencedData data) { Object key = data.rd_getKey(); synchronized (this) { // see if it's in the cache, if not, add it if (the_data.get(key)!=null) throw new ReferenceCacheException("object already in cache",key); // dump it in the object cache the_data.put(key,data); } // end synchronized block } // end register public synchronized void detach(Object key) { the_data.remove(key); } // end detach public void sweep() { int count = 0; synchronized (this) { // bail out early if possible if (the_data.size()==0) return; // provide a storage bin for all keys we decide to 86 Object keyzap[] = new Object[the_data.size()]; Iterator it = the_data.values().iterator(); while (it.hasNext()) { // check each value we contain in turn ReferencedData rd = (ReferencedData)(it.next()); if (rd.rd_unreferenced()) keyzap[count++] = rd.rd_getKey(); } // end while for (int i=0; i0) { // provide a storage bin for all keys we decide to 86 Object keyzap[] = new Object[the_data.size()]; Iterator it = the_data.values().iterator(); while (it.hasNext()) { // check each value we contain in turn ReferencedData rd = (ReferencedData)(it.next()); if (rd.rd_unreferenced()) keyzap[count++] = rd.rd_getKey(); else { // add another reference and tack it onto the list rd.rd_addRef(); rc.add(rd); } // end else } // end while for (int i=0; i