Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame^] | 1 | package org.onlab.packet; |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2014 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 MPLS label. |
| 21 | */ |
| 22 | public class MplsLabel { |
| 23 | |
| 24 | private final int mplsLabel; |
| 25 | |
| 26 | // An MPLS Label maximum 20 bits. |
| 27 | public static final int MAX_MPLS = 0xFFFFF; |
| 28 | |
| 29 | protected MplsLabel(int value) { |
| 30 | this.mplsLabel = value; |
| 31 | } |
| 32 | |
| 33 | public static MplsLabel mplsLabel(int value) { |
| 34 | |
| 35 | if (value > MAX_MPLS) { |
| 36 | throw new IllegalArgumentException("value exceeds allowed maximum MPLS label value (0xFFFFF)"); |
| 37 | } |
| 38 | return new MplsLabel(value); |
| 39 | } |
| 40 | |
| 41 | public int toInt() { |
| 42 | return this.mplsLabel; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public boolean equals(Object obj) { |
| 47 | if (this == obj) { |
| 48 | return true; |
| 49 | } |
| 50 | |
| 51 | if (obj instanceof MplsLabel) { |
| 52 | |
| 53 | MplsLabel other = (MplsLabel) obj; |
| 54 | |
| 55 | if (this.mplsLabel == other.mplsLabel) { |
| 56 | return true; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public int hashCode() { |
| 65 | return this.mplsLabel; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public String toString() { |
| 70 | return String.valueOf(this.mplsLabel); |
| 71 | } |
| 72 | } |