Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 1 | package net.floodlightcontroller.util; |
| 2 | |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 3 | import net.floodlightcontroller.util.serializers.IPv4Serializer; |
| 4 | |
| 5 | import org.codehaus.jackson.annotate.JsonProperty; |
| 6 | import org.codehaus.jackson.map.annotate.JsonSerialize; |
| 7 | |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 8 | /** |
| 9 | * The class representing an IPv4 address. |
| 10 | */ |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 11 | @JsonSerialize(using=IPv4Serializer.class) |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 12 | public class IPv4 { |
| 13 | private int value; |
| 14 | |
| 15 | /** |
| 16 | * Default constructor. |
| 17 | */ |
| 18 | public IPv4() { |
| 19 | this.value = 0; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Constructor from an integer value. |
| 24 | * |
| 25 | * @param value the value to use. |
| 26 | */ |
| 27 | public IPv4(int value) { |
| 28 | this.value = value; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Get the value of the IPv4 address. |
| 33 | * |
| 34 | * @return the value of the IPv4 address. |
| 35 | */ |
| 36 | public int value() { return value; } |
| 37 | |
| 38 | /** |
| 39 | * Set the value of the IPv4 address. |
| 40 | * |
| 41 | * @param value the value to set. |
| 42 | */ |
| 43 | public void setValue(int value) { |
| 44 | this.value = value; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Convert the IPv4 value to a '.' separated string. |
| 49 | * |
| 50 | * @return the IPv4 value as a '.' separated string. |
| 51 | */ |
| 52 | @Override |
| 53 | public String toString() { |
Pavlin Radoslavov | ad008e0 | 2013-02-21 18:42:42 -0800 | [diff] [blame] | 54 | return ((this.value >> 24) & 0xFF) + "." + |
| 55 | ((this.value >> 16) & 0xFF) + "." + |
| 56 | ((this.value >> 8) & 0xFF) + "." + |
| 57 | (this.value & 0xFF); |
Pavlin Radoslavov | 5363c2a | 2013-02-18 09:55:42 -0800 | [diff] [blame] | 58 | } |
| 59 | } |