blob: 7273dea0c9cb59f34351b8a47d5f64ae8555d44b [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.util;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08002
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08003import org.codehaus.jackson.annotate.JsonProperty;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08004
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08005/**
Yuta HIGUCHIfb564502014-06-16 21:29:00 -07006 * Immutable class representing a port number.
7 * <p/>
8 * Current implementation supports only OpenFlow 1.0 (16 bit unsigned) port number.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08009 */
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070010public final class PortNumber {
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080011
Pavlin Radoslavov29a2a882014-04-08 17:40:54 -070012 private final short value;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080013
Jonathan Hart3edb1752013-11-14 13:28:17 -080014 /**
15 * Default constructor.
16 */
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070017 protected PortNumber() {
Ray Milkey269ffb92014-04-03 14:43:30 -070018 this.value = 0;
Jonathan Hart3edb1752013-11-14 13:28:17 -080019 }
Pavlin Radoslavovf83aa442013-02-26 14:09:01 -080020
Jonathan Hart3edb1752013-11-14 13:28:17 -080021 /**
22 * Copy constructor.
23 *
24 * @param other the object to copy from.
25 */
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070026 public PortNumber(PortNumber other) {
Ray Milkey269ffb92014-04-03 14:43:30 -070027 this.value = other.value();
Jonathan Hart3edb1752013-11-14 13:28:17 -080028 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080029
Jonathan Hart3edb1752013-11-14 13:28:17 -080030 /**
31 * Constructor from a short integer value.
32 *
33 * @param value the value to use.
34 */
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070035 public PortNumber(short value) {
Ray Milkey269ffb92014-04-03 14:43:30 -070036 this.value = value;
Jonathan Hart3edb1752013-11-14 13:28:17 -080037 }
Pavlin Radoslavovede97582013-03-08 18:57:28 -080038
Jonathan Hart3edb1752013-11-14 13:28:17 -080039 /**
Jonathan Hart3edb1752013-11-14 13:28:17 -080040 * Get the value of the port.
41 *
42 * @return the value of the port.
43 */
44 @JsonProperty("value")
Ray Milkey269ffb92014-04-03 14:43:30 -070045 public short value() {
46 return value;
47 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080048
Jonathan Hart3edb1752013-11-14 13:28:17 -080049 /**
Jonathan Hart3edb1752013-11-14 13:28:17 -080050 * Convert the port value to a string.
51 *
52 * @return the port value as a string.
53 */
54 @Override
55 public String toString() {
Ray Milkey269ffb92014-04-03 14:43:30 -070056 return Short.toString(this.value);
Jonathan Hart3edb1752013-11-14 13:28:17 -080057 }
Ray Milkey269ffb92014-04-03 14:43:30 -070058
Jonathan Hart3edb1752013-11-14 13:28:17 -080059 @Override
60 public boolean equals(Object other) {
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070061 if (!(other instanceof PortNumber)) {
Ray Milkey269ffb92014-04-03 14:43:30 -070062 return false;
63 }
Jonathan Hart3edb1752013-11-14 13:28:17 -080064
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070065 PortNumber otherPort = (PortNumber) other;
Jonathan Hart3edb1752013-11-14 13:28:17 -080066
Ray Milkey269ffb92014-04-03 14:43:30 -070067 return value == otherPort.value;
Jonathan Hart3edb1752013-11-14 13:28:17 -080068 }
69
70 @Override
71 public int hashCode() {
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070072 return value;
Jonathan Hart3edb1752013-11-14 13:28:17 -080073 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080074}