Change some of the basic classes in the "unit/" directory to immutable:
* Change each class to "final"
* Change the fields to "final"
* Remove the "set" methods
Change-Id: I3499489024403b55328ffb1827fc8c3b5ffd231e
diff --git a/src/main/java/net/onrc/onos/core/util/CallerId.java b/src/main/java/net/onrc/onos/core/util/CallerId.java
index f58d69f..6986f13 100644
--- a/src/main/java/net/onrc/onos/core/util/CallerId.java
+++ b/src/main/java/net/onrc/onos/core/util/CallerId.java
@@ -4,14 +4,16 @@
/**
* The class representing a Caller ID for an ONOS component.
+ * This class is immutable.
*/
-public class CallerId {
- private String value;
+public final class CallerId {
+ private final String value;
/**
* Default constructor.
*/
public CallerId() {
+ this.value = null;
}
/**
@@ -44,16 +46,6 @@
}
/**
- * Set the value of the Caller ID.
- *
- * @param value the value to set.
- */
- @JsonProperty("value")
- public void setValue(String value) {
- this.value = value;
- }
-
- /**
* Convert the Caller ID value to a string.
*
* @return the Caller ID value to a string.
diff --git a/src/main/java/net/onrc/onos/core/util/DataPathEndpoints.java b/src/main/java/net/onrc/onos/core/util/DataPathEndpoints.java
index 70edfee..4e0d40c 100644
--- a/src/main/java/net/onrc/onos/core/util/DataPathEndpoints.java
+++ b/src/main/java/net/onrc/onos/core/util/DataPathEndpoints.java
@@ -5,15 +5,18 @@
/**
* The class representing the Data Path Endpoints.
+ * This class is immutable.
*/
-public class DataPathEndpoints {
- private SwitchPort srcPort; // The source port
- private SwitchPort dstPort; // The destination port
+public final class DataPathEndpoints {
+ private final SwitchPort srcPort; // The source port
+ private final SwitchPort dstPort; // The destination port
/**
* Default constructor.
*/
public DataPathEndpoints() {
+ srcPort = null;
+ dstPort = null;
}
/**
@@ -38,16 +41,6 @@
}
/**
- * Set the data path source port.
- *
- * @param srcPort the data path source port to set.
- */
- @JsonProperty("srcPort")
- public void setSrcPort(SwitchPort srcPort) {
- this.srcPort = srcPort;
- }
-
- /**
* Get the data path destination port.
*
* @return the data path destination port.
@@ -58,16 +51,6 @@
}
/**
- * Set the data path destination port.
- *
- * @param dstPort the data path destination port to set.
- */
- @JsonProperty("dstPort")
- public void setDstPort(SwitchPort dstPort) {
- this.dstPort = dstPort;
- }
-
- /**
* Convert the data path endpoints to a string.
* <p/>
* The string has the following form:
diff --git a/src/main/java/net/onrc/onos/core/util/Dpid.java b/src/main/java/net/onrc/onos/core/util/Dpid.java
index 80ebc15..f66a5d4 100644
--- a/src/main/java/net/onrc/onos/core/util/Dpid.java
+++ b/src/main/java/net/onrc/onos/core/util/Dpid.java
@@ -9,13 +9,13 @@
/**
* The class representing a network switch DPID.
+ * This class is immutable.
*/
@JsonDeserialize(using = DpidDeserializer.class)
@JsonSerialize(using = DpidSerializer.class)
-public class Dpid {
- static public final long UNKNOWN = 0;
-
- private long value;
+public final class Dpid {
+ private final static long UNKNOWN = 0;
+ private final long value;
/**
* Default constructor.
@@ -52,15 +52,6 @@
}
/**
- * Set the value of the DPID.
- *
- * @param value the value to set.
- */
- public void setValue(long value) {
- this.value = value;
- }
-
- /**
* Convert the DPID value to a ':' separated hexadecimal string.
*
* @return the DPID value as a ':' separated hexadecimal string.
diff --git a/src/main/java/net/onrc/onos/core/util/FlowEntryId.java b/src/main/java/net/onrc/onos/core/util/FlowEntryId.java
index 373962e..62c009a 100644
--- a/src/main/java/net/onrc/onos/core/util/FlowEntryId.java
+++ b/src/main/java/net/onrc/onos/core/util/FlowEntryId.java
@@ -11,17 +11,19 @@
/**
* The class representing a Flow Entry ID.
+ * This class is immutable.
*/
@JsonDeserialize(using = FlowEntryIdDeserializer.class)
@JsonSerialize(using = FlowEntryIdSerializer.class)
-public class FlowEntryId {
- private long value;
+public final class FlowEntryId {
+ private final static long INVALID = -1;
+ private final long value;
/**
* Default constructor.
*/
public FlowEntryId() {
- this.value = -1;
+ this.value = FlowEntryId.INVALID;
}
/**
@@ -62,22 +64,13 @@
}
/**
- * Set the value of the Flow Entry ID.
- *
- * @param value the value to set.
- */
- public void setValue(long value) {
- this.value = value;
- }
-
- /**
* Test whether the Flow Entry ID is valid.
*
* @return true if the Flow Entry ID is valid, otherwise false.
*/
@JsonIgnore
public boolean isValid() {
- return (this.value() != -1);
+ return (this.value() != FlowEntryId.INVALID);
}
/**
diff --git a/src/main/java/net/onrc/onos/core/util/FlowId.java b/src/main/java/net/onrc/onos/core/util/FlowId.java
index c5ee39d..5eace27 100644
--- a/src/main/java/net/onrc/onos/core/util/FlowId.java
+++ b/src/main/java/net/onrc/onos/core/util/FlowId.java
@@ -11,17 +11,19 @@
/**
* The class representing a Flow ID.
+ * This class is immutable.
*/
@JsonDeserialize(using = FlowIdDeserializer.class)
@JsonSerialize(using = FlowIdSerializer.class)
-public class FlowId implements Comparable<FlowId> {
- private long value;
+public final class FlowId implements Comparable<FlowId> {
+ private final static long INVALID = -1;
+ private final long value;
/**
* Default constructor.
*/
public FlowId() {
- this.value = -1;
+ this.value = FlowId.INVALID;
}
/**
@@ -62,22 +64,13 @@
}
/**
- * Set the value of the Flow ID.
- *
- * @param value the value to set.
- */
- public void setValue(long value) {
- this.value = value;
- }
-
- /**
* Test whether the Flow ID is valid.
*
* @return true if the Flow ID is valid, otherwise false.
*/
@JsonIgnore
public boolean isValid() {
- return (this.value() != -1);
+ return (this.value() != FlowId.INVALID);
}
/**
diff --git a/src/main/java/net/onrc/onos/core/util/IPv4.java b/src/main/java/net/onrc/onos/core/util/IPv4.java
index a7f3d24..b25099a 100644
--- a/src/main/java/net/onrc/onos/core/util/IPv4.java
+++ b/src/main/java/net/onrc/onos/core/util/IPv4.java
@@ -8,11 +8,12 @@
/**
* The class representing an IPv4 address.
+ * This class is immutable.
*/
@JsonDeserialize(using = IPv4Deserializer.class)
@JsonSerialize(using = IPv4Serializer.class)
-public class IPv4 {
- private int value;
+public final class IPv4 {
+ private final int value;
/**
* Default constructor.
@@ -67,15 +68,6 @@
}
/**
- * Set the value of the IPv4 address.
- *
- * @param value the value to set.
- */
- public void setValue(int value) {
- this.value = value;
- }
-
- /**
* Convert the IPv4 value to a '.' separated string.
*
* @return the IPv4 value as a '.' separated string.
diff --git a/src/main/java/net/onrc/onos/core/util/IPv4Net.java b/src/main/java/net/onrc/onos/core/util/IPv4Net.java
index 3aa5a8b..6a04635 100644
--- a/src/main/java/net/onrc/onos/core/util/IPv4Net.java
+++ b/src/main/java/net/onrc/onos/core/util/IPv4Net.java
@@ -8,17 +8,19 @@
/**
* The class representing an IPv4 network address.
+ * This class is immutable.
*/
@JsonDeserialize(using = IPv4NetDeserializer.class)
@JsonSerialize(using = IPv4NetSerializer.class)
-public class IPv4Net {
- private IPv4 address; // The IPv4 address
- private short prefixLen; // The prefix length
+public final class IPv4Net {
+ private final IPv4 address; // The IPv4 address
+ private final short prefixLen; // The prefix length
/**
* Default constructor.
*/
public IPv4Net() {
+ this.address = null;
this.prefixLen = 0;
}
@@ -30,6 +32,8 @@
public IPv4Net(IPv4Net other) {
if (other.address != null)
this.address = new IPv4(other.address);
+ else
+ this.address = null;
this.prefixLen = other.prefixLen;
}
@@ -69,15 +73,6 @@
}
/**
- * Set the address value of the IPv4Net address.
- *
- * @param address the address to use.
- */
- public void setAddress(IPv4 address) {
- this.address = address;
- }
-
- /**
* Get the prefix length value of the IPv4Net address.
*
* @return the prefix length value of the IPv4Net address.
@@ -87,26 +82,6 @@
}
/**
- * Set the prefix length value of the IPv4Net address.
- *
- * @param prefixLen the prefix length to use.
- */
- public void setPrefixLen(short prefixLen) {
- this.prefixLen = prefixLen;
- }
-
- /**
- * Set the value of the IPv4Net address.
- *
- * @param address the address to use.
- * @param prefixLen the prefix length to use.
- */
- public void setValue(IPv4 address, short prefixLen) {
- this.address = address;
- this.prefixLen = prefixLen;
- }
-
- /**
* Convert the IPv4Net value to an "address/prefixLen" string.
*
* @return the IPv4Net value as an "address/prefixLen" string.
diff --git a/src/main/java/net/onrc/onos/core/util/IPv6.java b/src/main/java/net/onrc/onos/core/util/IPv6.java
index 38a86b5..986fd11 100644
--- a/src/main/java/net/onrc/onos/core/util/IPv6.java
+++ b/src/main/java/net/onrc/onos/core/util/IPv6.java
@@ -9,12 +9,13 @@
/**
* The class representing an IPv6 address.
+ * This class is immutable.
*/
@JsonDeserialize(using = IPv6Deserializer.class)
@JsonSerialize(using = IPv6Serializer.class)
-public class IPv6 {
- private long valueHigh; // The higher (more significant) 64 bits
- private long valueLow; // The lower (less significant) 64 bits
+public final class IPv6 {
+ private final long valueHigh; // The higher (more significant) 64 bits
+ private final long valueLow; // The lower (less significant) 64 bits
/**
* Default constructor.
@@ -67,15 +68,6 @@
}
/**
- * Set the value of the higher (more significant) 64 bits of the address.
- *
- * @param valueHigh the higher (more significant) 64 bits of the address.
- */
- public void setValueHigh(long valueHigh) {
- this.valueHigh = valueHigh;
- }
-
- /**
* Get the value of the lower (less significant) 64 bits of the address.
*
* @return the value of the lower (less significant) 64 bits of the
@@ -86,26 +78,6 @@
}
/**
- * Get the value of the lower (less significant) 64 bits of the address.
- *
- * @param valueLow the lower (less significant) 64 bits of the address.
- */
- public void setValueLow(long valueLow) {
- this.valueLow = valueLow;
- }
-
- /**
- * Set the value of the IPv6 address.
- *
- * @param valueHigh the higher (more significant) 64 bits of the address.
- * @param valueLow the lower (less significant) 64 bits of the address.
- */
- public void setValue(long valueHigh, long valueLow) {
- this.valueHigh = valueHigh;
- this.valueLow = valueLow;
- }
-
- /**
* Convert the IPv6 value to a ':' separated string.
*
* @return the IPv6 value as a ':' separated string.
diff --git a/src/main/java/net/onrc/onos/core/util/IPv6Net.java b/src/main/java/net/onrc/onos/core/util/IPv6Net.java
index 1faa7c5..72ec5e9 100644
--- a/src/main/java/net/onrc/onos/core/util/IPv6Net.java
+++ b/src/main/java/net/onrc/onos/core/util/IPv6Net.java
@@ -8,17 +8,19 @@
/**
* The class representing an IPv6 network address.
+ * This class is immutable.
*/
@JsonDeserialize(using = IPv6NetDeserializer.class)
@JsonSerialize(using = IPv6NetSerializer.class)
-public class IPv6Net {
- private IPv6 address; // The IPv6 address
- private short prefixLen; // The prefix length
+public final class IPv6Net {
+ private final IPv6 address; // The IPv6 address
+ private final short prefixLen; // The prefix length
/**
* Default constructor.
*/
public IPv6Net() {
+ this.address = null;
this.prefixLen = 0;
}
@@ -30,6 +32,8 @@
public IPv6Net(IPv6Net other) {
if (other.address != null)
this.address = new IPv6(other.address);
+ else
+ this.address = null;
this.prefixLen = other.prefixLen;
}
@@ -69,15 +73,6 @@
}
/**
- * Set the address value of the IPv6Net address.
- *
- * @param address the address to use.
- */
- public void setAddress(IPv6 address) {
- this.address = address;
- }
-
- /**
* Get the prefix length value of the IPv6Net address.
*
* @return the prefix length value of the IPv6Net address.
@@ -87,26 +82,6 @@
}
/**
- * Set the prefix length value of the IPv6Net address.
- *
- * @param prefixLen the prefix length to use.
- */
- public void setPrefixLen(short prefixLen) {
- this.prefixLen = prefixLen;
- }
-
- /**
- * Set the value of the IPv6Net address.
- *
- * @param address the address to use.
- * @param prefixLen the prefix length to use.
- */
- public void setValue(IPv6 address, short prefixLen) {
- this.address = address;
- this.prefixLen = prefixLen;
- }
-
- /**
* Convert the IPv6Net value to an "address/prefixLen" string.
*
* @return the IPv6Net value as an "address/prefixLen" string.
diff --git a/src/main/java/net/onrc/onos/core/util/Port.java b/src/main/java/net/onrc/onos/core/util/Port.java
index 77d983b..20a2a21 100644
--- a/src/main/java/net/onrc/onos/core/util/Port.java
+++ b/src/main/java/net/onrc/onos/core/util/Port.java
@@ -4,9 +4,9 @@
/**
* The class representing a network port of a switch.
+ * This class is immutable.
*/
-
-public class Port {
+public final class Port {
/**
* Special port values.
* <p/>
@@ -70,7 +70,7 @@
}
}
- private short value;
+ private final short value;
/**
* Default constructor.
@@ -117,16 +117,6 @@
}
/**
- * Set the value of the port.
- *
- * @param value the value to set.
- */
- @JsonProperty("value")
- public void setValue(short value) {
- this.value = value;
- }
-
- /**
* Convert the port value to a string.
*
* @return the port value as a string.
@@ -136,7 +126,6 @@
return Short.toString(this.value);
}
-
@Override
public boolean equals(Object other) {
if (!(other instanceof Port)) {
diff --git a/src/main/java/net/onrc/onos/core/util/Switch.java b/src/main/java/net/onrc/onos/core/util/Switch.java
index d1a463c..8ea1184 100644
--- a/src/main/java/net/onrc/onos/core/util/Switch.java
+++ b/src/main/java/net/onrc/onos/core/util/Switch.java
@@ -4,9 +4,10 @@
/**
* The class representing a Switch.
+ * This class is almost immutable: the switch state is mutable.
* NOTE: Currently this class is (almost) not used.
*/
-public class Switch {
+public final class Switch {
/**
* The Switch state.
*/
@@ -15,8 +16,8 @@
ACTIVE,
}
- private Dpid dpid; // The DPID of the switch
- private SwitchState state; // The state of the switch
+ private final Dpid dpid; // The DPID of the switch
+ private SwitchState state; // The state of the switch
/**
* Default constructor.
@@ -62,16 +63,6 @@
}
/**
- * Set the DPID.
- *
- * @param dpid the DPID to use.
- */
- @JsonProperty("dpid")
- public void setDpid(Dpid dpid) {
- this.dpid = dpid;
- }
-
- /**
* Get the state.
*
* @return the state.
diff --git a/src/main/java/net/onrc/onos/core/util/SwitchPort.java b/src/main/java/net/onrc/onos/core/util/SwitchPort.java
index eb72b72..9178267 100644
--- a/src/main/java/net/onrc/onos/core/util/SwitchPort.java
+++ b/src/main/java/net/onrc/onos/core/util/SwitchPort.java
@@ -4,15 +4,18 @@
/**
* The class representing a Switch-Port.
+ * This class is immutable.
*/
-public class SwitchPort {
- private Dpid dpid; // The DPID of the switch
- private Port port; // The port of the switch
+public final class SwitchPort {
+ private final Dpid dpid; // The DPID of the switch
+ private final Port port; // The port of the switch
/**
* Default constructor.
*/
public SwitchPort() {
+ this.dpid = null;
+ this.port = null;
}
/**
@@ -37,16 +40,6 @@
}
/**
- * Set the DPID value of the Switch-Port.
- *
- * @param dpid the DPID to use.
- */
- @JsonProperty("dpid")
- public void setDpid(Dpid dpid) {
- this.dpid = dpid;
- }
-
- /**
* Get the port value of the Switch-Port.
*
* @return the port value of the Switch-Port.
@@ -57,27 +50,6 @@
}
/**
- * Set the port value of the Switch-Port.
- *
- * @param port the port to use.
- */
- @JsonProperty("port")
- public void setPort(Port port) {
- this.port = port;
- }
-
- /**
- * Set the DPID and port values of the Switch-Port.
- *
- * @param dpid the DPID to use.
- * @param port the port to use.
- */
- public void setValue(Dpid dpid, Port port) {
- this.dpid = dpid;
- this.port = port;
- }
-
- /**
* Convert the Switch-Port value to a string.
* <p/>
* The string has the following form:
@@ -90,7 +62,6 @@
return this.dpid.toString() + "/" + this.port;
}
-
@Override
public boolean equals(Object other) {
if (!(other instanceof SwitchPort)) {