/*
 * 
 * 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 request.
 *
 * @author Sun Microsystems, Inc.
 * @since 2.0
 */
public class MulticastRequest {

    /** The client host. */
    protected String host;
    /** The client listen port. */
    protected int port;
    /** The groups of interest. */
    protected String[] groups;
    /** The IDs of known lookup services. */
    protected ServiceID[] serviceIDs;

    /**
     * Creates a new <code>MulticastRequest</code> instance containing the
     * given values.  Arrays are copied; <code>null</code> array values are
     * considered equivalent to empty arrays.
     *
     * @param host the client host
     * @param port the client listen port
     * @param groups the groups of interest
     * @param serviceIDs the IDs of known lookup services
     * @throws NullPointerException if <code>host</code> is <code>null</code>,
     * or if <code>groups</code> or <code>serviceIDs</code> contains
     * <code>null</code>
     * @throws IllegalArgumentException if <code>port</code> is out of range
     */
    public MulticastRequest(String host,
			    int port,
			    String[] groups,
			    ServiceID[] serviceIDs)
    {
	groups = (groups != null) ? (String[]) groups.clone() : new String[0];
	serviceIDs = (serviceIDs != null) ?
	    (ServiceID[]) serviceIDs.clone() : new ServiceID[0];
	if (host == null ||
	    Arrays.asList(groups).contains(null) ||
	    Arrays.asList(serviceIDs).contains(null))
	{
	    throw new NullPointerException();
	}
	if (port < 0 || port > 0xFFFF) {
	    throw new IllegalArgumentException("port out of range: " + port);
	}
	this.host = host;
	this.port = port;
	this.groups = groups;
	this.serviceIDs = serviceIDs;
    }

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

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

    /**
     * Returns the groups targeted by the request.
     *
     * @return the groups targeted by the request
     */
    public String[] getGroups() {
	return (String[]) groups.clone();
    }

    /**
     * Returns the service IDs of known lookup services.
     *
     * @return the service IDs of known lookup services
     */
    public ServiceID[] getServiceIDs() {
	return (ServiceID[]) serviceIDs.clone();
    }

    /**
     * Returns string representation of this request.
     *
     * @return string representation of this request
     */
    public String toString() {
	return "MulticastRequest[" + host + ":" + port + ", " +
	       Arrays.asList(groups) + ", " + Arrays.asList(serviceIDs) + "]";
    }
}
