public class MACAddressString extends Object implements HostIdentifierString, Comparable<MACAddressString>
This supports a wide range of address formats and provides specific error messages, and allows specific configuration.
You can control all of the supported formats using MACAddressStringParameters.Builder
to build a parameters instance of MACAddressStringParameters
.
When not using the constructor that takes a MACAddressStringParameters
, a default instance of MACAddressStringParameters
is used that is generally permissive.
Ranges are supported:
The different methods of representing MAC addresses are supported:
All of the above range variations also work for each of these ways of representing MAC addresses.
Some additional formats:
MACAddress
address = new MACAddressString
("1.2.3.4").getAddress()
;
If your application takes user input IP addresses, you can validate with:
try {
MACAddress
address = new MACAddressString("1.2.3.4").toAddress()
;
} catch(AddressStringException
e) {
//e.getMessage() provides description of validation failure
}
For empty addresses, both toAddress()
and getAddress()
returns null. For invalid addresses, getAddress()
returns null.
This class is thread-safe. In fact, MACAddressString objects are immutable. A MACAddressString object represents a single MAC address representation that cannot be changed after construction. Some of the derived state is created upon demand and cached, such as the derived MACAddress instances.
Modifier and Type | Field and Description |
---|---|
static MACAddressString |
ALL_ADDRESSES |
static MACAddressString |
EMPTY_ADDRESS |
SEGMENT_VALUE_DELIMITER
Constructor and Description |
---|
MACAddressString(MACAddress address) |
MACAddressString(String addr)
Constructs an MACAddressString instance using the given String instance.
|
MACAddressString(String addr,
MACAddressStringParameters valOptions) |
Modifier and Type | Method and Description |
---|---|
int |
compareTo(MACAddressString other) |
static int |
countDelimitedAddresses(String str)
Given a string with comma delimiters to denote segment elements, this method will count the possible combinations.
|
boolean |
equals(Object o)
Two MACAddressString objects are equal if they represent the same set of addresses.
|
MACAddress |
getAddress()
Produces the
MACAddress corresponding to this MACAddressString. |
Integer |
getPrefixLength() |
MACAddressStringParameters |
getValidationOptions() |
int |
hashCode() |
boolean |
isAllAddresses() |
boolean |
isEmpty()
Returns true if the address is empty (zero-length).
|
boolean |
isPrefixed() |
boolean |
isValid() |
boolean |
isZero() |
static Iterator<String> |
parseDelimitedSegments(String str)
Given a string with comma delimiters to denote segment elements, this method will provide an iterator to iterate through the possible combinations.
|
MACAddress |
toAddress()
Produces the
MACAddress corresponding to this MACAddressString. |
String |
toNormalizedString()
provides a normalized String representation for the host identified by this HostIdentifierString instance
|
String |
toString()
Gives us the original string provided to the constructor.
|
void |
validate()
Validates this string is a valid address, and if not, throws an exception with a descriptive message indicating why it is not.
|
public static final MACAddressString EMPTY_ADDRESS
public static final MACAddressString ALL_ADDRESSES
public MACAddressString(String addr)
addr
- the address in string format, in some valid MAC address form.
You can also alter the addresses to include ranges using the wildcards * and -, such as 1:*:1-2:3:4:5.
public MACAddressString(String addr, MACAddressStringParameters valOptions)
addr
- the address in string format
This constructor allows you to alter the default validation options.public MACAddressString(MACAddress address)
public MACAddressStringParameters getValidationOptions()
public boolean isPrefixed()
public Integer getPrefixLength()
public boolean isAllAddresses()
public boolean isEmpty()
public boolean isZero()
public boolean isValid()
public void validate() throws AddressStringException
validate
in interface HostIdentifierString
AddressStringException
public int compareTo(MACAddressString other)
compareTo
in interface Comparable<MACAddressString>
public boolean equals(Object o)
public MACAddress getAddress()
MACAddress
corresponding to this MACAddressString.
If this object does not represent a specific MACAddress or a ranged MACAddress,
or if the string used to construct this object is not a known format, null is returned.
It is equivalent to toAddress()
except for the fact that it does not throw AddressStringException for invalid address formats.getAddress
in interface HostIdentifierString
public MACAddress toAddress() throws AddressStringException, IncompatibleAddressException
MACAddress
corresponding to this MACAddressString. If this object does not represent a specific MACAddress or a ranged MACAddress, null is returned,
which may be the case if this object represents the empty address string.
If the string used to construct this object is not a known format then this method throws AddressStringException, unlike the equivalent method getAddress()
which simply returns null in such cases.
As long as this object represents a valid address (but not necessarily a specific address), this method does not throw.toAddress
in interface HostIdentifierString
AddressStringException
- if the address format is invalidIncompatibleAddressException
- if a valid address string representing multiple addresses cannot be representedpublic String toNormalizedString()
HostIdentifierString
toNormalizedString
in interface HostIdentifierString
public String toString()
getAddress()
/toAddress()
and then use string methods on the address object.toString
in interface HostIdentifierString
toString
in class Object
public static int countDelimitedAddresses(String str)
str
- public static Iterator<String> parseDelimitedSegments(String str)
str
-