Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 3 | |
| 4 | public abstract class IPAddressWithMask<F extends IPAddress<F>> extends Masked<F> { |
| 5 | |
| 6 | protected IPAddressWithMask(F value, F mask) { |
| 7 | super(value, mask); |
| 8 | } |
| 9 | |
Yotam Harchol | eb023dc | 2013-09-26 15:45:44 -0700 | [diff] [blame] | 10 | public abstract IPVersion getIpVersion(); |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 11 | |
| 12 | public static IPAddressWithMask<?> of(String ip) { |
| 13 | if (ip.indexOf('.') != -1) |
| 14 | return IPv4AddressWithMask.of(ip); |
| 15 | else if (ip.indexOf(':') != -1) |
| 16 | return IPv6AddressWithMask.of(ip); |
| 17 | else |
| 18 | throw new IllegalArgumentException("IP Address not well formed: " + ip); |
| 19 | } |
| 20 | |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame^] | 21 | public String toString() { |
| 22 | StringBuilder res = new StringBuilder(); |
| 23 | res.append(value.toString()); |
| 24 | |
| 25 | res.append('/'); |
| 26 | if (mask.asCidrMaskLength() != -1) { |
| 27 | // CIDR notation |
| 28 | res.append(mask.asCidrMaskLength()); |
| 29 | } else { |
| 30 | // Full address mask |
| 31 | res.append(mask.toString()); |
| 32 | } |
| 33 | |
| 34 | return res.toString(); |
| 35 | } |
| 36 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 37 | } |