blob: a9b42b06dfdbe3ca2ec72aa2dc9ab920b95049b4 [file] [log] [blame]
Jonathan Hart23701d12014-04-03 10:45:48 -07001package net.onrc.onos.core.util;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08002
Yuta HIGUCHI6d7ee9f2014-08-22 09:56:50 -07003import javax.annotation.concurrent.Immutable;
4
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08005import org.codehaus.jackson.annotate.JsonProperty;
Pavlin Radoslavovad008e02013-02-21 18:42:42 -08006
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -07007import com.google.common.primitives.UnsignedInts;
8
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -08009/**
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070010 * Immutable class representing a port number.
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080011 */
Yuta HIGUCHI6d7ee9f2014-08-22 09:56:50 -070012@Immutable
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070013public final class PortNumber {
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080014
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -070015 private final int value;
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080016
Jonathan Hart3edb1752013-11-14 13:28:17 -080017 /**
18 * Default constructor.
19 */
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070020 protected PortNumber() {
Ray Milkey269ffb92014-04-03 14:43:30 -070021 this.value = 0;
Jonathan Hart3edb1752013-11-14 13:28:17 -080022 }
Pavlin Radoslavovf83aa442013-02-26 14:09:01 -080023
Jonathan Hart3edb1752013-11-14 13:28:17 -080024 /**
25 * Copy constructor.
26 *
27 * @param other the object to copy from.
28 */
Yuta HIGUCHIfb564502014-06-16 21:29:00 -070029 public PortNumber(PortNumber other) {
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -070030 this.value = other.value;
Jonathan Hart3edb1752013-11-14 13:28:17 -080031 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -080032
Jonathan Hart3edb1752013-11-14 13:28:17 -080033 /**
34 * Constructor from a short integer value.
35 *
36 * @param value the value to use.
37 */
Yuta HIGUCHIa507baf2014-08-22 13:42:40 -070038 protected PortNumber(short value) {
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -070039 this.value = (int) shortToUnsignedLong(value);
Jonathan Hart3edb1752013-11-14 13:28:17 -080040 }
Pavlin Radoslavovede97582013-03-08 18:57:28 -080041
Jonathan Hart3edb1752013-11-14 13:28:17 -080042 /**
Yuta HIGUCHI5865bdb2014-07-30 09:32:13 -070043 * Creates the unsigned 16 bit port number.
44 *
45 * @param number unsigned 16 bit port number.
46 * @return PortNumber instance
47 */
48 public static PortNumber uint16(final short number) {
49 return new PortNumber(number);
50 }
51
52 /**
53 * Creates the unsigned 32 bit port number.
54 *
55 * @param number unsigned 32 bit port number.
56 * @return PortNumber instance
57 */
58 public static PortNumber uint32(final int number) {
59 return new PortNumber(number);
60 }
61
62 /**
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -070063 * Constructor from an int.
64 *
65 * @param value the value to use. (Value will not be validated in any way.)
66 */
67 PortNumber(int value) {
68 this.value = value;
69 }
70
71 // TODO We may want a factory method version
72 // which does the range validation of parsed value.
73 /**
74 * Constructor from decimal string.
75 *
76 * @param decStr decimal string representation of a port number
77 */
78 public PortNumber(String decStr) {
79 this(decStr, 10);
80 }
81
82 /**
83 * Constructor from string.
84 *
85 * @param s string representation of a port number
86 * @param radix the radix to use while parsing {@code s}
87 */
88 public PortNumber(String s, int radix) {
89 this(UnsignedInts.parseUnsignedInt(s, radix));
90 }
91
92 /**
93 * Convert unsigned short to unsigned long.
94 *
95 * @param portno unsigned integer representing port number
96 * @return port number as unsigned long
97 */
98 public static long shortToUnsignedLong(short portno) {
99 return UnsignedInts.toLong(0xffff & portno);
100 }
101
102 /**
103 * Gets the port number as short.
104 * <p/>
105 * Note: User of this method needs to be careful, handling unsigned value.
106 * @return number as short
107 */
108 public short shortValue() {
109 return (short) value;
110 }
111
112 /**
113 * Gets the value of the port as unsigned integer.
Jonathan Hart3edb1752013-11-14 13:28:17 -0800114 *
115 * @return the value of the port.
116 */
117 @JsonProperty("value")
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -0700118 public long value() {
Yuta HIGUCHI5865bdb2014-07-30 09:32:13 -0700119 return 0xffffffffL & value;
Ray Milkey269ffb92014-04-03 14:43:30 -0700120 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800121
Jonathan Hart3edb1752013-11-14 13:28:17 -0800122 /**
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -0700123 * Convert the port value as unsigned integer to a string.
Jonathan Hart3edb1752013-11-14 13:28:17 -0800124 *
125 * @return the port value as a string.
126 */
127 @Override
128 public String toString() {
Yuta HIGUCHI9da3a6e2014-06-10 22:11:58 -0700129 return UnsignedInts.toString(value);
Jonathan Hart3edb1752013-11-14 13:28:17 -0800130 }
Ray Milkey269ffb92014-04-03 14:43:30 -0700131
Jonathan Hart3edb1752013-11-14 13:28:17 -0800132 @Override
133 public boolean equals(Object other) {
Yuta HIGUCHIfb564502014-06-16 21:29:00 -0700134 if (!(other instanceof PortNumber)) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700135 return false;
136 }
Jonathan Hart3edb1752013-11-14 13:28:17 -0800137
Yuta HIGUCHIfb564502014-06-16 21:29:00 -0700138 PortNumber otherPort = (PortNumber) other;
Jonathan Hart3edb1752013-11-14 13:28:17 -0800139
Ray Milkey269ffb92014-04-03 14:43:30 -0700140 return value == otherPort.value;
Jonathan Hart3edb1752013-11-14 13:28:17 -0800141 }
142
143 @Override
144 public int hashCode() {
Yuta HIGUCHIfb564502014-06-16 21:29:00 -0700145 return value;
Jonathan Hart3edb1752013-11-14 13:28:17 -0800146 }
Pavlin Radoslavov5363c2a2013-02-18 09:55:42 -0800147}