Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 3 | * |
| 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'Connor | 7cbbbb7 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 16 | package org.onlab.packet; |
Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 17 | |
| 18 | /** |
| 19 | * Representation of a transport layer port. |
| 20 | */ |
Hyunsun Moon | 23f981d | 2015-08-20 14:01:04 -0700 | [diff] [blame] | 21 | public class TpPort { |
Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 22 | |
| 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 Moon | 23f981d | 2015-08-20 14:01:04 -0700 | [diff] [blame] | 30 | * Constructs a new TpPort. |
Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 31 | * |
| 32 | * @param value the transport layer port |
| 33 | */ |
Hyunsun Moon | 23f981d | 2015-08-20 14:01:04 -0700 | [diff] [blame] | 34 | protected TpPort(int value) { |
Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 35 | this.port = value; |
| 36 | } |
| 37 | |
| 38 | /** |
Hyunsun Moon | 23f981d | 2015-08-20 14:01:04 -0700 | [diff] [blame] | 39 | * Converts an integer into a TpPort. |
Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 40 | * |
| 41 | * @param value an integer representing the transport layer port |
Hyunsun Moon | 23f981d | 2015-08-20 14:01:04 -0700 | [diff] [blame] | 42 | * @return a TpPort |
Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 43 | * @throws IllegalArgumentException if the value is invalid |
| 44 | */ |
Hyunsun Moon | 23f981d | 2015-08-20 14:01:04 -0700 | [diff] [blame] | 45 | public static TpPort tpPort(int value) { |
Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 46 | 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 Moon | 23f981d | 2015-08-20 14:01:04 -0700 | [diff] [blame] | 50 | return new TpPort(value); |
Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 51 | } |
| 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 Moon | 23f981d | 2015-08-20 14:01:04 -0700 | [diff] [blame] | 73 | if (obj instanceof TpPort) { |
Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 74 | |
Hyunsun Moon | 23f981d | 2015-08-20 14:01:04 -0700 | [diff] [blame] | 75 | TpPort other = (TpPort) obj; |
Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 76 | |
| 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 | } |