Skip to content

Commit

Permalink
Reduce NPath complexity of AKMAlias.matches
Browse files Browse the repository at this point in the history
This should reduce NPath complexity of `AKMAlias.matches` method without changing its behavior. This was verified using the previously added unit test that should cover all relevant cases.

Signed-off-by: Elv1zz <[email protected]>
  • Loading branch information
Elv1zz committed Oct 27, 2023
1 parent dcc52f5 commit 89ff8c4
Showing 1 changed file with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -755,27 +755,26 @@ public boolean equals(Object object) {
* Exception: both hostname fields are resolved to an ip address before comparing if possible.
*/
public boolean matches(@NonNull AKMAlias filter) {
if (filter.type != null && !filter.type.equals(type)) {
Log_OC.d(TAG, "matches: alias " + this + " does not match type " + filter.type);
return false;
}
if (filter.alias != null && !filter.alias.equals(alias)) {
Log_OC.d(TAG, "matches: alias " + this + " does not match original alias " + filter.alias);
return false;
}
if (hostname != null && filter.hostname != null && !filter.hostname.equals(hostname)) {
boolean matches = isNullOrEqual(filter.type, type, "matches: alias " + this + " does not match type " + filter.type);
matches &= isNullOrEqual(filter.alias, alias, "matches: alias " + this + " does not match original alias " + filter.alias);
if (matches && hostname != null && filter.hostname != null && !filter.hostname.equals(hostname)) {
// Resolve hostname fields to ip addresses
InetAddress address = getInetAddressByName(hostname);
InetAddress filterAddress = getInetAddressByName(filter.hostname);
// If resolution succeeded, compare addresses, otherwise host names
if ((address == null || !address.equals(filterAddress))) {
Log_OC.d(TAG, "matches: alias " + this + " (address=" + address + ") does not match hostname " +
filter.hostname + " (address=" + filterAddress + ")");
return false;
matches = false;
}
}
if (port != null && filter.port != null && !filter.port.equals(port)) {
Log_OC.d(TAG, "matches: alias " + this + " does not match port " + filter.port);
matches &= isNullOrEqual(filter.port, port, "matches: alias " + this + " does not match port " + filter.port);
return matches;
}

private boolean isNullOrEqual(Object a, Object b, String message) {
if (a != null && !a.equals(b)) {
Log_OC.d(TAG, message);
return false;
}
return true;
Expand Down

0 comments on commit 89ff8c4

Please sign in to comment.