blob: b25099aec192609f0036d062c21a34928b07d59e [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("\\.");
50 if (splits.length != 4)
51 throw new IllegalArgumentException("Specified IPv4 address must contain four " +
Ray Milkey269ffb92014-04-03 14:43:30 -070052 "numerical digits separated by '.'");
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080053
54 int result = 0;
55 for (int i = 0; i < 4; ++i) {
Ray Milkey269ffb92014-04-03 14:43:30 -070056 result |= Integer.valueOf(splits[i]) << ((3 - i) * 8);
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080057 }
Ray Milkey269ffb92014-04-03 14:43:30 -070058 this.value = result;
Pavlin Radoslavov2013cbb2013-02-26 10:15:18 -080059 }
60
61 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080062 * Get the value of the IPv4 address.
63 *
64 * @return the value of the IPv4 address.
65 */
Ray Milkey269ffb92014-04-03 14:43:30 -070066 public int value() {
67 return value;
68 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080069
70 /**
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080071 * Convert the IPv4 value to a '.' separated string.
72 *
73 * @return the IPv4 value as a '.' separated string.
74 */
75 @Override
76 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070077 return ((this.value >> 24) & 0xFF) + "." +
78 ((this.value >> 16) & 0xFF) + "." +
79 ((this.value >> 8) & 0xFF) + "." +
80 (this.value & 0xFF);
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080081 }
82}