Marc De Leenheer
Committed by Gerrit Code Review

PortNumber also backed by port name

Change-Id: Id47781be714bf1efe7d008c8e97ab172177a85f9
......@@ -15,10 +15,10 @@
*/
package org.onosproject.net;
import java.util.Objects;
import com.google.common.primitives.UnsignedLongs;
import java.util.Objects;
/**
* Representation of a port number.
*/
......@@ -48,10 +48,17 @@ public final class PortNumber {
public static final PortNumber CONTROLLER = new PortNumber(CONTROLLER_NUMBER);
private final long number;
private final String name;
// Public creation is prohibited
private PortNumber(long number) {
this.number = number;
this.name = UnsignedLongs.toString(number);
}
private PortNumber(long number, String name) {
this.number = number;
this.name = name;
}
/**
......@@ -75,6 +82,17 @@ public final class PortNumber {
}
/**
* Returns the port number representing the specified long value and name.
*
* @param number port number as long value
* @param name port name as string value
* @return port number
*/
public static PortNumber portNumber(long number, String name) {
return new PortNumber(number, name);
}
/**
* Indicates whether or not this port number is a reserved logical one or
* whether it corresponds to a normal physical port of a device or NIC.
*
......@@ -115,7 +133,7 @@ public final class PortNumber {
@Override
public String toString() {
if (!isLogical()) {
return UnsignedLongs.toString(number);
return name;
} else {
return decodeLogicalPort();
}
......
......@@ -15,12 +15,11 @@
*/
package org.onosproject.net;
import static org.junit.Assert.assertEquals;
import static org.onosproject.net.PortNumber.portNumber;
import com.google.common.testing.EqualsTester;
import org.junit.Test;
import com.google.common.testing.EqualsTester;
import static org.junit.Assert.assertEquals;
import static org.onosproject.net.PortNumber.portNumber;
/**
* Test of the port number.
......@@ -30,9 +29,8 @@ public class PortNumberTest {
@Test
public void basics() {
new EqualsTester()
.addEqualityGroup(portNumber(123),
portNumber("123"))
.addEqualityGroup(portNumber(321))
.addEqualityGroup(portNumber(123), portNumber("123"))
.addEqualityGroup(portNumber(321), portNumber(321, "LIM-3-2-1"))
.testEquals();
}
......