com.ionos.network.commons.address.MACParsers Maven / Gradle / Ivy
package com.ionos.network.commons.address;
/**
* Parser implementations for MAC addresses.
* @see MAC
* @author Stephan Fuhrmann
* */
public final class MACParsers {
private abstract static class AbstractMacParser
implements AddressParser {
public MAC parse(final String address) {
return new MAC(parseAsBytes(address));
}
}
/**
* Parses a MAC address in every possible known format.
* @see #CISCO_CUSTOM_FORMAT
* @see #COLON_SEPARATED_HEX_FORMAT
*/
public static final AddressParser DEFAULT = new AbstractMacParser() {
@Override
public byte[] parseAsBytes(final String address) {
byte[] result;
try {
result = COLON_SEPARATED_HEX_FORMAT.parseAsBytes(address);
} catch (IllegalArgumentException e) {
result = CISCO_CUSTOM_FORMAT.parseAsBytes(address);
}
return result;
}
};
/**
* Parses a single byte hex representation of the MAC.
*
* Example: {@code 00:ac:ac:dd:00:12}.
* @see MACFormats#COLON_SEPARATED_HEX_FORMAT
*/
public static final AddressParser COLON_SEPARATED_HEX_FORMAT =
new HexadecimalAddressParser<>(':',
2,
MAC::new,
MAC.MAC_LENGTH);
/**
* Parses a word hex representation of the MAC in CISCO style.
*
* Example:{@code 00ac.acdd.0012}.
* @see MACFormats#CISCO_CUSTOM_FORMAT
*/
public static final AddressParser CISCO_CUSTOM_FORMAT =
new HexadecimalAddressParser<>('.',
4,
MAC::new,
MAC.MAC_LENGTH);
/**
* Private constructor. No instance allowed.
*/
private MACParsers() {
// no instance allowed
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy