Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 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 | */ |
Hyunsun Moon | 23f981d | 2015-08-20 14:01:04 -0700 | [diff] [blame] | 22 | public class TpPort { |
Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 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 | /** |
Hyunsun Moon | 23f981d | 2015-08-20 14:01:04 -0700 | [diff] [blame] | 31 | * Constructs a new TpPort. |
Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 32 | * |
| 33 | * @param value the transport layer port |
| 34 | */ |
Hyunsun Moon | 23f981d | 2015-08-20 14:01:04 -0700 | [diff] [blame] | 35 | protected TpPort(int value) { |
Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 36 | this.port = value; |
| 37 | } |
| 38 | |
| 39 | /** |
Hyunsun Moon | 23f981d | 2015-08-20 14:01:04 -0700 | [diff] [blame] | 40 | * Converts an integer into a TpPort. |
Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 41 | * |
| 42 | * @param value an integer representing the transport layer port |
Hyunsun Moon | 23f981d | 2015-08-20 14:01:04 -0700 | [diff] [blame] | 43 | * @return a TpPort |
Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 44 | * @throws IllegalArgumentException if the value is invalid |
| 45 | */ |
Hyunsun Moon | 23f981d | 2015-08-20 14:01:04 -0700 | [diff] [blame] | 46 | public static TpPort tpPort(int value) { |
Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 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 | } |
Hyunsun Moon | 23f981d | 2015-08-20 14:01:04 -0700 | [diff] [blame] | 51 | return new TpPort(value); |
Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 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 | |
Hyunsun Moon | 23f981d | 2015-08-20 14:01:04 -0700 | [diff] [blame] | 74 | if (obj instanceof TpPort) { |
Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 75 | |
Hyunsun Moon | 23f981d | 2015-08-20 14:01:04 -0700 | [diff] [blame] | 76 | TpPort other = (TpPort) obj; |
Hyunsun Moon | a5a18ad | 2015-08-18 23:11:23 -0700 | [diff] [blame] | 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 | } |