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("\\."); |
| 50 | if (splits.length != 4) |
| 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 '.'"); |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 53 | |
| 54 | int result = 0; |
| 55 | for (int i = 0; i < 4; ++i) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 56 | result |= Integer.valueOf(splits[i]) << ((3 - i) * 8); |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 57 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 58 | this.value = result; |
Pavlin Radoslavov | 2013cbb | 2013-02-26 10:15:18 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 62 | * Get the value of the IPv4 address. |
| 63 | * |
| 64 | * @return the value of the IPv4 address. |
| 65 | */ |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | public int value() { |
| 67 | return value; |
| 68 | } |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 69 | |
| 70 | /** |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 71 | * 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 Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 77 | return ((this.value >> 24) & 0xFF) + "." + |
| 78 | ((this.value >> 16) & 0xFF) + "." + |
| 79 | ((this.value >> 8) & 0xFF) + "." + |
| 80 | (this.value & 0xFF); |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 81 | } |
| 82 | } |