blob: 2da83f824b8e36a8d510a1fc47e54ed615cdf0af [file] [log] [blame]
Hyunsun Moona5a18ad2015-08-18 23:11:23 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Hyunsun Moona5a18ad2015-08-18 23:11:23 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Brian O'Connor7cbbbb72016-04-09 02:13:23 -070016package org.onlab.packet;
Hyunsun Moona5a18ad2015-08-18 23:11:23 -070017
18/**
19 * Representation of a transport layer port.
20 */
Hyunsun Moon23f981d2015-08-20 14:01:04 -070021public class TpPort {
Hyunsun Moona5a18ad2015-08-18 23:11:23 -070022
23 private final int port;
24
25 // Transport layer port is unsigned 16 bits integer.
26 public static final int MAX_PORT = 0xFFFF;
27 public static final int MIN_PORT = 0;
28
29 /**
Hyunsun Moon23f981d2015-08-20 14:01:04 -070030 * Constructs a new TpPort.
Hyunsun Moona5a18ad2015-08-18 23:11:23 -070031 *
32 * @param value the transport layer port
33 */
Hyunsun Moon23f981d2015-08-20 14:01:04 -070034 protected TpPort(int value) {
Hyunsun Moona5a18ad2015-08-18 23:11:23 -070035 this.port = value;
36 }
37
38 /**
Hyunsun Moon23f981d2015-08-20 14:01:04 -070039 * Converts an integer into a TpPort.
Hyunsun Moona5a18ad2015-08-18 23:11:23 -070040 *
41 * @param value an integer representing the transport layer port
Hyunsun Moon23f981d2015-08-20 14:01:04 -070042 * @return a TpPort
Hyunsun Moona5a18ad2015-08-18 23:11:23 -070043 * @throws IllegalArgumentException if the value is invalid
44 */
Hyunsun Moon23f981d2015-08-20 14:01:04 -070045 public static TpPort tpPort(int value) {
Hyunsun Moona5a18ad2015-08-18 23:11:23 -070046 if (value < MIN_PORT || value > MAX_PORT) {
47 throw new IllegalArgumentException(
48 "Transport layer port value " + value + "is not in the interval [0, 0xFFFF]");
49 }
Hyunsun Moon23f981d2015-08-20 14:01:04 -070050 return new TpPort(value);
Hyunsun Moona5a18ad2015-08-18 23:11:23 -070051 }
52
53 /**
54 * Returns the integer value for this transport port.
55 *
56 * @return an integer value
57 */
58 public int toInt() {
59 return this.port;
60 }
61
62 /*
63 * (non-Javadoc)
64 *
65 * @see java.lang.Object#equals(java.lang.Object)
66 */
67 @Override
68 public boolean equals(Object obj) {
69 if (this == obj) {
70 return true;
71 }
72
Hyunsun Moon23f981d2015-08-20 14:01:04 -070073 if (obj instanceof TpPort) {
Hyunsun Moona5a18ad2015-08-18 23:11:23 -070074
Hyunsun Moon23f981d2015-08-20 14:01:04 -070075 TpPort other = (TpPort) obj;
Hyunsun Moona5a18ad2015-08-18 23:11:23 -070076
77 if (this.port == other.port) {
78 return true;
79 }
80 }
81 return false;
82 }
83
84 /*
85 * (non-Javadoc)
86 *
87 * @see java.lang.Object#hashCode()
88 */
89 @Override
90 public int hashCode() {
91 return this.port;
92 }
93
94 /*
95 * (non-Javadoc)
96 *
97 * @see java.lang.Object#toString()
98 */
99 @Override
100 public String toString() {
101 return String.valueOf(this.port);
102 }
103}