Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.util; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 2 | |
Jonathan Hart | 23701d1 | 2014-04-03 10:45:48 -0700 | [diff] [blame] | 3 | import net.onrc.onos.core.util.serializers.IPv4Deserializer; |
| 4 | import net.onrc.onos.core.util.serializers.IPv4Serializer; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 5 | |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 6 | import org.codehaus.jackson.map.annotate.JsonDeserialize; |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 7 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
| 8 | |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 9 | /** |
| 10 | * The class representing an IPv4 address. |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 11 | * This class is immutable. |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 12 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 13 | @JsonDeserialize(using = IPv4Deserializer.class) |
| 14 | @JsonSerialize(using = IPv4Serializer.class) |
Pavlin Radoslavov | 29a2a88 | 2014-04-08 17:40:54 -0700 | [diff] [blame] | 15 | public final class IPv4 { |
| 16 | private final int value; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * Default constructor. |
| 20 | */ |
| 21 | public IPv4() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 22 | this.value = 0; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | /** |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 26 | * Copy constructor. |
| 27 | * |
| 28 | * @param other the object to copy from. |
| 29 | */ |
| 30 | public IPv4(IPv4 other) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 31 | this.value = other.value; |
Pavlin Radoslavov | 1bc2c47 | 2013-07-17 18:11:37 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 35 | * Constructor from an integer value. |
| 36 | * |
| 37 | * @param value the value to use. |
| 38 | */ |
| 39 | public IPv4(int value) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 40 | this.value = value; |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | /** |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 44 | * Constructor from a string. |
| 45 | * |
| 46 | * @param value the value to use. |
| 47 | */ |
| 48 | public IPv4(String value) { |
| 49 | String[] splits = value.split("\\."); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 50 | if (splits.length != 4) { |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 51 | throw new IllegalArgumentException("Specified IPv4 address must contain four " + |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 52 | "numerical digits separated by '.'"); |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 53 | } |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 54 | |
| 55 | int result = 0; |
| 56 | for (int i = 0; i < 4; ++i) { |
Yuta HIGUCHI | 0fe749a | 2014-05-27 09:35:16 -0700 | [diff] [blame] | 57 | result |= Integer.parseInt(splits[i]) << ((3 - i) * 8); |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 58 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 59 | this.value = result; |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 63 | * Get the value of the IPv4 address. |
| 64 | * |
| 65 | * @return the value of the IPv4 address. |
| 66 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 67 | public int value() { |
| 68 | return value; |
| 69 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 70 | |
| 71 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 72 | * 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 78 | return ((this.value >> 24) & 0xFF) + "." + |
| 79 | ((this.value >> 16) & 0xFF) + "." + |
| 80 | ((this.value >> 8) & 0xFF) + "." + |
| 81 | (this.value & 0xFF); |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 82 | } |
Komal Shah | 399a292 | 2014-05-28 01:57:40 -0700 | [diff] [blame] | 83 | |
| 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 Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 100 | } |