blob: 50e56f71f68dd6d06a265453538cbc180499beeb [file] [log] [blame]
wei wei89ddc322015-03-22 16:29:04 -05001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
16package org.onosproject.net.tunnel;
17
18import static com.google.common.base.Preconditions.checkArgument;
19
20/**
21 * Representation of a Tunnel Id.
22 */
23public final class TunnelId {
24 private final long value;
25
26 /**
27 * Creates an tunnel identifier from the specified tunnel.
28 *
29 * @param value long value
30 * @return tunnel identifier
31 */
32 public static TunnelId valueOf(long value) {
33 return new TunnelId(value);
34 }
35
36 public static TunnelId valueOf(String value) {
37 checkArgument(value.startsWith("0x"));
38 return new TunnelId(Long.parseLong(value.substring("0x".length()), 16));
39 }
40
41 /**
42 * Constructor for serializer.
43 */
44 TunnelId() {
45 this.value = 0;
46 }
47
48 /**
49 * Constructs the ID corresponding to a given long value.
50 *
51 * @param value the underlying value of this ID
52 */
53 TunnelId(long value) {
54 this.value = value;
55 }
56
57 /**
58 * Returns the backing value.
59 *
60 * @return the value
61 */
62 public long id() {
63 return value;
64 }
65
66 @Override
67 public int hashCode() {
68 return (int) (value ^ (value >>> 32));
69 }
70
71 @Override
72 public boolean equals(Object obj) {
73 if (obj == this) {
74 return true;
75 }
76 if (!(obj instanceof TunnelId)) {
77 return false;
78 }
79 TunnelId that = (TunnelId) obj;
80 return this.value == that.value;
81 }
82
83 @Override
84 public String toString() {
85 return "0x" + Long.toHexString(value);
86 }
87
88}