Committed by
Gerrit Code Review
ONOS-2580 Add rich type for transport layer port
Change-Id: I5cebed1671dd71274e4443618eb4d540aa43e65f
Showing
1 changed file
with
104 additions
and
0 deletions
1 | +package org.onlab.packet; | ||
2 | + | ||
3 | +/* | ||
4 | + * Copyright 2014-2015 Open Networking Laboratory | ||
5 | + * | ||
6 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
7 | + * you may not use this file except in compliance with the License. | ||
8 | + * You may obtain a copy of the License at | ||
9 | + * | ||
10 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
11 | + * | ||
12 | + * Unless required by applicable law or agreed to in writing, software | ||
13 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
15 | + * See the License for the specific language governing permissions and | ||
16 | + * limitations under the License. | ||
17 | + */ | ||
18 | + | ||
19 | +/** | ||
20 | + * Representation of a transport layer port. | ||
21 | + */ | ||
22 | +public class TransportPort { | ||
23 | + | ||
24 | + private final int port; | ||
25 | + | ||
26 | + // Transport layer port is unsigned 16 bits integer. | ||
27 | + public static final int MAX_PORT = 0xFFFF; | ||
28 | + public static final int MIN_PORT = 0; | ||
29 | + | ||
30 | + /** | ||
31 | + * Constructs a new TransportPort. | ||
32 | + * | ||
33 | + * @param value the transport layer port | ||
34 | + */ | ||
35 | + protected TransportPort(int value) { | ||
36 | + this.port = value; | ||
37 | + } | ||
38 | + | ||
39 | + /** | ||
40 | + * Converts an integer into a TransportPort. | ||
41 | + * | ||
42 | + * @param value an integer representing the transport layer port | ||
43 | + * @return a TransportPort | ||
44 | + * @throws IllegalArgumentException if the value is invalid | ||
45 | + */ | ||
46 | + public static TransportPort transportPort(int value) { | ||
47 | + if (value < MIN_PORT || value > MAX_PORT) { | ||
48 | + throw new IllegalArgumentException( | ||
49 | + "Transport layer port value " + value + "is not in the interval [0, 0xFFFF]"); | ||
50 | + } | ||
51 | + return new TransportPort(value); | ||
52 | + } | ||
53 | + | ||
54 | + /** | ||
55 | + * Returns the integer value for this transport port. | ||
56 | + * | ||
57 | + * @return an integer value | ||
58 | + */ | ||
59 | + public int toInt() { | ||
60 | + return this.port; | ||
61 | + } | ||
62 | + | ||
63 | + /* | ||
64 | + * (non-Javadoc) | ||
65 | + * | ||
66 | + * @see java.lang.Object#equals(java.lang.Object) | ||
67 | + */ | ||
68 | + @Override | ||
69 | + public boolean equals(Object obj) { | ||
70 | + if (this == obj) { | ||
71 | + return true; | ||
72 | + } | ||
73 | + | ||
74 | + if (obj instanceof TransportPort) { | ||
75 | + | ||
76 | + TransportPort other = (TransportPort) obj; | ||
77 | + | ||
78 | + if (this.port == other.port) { | ||
79 | + return true; | ||
80 | + } | ||
81 | + } | ||
82 | + return false; | ||
83 | + } | ||
84 | + | ||
85 | + /* | ||
86 | + * (non-Javadoc) | ||
87 | + * | ||
88 | + * @see java.lang.Object#hashCode() | ||
89 | + */ | ||
90 | + @Override | ||
91 | + public int hashCode() { | ||
92 | + return this.port; | ||
93 | + } | ||
94 | + | ||
95 | + /* | ||
96 | + * (non-Javadoc) | ||
97 | + * | ||
98 | + * @see java.lang.Object#toString() | ||
99 | + */ | ||
100 | + @Override | ||
101 | + public String toString() { | ||
102 | + return String.valueOf(this.port); | ||
103 | + } | ||
104 | +} |
-
Please register or login to post a comment