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

import java.util.Arrays;
import net.jini.core.lookup.ServiceID;

/**
 * Class representing the values in a multicast announcement.
 *
 * @author Sun Microsystems, Inc.
 * @since 2.0
 */
public class MulticastAnnouncement {

    /** The announcement sequence number. */
    protected long sequenceNumber;
    /** The lookup service host. */
    protected String host;
    /** The lookup service listen port. */
    protected int port;
    /** The lookup service member groups. */
    protected String[] groups;
    /** The lookup service ID. */
    protected ServiceID serviceID;

    /**
     * Creates a new <code>MulticastAnnouncement</code> instance containing the
     * given values.  The <code>groups</code> array is copied; a
     * <code>null</code> value is considered equivalent to an empty array.
     *
     * @param sequenceNumber the announcement sequence number
     * @param host the lookup service host
     * @param port the lookup service listen port
     * @param groups the lookup service member groups
     * @param serviceID the lookup service ID
     * @throws NullPointerException if <code>host</code> or
     * <code>serviceID</code> is <code>null</code>, or if <code>groups</code>
     * contains <code>null</code>
     * @throws IllegalArgumentException if <code>port</code> is out of range
     */
    public MulticastAnnouncement(long sequenceNumber,
				 String host,
				 int port,
				 String[] groups,
				 ServiceID serviceID)
    {
	groups = (groups != null) ? (String[]) groups.clone() : new String[0];
	if (host == null || 
	    serviceID == null ||
	    Arrays.asList(groups).contains(null))
	{
	    throw new NullPointerException();
	}
	if (port < 0 || port > 0xFFFF) {
	    throw new IllegalArgumentException("port out of range: " + port);
	}
	this.sequenceNumber = sequenceNumber;
	this.host = host;
	this.port = port;
	this.groups = groups;
	this.serviceID = serviceID;
    }

    /**
     * Returns the announcement sequence number.
     *
     * @return the announcement sequence number
     */
    public long getSequenceNumber() {
	return sequenceNumber;
    }

    /**
     * Returns the lookup service host name.
     *
     * @return the lookup service host name
     */
    public String getHost() {
	return host;
    }

    /**
     * Returns the lookup service listen port.
     *
     * @return the lookup service listen port
     */
    public int getPort() {
	return port;
    }

    /**
     * Returns the member groups of the lookup service.
     *
     * @return the member groups of the lookup service
     */
    public String[] getGroups() {
	return (String[]) groups.clone();
    }

    /**
     * Returns the service ID of the lookup service.
     *
     * @return the service ID of the lookup service
     */
    public ServiceID getServiceID() {
	return serviceID;
    }

    /**
     * Returns a string representation of this announcement.
     *
     * @return a string representation of this announcement
     */
    public String toString() {
	return "MulticastAnnouncement[" + sequenceNumber + ", " + host + ":" +
	       port + ", " + Arrays.asList(groups) + ", " + serviceID + "]";
    }
}
