/*
 * 
 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
 * 
 * The contents of this file are subject to the Sun Community Source
 * License v3.0/Jini Technology Specific Attachment v 1.0 (the
 * "License"). You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 * http://wwws.sun.com/software/jini/licensing/SCSL3_JiniTSA1.html.
 * 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 Reference Code is Jini Technology Core Platform code, v2.0. The
 * Developer of the Reference Code is Sun Microsystems, Inc.
 * 
 * Contributor(s): Sun Microsystems, Inc.
 * 
 * The contents of this file comply with the Jini Technology Core
 * Platform Compatibility Kit, v2.0A.
 * 
 * Tester(s): Sun Microsystems, Inc.
 * 
 * Test Platform(s):
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Solaris(TM)SPARC/x86
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Linux (Intel x86)
 * Java 2 SDK, Standard Edition, Version 1.4.2_03 for Windows (Intel Platform)
 * 
 */
package com.sun.jini.outrigger;

import java.util.Hashtable;
import java.util.Map;
import java.util.Collection;

import net.jini.core.lease.Lease;
import net.jini.id.Uuid;
import com.sun.jini.landlord.LeasedResource;

/**
 * A set of <code>EntryHolder</code> objects for a given space.
 *
 * @author Sun Microsystems, Inc.
 *
 * @see EntryHolder
 * @see OutriggerServerImpl
 */
class EntryHolderSet {
    private final Map holders = new java.util.HashMap();
    // Map of LeaseDescs indexed by the cookie of the underlying
    // LeasedResource.
    private final Hashtable idMap = new Hashtable();

    private final OutriggerServerImpl space;

    EntryHolderSet(OutriggerServerImpl space) {
	this.space = space;
    }

    /**
     * Return the <code>EntryHolder</code> object for the exact class
     * of the <code>Entry</code> object held in <code>bits</code>.  If
     * one doesn't yet exist, it will be created.
     *
     * @see #holderFor(java.lang.String)
     */
    EntryHolder holderFor(EntryRep rep) {
	return holderFor(rep.classFor());
    }

    /**
     * Return the <code>EntryHolder</code> object for the exact class
     * with the given ID. If one doesn't yet exist, it will be created.
     *
     * @see #holderFor(EntryRep)
     */
    synchronized EntryHolder holderFor(String className) {
	synchronized (holders) {
	    EntryHolder holder = (EntryHolder) holders.get(className);
	    if (holder == null) {
		holder = new EntryHolder(space, idMap);
		holders.put(className, holder);
	    }
	    return holder;
	}
    }

    public boolean cancel(Uuid id) {
	final EntryHandle handle = (EntryHandle)idMap.get(id);
	if (handle == null)
	    return false;
	return holderFor(handle.classFor()).cancel(handle);
    }

    LeasedResource getLeasedResource(Uuid cookie) {
	final EntryHandle handle = handleFor(cookie);
	if (handle == null)
	    return null;
	return handle.getLeasedResource();
    }

    /**
     * Given an entry ID, return the handle associated
     * with it.
     */
    EntryHandle handleFor(Object cookie) {
	return (EntryHandle)idMap.get(cookie);
    }

    /**
     * Force all of the holders to reap their <code>FastList</code>s
     */
    void reap() {
 	/* This could take a while, instead of blocking all other
	 * access to holders, clone the contents of holders and
	 * iterate down the clone (we don't do this too often and
	 * holders should never be that big so a shallow copy should
	 * not be that bad, if it did get to be bad caching the
	 * clone would probably work well since we don't add (and
	 * never remove) elements that often.)
	 */
	final EntryHolder content[];
	synchronized (holders) {
	    final Collection values = holders.values();
	    content = new EntryHolder[values.size()];
	    values.toArray(content);
	}

	for (int i=0; i<content.length; i++) {
	    content[i].reap();
	}	    
    }
}
