Subversion Repositories programming

Rev

Rev 224 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*******************************************************************************
 * File: IPAddr.java
 * Author: Ira W. Snyder (devel@irasnyder.com)
 * License: GNU General Public License v2
 * Class: CS380 - Computer Networking
 *
 * Assignment: Project #3
 * Date Last Modified: 2006-02-15
 *
 * Purpose: Implement a simple-to-access representation of an IP Address.
 * This is used extensively by the DHCPTable class. This class allows easy
 * access to each of the fields in the IP Address.
 ******************************************************************************/

public class IPAddr
{
    /* Instance Variables */
    private int[] fields = new int[4];

    /**
     * Method: IPAddr constructor
     * Purpose: Construct an IPAddr object
     */
    public IPAddr (String ip)
    {
        int i;
        String[] sp = ip.split("\\.");

        // We must have a bad argument
        if (sp.length < 4)
            throw new IllegalArgumentException();

        // Set the private variable array to the argument
        for (i=0; i<sp.length; i++)
            fields[i] = Integer.parseInt(sp[i]);
    }

    /**
     * Method: getField
     * Purpose: Get one of the fields of an IP Address
     */
    public int getField (int fieldNum)
    {
        if (fieldNum < 0 || fieldNum > 3)
            throw new IllegalArgumentException();

        return fields[fieldNum];
    }

    /**
     * Method: toString
     * Purpose: Convert this IPAddr object to a String
     */
    public String toString ()
    {
        String s = new String("" + fields[0] + '.' + fields[1] + '.'
                            + fields[2] + '.' + fields[3]);
        return s;
    }
}