blob: 2ba88c5e4908bb4057563b12f133b2c243729f68 [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.util;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08002
Jonathan Hart23701d12014-04-03 10:45:48 -07003import net.onrc.onos.core.util.serializers.IPv4Deserializer;
4import net.onrc.onos.core.util.serializers.IPv4Serializer;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08005
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -08006import org.codehaus.jackson.map.annotate.JsonDeserialize;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08007import org.codehaus.jackson.map.annotate.JsonSerialize;
8
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08009/**
10 * The class representing an IPv4 address.
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070011 * This class is immutable.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080012 */
Ray Milkey269ffb92014-04-03 14:43:30 -070013@JsonDeserialize(using = IPv4Deserializer.class)
14@JsonSerialize(using = IPv4Serializer.class)
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070015public final class IPv4 {
16 private final int value;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080017
18 /**
19 * Default constructor.
20 */
21 public IPv4() {
Ray Milkey269ffb92014-04-03 14:43:30 -070022 this.value = 0;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080023 }
24
25 /**
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070026 * Copy constructor.
27 *
28 * @param other the object to copy from.
29 */
30 public IPv4(IPv4 other) {
Ray Milkey269ffb92014-04-03 14:43:30 -070031 this.value = other.value;
Pavlin Radoslavov1bc2c472013-07-17 18:11:37 -070032 }
33
34 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080035 * Constructor from an integer value.
36 *
37 * @param value the value to use.
38 */
39 public IPv4(int value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070040 this.value = value;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080041 }
42
43 /**
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080044 * Constructor from a string.
45 *
46 * @param value the value to use.
47 */
48 public IPv4(String value) {
49 String[] splits = value.split("\\.");
Ray Milkeyb29e6262014-04-09 16:02:14 -070050 if (splits.length != 4) {
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080051 throw new IllegalArgumentException("Specified IPv4 address must contain four " +
Ray Milkey269ffb92014-04-03 14:43:30 -070052 "numerical digits separated by '.'");
Ray Milkeyb29e6262014-04-09 16:02:14 -070053 }
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080054
55 int result = 0;
56 for (int i = 0; i < 4; ++i) {
Yuta HIGUCHI0fe749a2014-05-27 09:35:16 -070057 result |= Integer.parseInt(splits[i]) << ((3 - i) * 8);
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080058 }
Ray Milkey269ffb92014-04-03 14:43:30 -070059 this.value = result;
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080060 }
61
62 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080063 * Get the value of the IPv4 address.
64 *
65 * @return the value of the IPv4 address.
66 */
Ray Milkey269ffb92014-04-03 14:43:30 -070067 public int value() {
68 return value;
69 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080070
71 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080072 * Convert the IPv4 value to a '.' separated string.
73 *
74 * @return the IPv4 value as a '.' separated string.
75 */
76 @Override
77 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070078 return ((this.value >> 24) & 0xFF) + "." +
79 ((this.value >> 16) & 0xFF) + "." +
80 ((this.value >> 8) & 0xFF) + "." +
81 (this.value & 0xFF);
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080082 }
Komal Shah399a2922014-05-28 01:57:40 -070083
84 @Override
85 public boolean equals(Object o) {
86 if (!(o instanceof IPv4)) {
87 return false;
88 }
89 IPv4 other = (IPv4) o;
90 if (this.value != other.value) {
91 return false;
92 }
93 return true;
94 }
95
96 @Override
97 public int hashCode() {
98 return this.value;
99 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800100}