blob: 4d0c755f379eea5d850048c8d779bcf63c26a715 [file] [log] [blame]
Pavlin Radoslavov9de27722014-10-23 20:31:15 -07001package org.onlab.packet;
2
3import java.util.Objects;
4
5/**
6 * The class representing an IPv4 network address.
7 * This class is immutable.
8 */
9public final class Ip4Prefix {
10 private final Ip4Address address; // The IPv4 address
11 private final short prefixLen; // The prefix length
12
13 /**
14 * Default constructor.
15 */
16 public Ip4Prefix() {
17 this.address = new Ip4Address();
18 this.prefixLen = 0;
19 }
20
21 /**
22 * Copy constructor.
23 *
24 * @param other the object to copy from
25 */
26 public Ip4Prefix(Ip4Prefix other) {
27 this.address = new Ip4Address(other.address);
28 this.prefixLen = other.prefixLen;
29 }
30
31 /**
32 * Constructor for a given address and prefix length.
33 *
34 * @param address the address to use
35 * @param prefixLen the prefix length to use
36 */
37 public Ip4Prefix(Ip4Address address, short prefixLen) {
38 this.address = Ip4Address.makeMaskedAddress(address, prefixLen);
39 this.prefixLen = prefixLen;
40 }
41
42 /**
43 * Constructs an IPv4 prefix from a string representation of the
44 * prefix.
45 *<p>
46 * Example: "1.2.0.0/16"
47 *
48 * @param value the value to use
49 */
50 public Ip4Prefix(String value) {
51 String[] splits = value.split("/");
52 if (splits.length != 2) {
53 throw new IllegalArgumentException("Specified IPv4 prefix must contain an IPv4 " +
54 "address and a prefix length separated by '/'");
55 }
56 this.prefixLen = Short.decode(splits[1]);
57 this.address = Ip4Address.makeMaskedAddress(new Ip4Address(splits[0]),
58 this.prefixLen);
59 }
60
61 /**
62 * Gets the address value of the IPv4 prefix.
63 *
64 * @return the address value of the IPv4 prefix
65 */
66 public Ip4Address getAddress() {
67 return address;
68 }
69
70 /**
71 * Gets the prefix length value of the IPv4 prefix.
72 *
73 * @return the prefix length value of the IPv4 prefix
74 */
75 public short getPrefixLen() {
76 return prefixLen;
77 }
78
79 /**
80 * Converts the IPv4 prefix value to an "address/prefixLen" string.
81 *
82 * @return the IPv4 prefix value as an "address/prefixLen" string
83 */
84 @Override
85 public String toString() {
86 return this.address.toString() + "/" + this.prefixLen;
87 }
88
89 /**
90 * Compares the value of two Ip4Prefix objects.
91 * <p/>
92 * Note the value of the IPv4 address is compared directly between the
93 * objects, and must match exactly for the objects to be considered equal.
94 * This may result in objects which represent the same IP prefix being
95 * classified as unequal, because the unsignificant bits of the address
96 * field don't match (the bits to the right of the prefix length).
97 * <p/>
98 * TODO Change this behavior so that objects that represent the same prefix
99 * are classified as equal according to this equals method.
100 *
101 * @see Object#equals(Object)
102 */
103 @Override
104 public boolean equals(Object other) {
105 if (other == this) {
106 return true;
107 }
108
109 if (!(other instanceof Ip4Prefix)) {
110 return false;
111 }
112
113 Ip4Prefix otherIp4Prefix = (Ip4Prefix) other;
114
115 return Objects.equals(this.address, otherIp4Prefix.address)
116 && this.prefixLen == otherIp4Prefix.prefixLen;
117 }
118
119 @Override
120 public int hashCode() {
121 return Objects.hash(address, prefixLen);
122 }
123}