/*
 * 
 * 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 net.jini.discovery;

import java.util.EventObject;
import java.util.Map;
import net.jini.core.lookup.ServiceRegistrar;

/**
 * Event object passed (via either the <code>DiscoveryListener</code>
 * interface or the <code>DiscoveryChangeListener</code>) to indicate to
 * interested parties that one or more <code>ServiceRegistrar</code>
 * objects have been discovered or discarded during the discovery process.
 *
 * @author Sun Microsystems, Inc.
 *
 * @see LookupDiscovery
 * @see LookupLocatorDiscovery
 * @see LookupDiscoveryManager
 * @see DiscoveryListener
 * @see DiscoveryChangeListener
 * @see net.jini.core.lookup.ServiceRegistrar
 */
public class DiscoveryEvent extends EventObject {
    private static final long serialVersionUID = 5280303374696501479L;

    /**
     * The registrars with which this event is associated.
     *
     * @serial
     */
    protected ServiceRegistrar[] regs;

    /**
     * Map from the registrars of this event to the groups in which each
     * is a member.
     *
     * @serial
     */
    protected Map groups;

    /**
     * Construct a new <code>DiscoveryEvent</code> object, with the given
     * source and set of registrars.  The set of registrars should not be
     * empty.
     *
     * @param source the source of this event
     * @param regs   the registrars to which this event applies
     */
    public DiscoveryEvent(Object source, ServiceRegistrar[] regs) {
	super(source);
	this.regs = regs;
	this.groups = null;
    }

    /**
     * Construct a new <code>DiscoveryEvent</code> object, with the given
     * source and registrars-to-groups mapping. The mapping should not be
     * empty.
     *
     * @param source the source of this event
     * @param groups mapping from the elements of the registrars of this
     *               event to the member groups in which each registrar is
     *               a member
     */
    public DiscoveryEvent(Object source, Map groups) {
	super(source);
	this.groups = groups;
        this.regs   = (ServiceRegistrar[])(groups.keySet()).toArray
                                       (new ServiceRegistrar[groups.size()]);
    }

    /**
     * Return the set of registrars to which this event applies.
     * The same array is returned on every call; a copy is not made.
     * @return the set of registrars to which this event applies.
     */
    public ServiceRegistrar[] getRegistrars() {
	return regs;
    }

    /**
     * Returns a set that maps to each registrar referenced by this event,
     * the current set of groups in which each registrar is a member.
     * <p>
     * To retrieve the set of member groups corresponding to any element
     * of the array returned by the <code>getRegistrars</code> method,
     * simply use the desired element from that array as the key to the
     * <code>get</code> method of the <code>Map</code> object returned
     * by this method and cast to <code>String</code>[].
     * <p>
     * Note that the same <code>Map</code> object is returned on every
     * call to this method; that is, a copy is not made.
     *
     *  @return <code>Map</code> in which the keys are the elements of the
     *          array returned by the <code>getRegistrars</code> method
     *          of this class; and the values are <code>String</code>[]
     *          arrays containing the member groups corresponding to each
     *          registrar.
     */
    public Map getGroups() {
        return groups;
    }
}
