Subversion Repositories programming

Rev

Rev 222 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
221 ira 1
 
2
public class IPAddr
3
{
4
    private int[] fields = new int[4];
5
 
6
    public IPAddr (String ip)
7
    {
8
        String[] sp = ip.split("\\.");
9
 
10
        for (int i=0; i<sp.length; i++)
11
            fields[i] = Integer.parseInt(sp[i]);
12
    }
13
 
14
    public int getField (int fieldNum)
15
    {
16
        return fields[fieldNum];
17
    }
18
 
19
    public int compareTo (IPAddr ip)
20
    {
21
        int i;
22
 
23
        for (i=0; i<4; i++)
24
            if (fields[i] < ip.getField(i))
25
                return -1;
26
            else if (fields[i] > ip.getField(i))
27
                return 1;
28
 
29
        return 0; // Equal
30
    }
31
 
32
    public int compareTo (String ip)
33
    {
34
        IPAddr that = new IPAddr (ip);
35
 
36
        return this.compareTo(that);
37
    }
38
 
39
    public String toString ()
40
    {
41
        String s = new String();
42
        s.format ("%d.%d.%d.%d", fields[0], fields[1], fields[2], fields[3]);
43
 
44
        return s;
45
    }
46
}
47