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) { |
Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame^] | 13 | if (ip == null) { |
| 14 | throw new NullPointerException("String ip must not be null"); |
| 15 | } |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 16 | if (ip.indexOf('.') != -1) |
| 17 | return IPv4AddressWithMask.of(ip); |
| 18 | else if (ip.indexOf(':') != -1) |
| 19 | return IPv6AddressWithMask.of(ip); |
| 20 | else |
| 21 | throw new IllegalArgumentException("IP Address not well formed: " + ip); |
| 22 | } |
| 23 | |
Gregor Maier | 1ff5597 | 2013-12-11 02:22:56 -0800 | [diff] [blame^] | 24 | @Override |
Gregor Maier | 7f987e6 | 2013-12-10 19:34:18 -0800 | [diff] [blame] | 25 | public String toString() { |
| 26 | StringBuilder res = new StringBuilder(); |
| 27 | res.append(value.toString()); |
| 28 | |
| 29 | res.append('/'); |
| 30 | if (mask.asCidrMaskLength() != -1) { |
| 31 | // CIDR notation |
| 32 | res.append(mask.asCidrMaskLength()); |
| 33 | } else { |
| 34 | // Full address mask |
| 35 | res.append(mask.toString()); |
| 36 | } |
| 37 | |
| 38 | return res.toString(); |
| 39 | } |
| 40 | |
Yotam Harchol | 4d63468 | 2013-09-26 13:21:06 -0700 | [diff] [blame] | 41 | } |