Skip to content

Commit

Permalink
adding Port abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanCheshire committed Nov 7, 2023
1 parent 2e06afd commit dda12f9
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/main/java/cyder/network/CommonServicePort.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,13 @@ public String getAcronym() {
public String getDescription() {
return description;
}

/**
* Constructs and returns a new {@link Port} object using this port.
*
* @return a new {@link Port} object using this port.
*/
public Port constructPort() {
return new Port(this.port);
}
}
10 changes: 4 additions & 6 deletions src/main/java/cyder/network/HighLatencyChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
* An object which pings a provided ip:port and determines a status based on a provided tolerance.
*/
public final class HighLatencyChecker {
/**
* The range a general computer port must fall into.
*/
public static final Range<Integer> portRange = Range.closed(1024, 65535);


/**
* The IP address this checker will ping.
Expand Down Expand Up @@ -49,7 +46,8 @@ public static final class Builder {
DEFAULT_REMOTE_NAME + " unreachable");

private String ipAddress;
private int port;
private Port port;

private LatencyCategorizer latencyCategorizer;
private String remoteName;

Expand All @@ -59,7 +57,7 @@ public static final class Builder {
*/
public Builder() {
this.ipAddress = DEFAULT_IP_ADDRESS;
this.port = DEFAULT_PORT;
this.port = new Port(DEFAULT_PORT);
this.remoteName = DEFAULT_REMOTE_NAME;
this.latencyCategorizer = DEFAULT_LATENCY_CATEGORIZER;
}
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/cyder/network/Port.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package cyder.network;

import com.google.common.base.Preconditions;
import com.google.common.collect.Range;

public class Port {
/**
* The range a general computer port must fall into.
*/
public static final Range<Integer> portRange = Range.closed(1024, 65535);

/**
* The encapsulated port.
*/
private final int port;

/**
* Constructs a new Port object from the provided {@link CommonServicePort}.
*
* @param commonServicePort the common service port
*/
public Port(CommonServicePort commonServicePort) {
this.port = commonServicePort.getPort();
}

/**
* Constructs a new Port object from the provided port number.
*
* @param port the port number
* @throws IllegalArgumentException if the provided port is outside of the range {@link #portRange}.
*/
public Port(int port) {
Preconditions.checkArgument(portRange.contains(port));
this.port = port;
}

/**
* {@inheritDoc}
*/
@Override
public String toString() {
return "Port{port=" + port + "}";
}

/**
* {@inheritDoc}
*/
@Override
public int hashCode() {
return Integer.hashCode(port);
}

/**
* {@inheritDoc}
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
} else if (!(o instanceof Port)) {
return false;
}

Port other = (Port) o;
return other.port == port;
}
}

0 comments on commit dda12f9

Please sign in to comment.