blob: 9b86a8164ec29786824c0fdf6c379d4b19b2f2d2 [file] [log] [blame]
Hyunsun Moona5a18ad2015-08-18 23:11:23 -07001package 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 */
Hyunsun Moon23f981d2015-08-20 14:01:04 -070022public class TpPort {
Hyunsun Moona5a18ad2015-08-18 23:11:23 -070023
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 /**
Hyunsun Moon23f981d2015-08-20 14:01:04 -070031 * Constructs a new TpPort.
Hyunsun Moona5a18ad2015-08-18 23:11:23 -070032 *
33 * @param value the transport layer port
34 */
Hyunsun Moon23f981d2015-08-20 14:01:04 -070035 protected TpPort(int value) {
Hyunsun Moona5a18ad2015-08-18 23:11:23 -070036 this.port = value;
37 }
38
39 /**
Hyunsun Moon23f981d2015-08-20 14:01:04 -070040 * Converts an integer into a TpPort.
Hyunsun Moona5a18ad2015-08-18 23:11:23 -070041 *
42 * @param value an integer representing the transport layer port
Hyunsun Moon23f981d2015-08-20 14:01:04 -070043 * @return a TpPort
Hyunsun Moona5a18ad2015-08-18 23:11:23 -070044 * @throws IllegalArgumentException if the value is invalid
45 */
Hyunsun Moon23f981d2015-08-20 14:01:04 -070046 public static TpPort tpPort(int value) {
Hyunsun Moona5a18ad2015-08-18 23:11:23 -070047 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 }
Hyunsun Moon23f981d2015-08-20 14:01:04 -070051 return new TpPort(value);
Hyunsun Moona5a18ad2015-08-18 23:11:23 -070052 }
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
Hyunsun Moon23f981d2015-08-20 14:01:04 -070074 if (obj instanceof TpPort) {
Hyunsun Moona5a18ad2015-08-18 23:11:23 -070075
Hyunsun Moon23f981d2015-08-20 14:01:04 -070076 TpPort other = (TpPort) obj;
Hyunsun Moona5a18ad2015-08-18 23:11:23 -070077
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}