Fix findbug and checkstyle issues
[bgpcep.git] / bgp / path-selection-mode / src / main / java / org / opendaylight / protocol / bgp / mode / impl / add / RouteKey.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.bgp.mode.impl.add;
9
10 import com.google.common.base.MoreObjects;
11 import com.google.common.primitives.UnsignedInteger;
12 import javax.annotation.Nonnull;
13
14 public final class RouteKey implements Comparable<RouteKey> {
15     private final Long remotePathId;
16     private final UnsignedInteger routerId;
17
18     public RouteKey(final UnsignedInteger routerId, final Long remotePathId) {
19         this.routerId = routerId;
20         this.remotePathId = remotePathId != null ? remotePathId : 0;
21     }
22
23     private Long getExternalPathId() {
24         return this.remotePathId;
25     }
26
27     public UnsignedInteger getRouteId() {
28         return this.routerId;
29     }
30
31     private MoreObjects.ToStringHelper addToStringAttributes(final MoreObjects.ToStringHelper toStringHelper) {
32         toStringHelper.add("externalPathId", this.remotePathId);
33         toStringHelper.add("routerId", this.routerId);
34         return toStringHelper;
35     }
36
37     @Override
38     public int hashCode() {
39         final int prime = 27;
40         int result = 1;
41         result = prime * result + this.remotePathId.hashCode();
42         result = prime * result + this.routerId.hashCode();
43         return result;
44     }
45
46     @Override
47     public boolean equals(final Object obj) {
48         if (this == obj) {
49             return true;
50         }
51
52         if (!(obj instanceof RouteKey)) {
53             return false;
54         }
55
56         final RouteKey other = (RouteKey) obj;
57         return this.remotePathId.equals(other.remotePathId) && this.routerId.equals(other.routerId);
58     }
59
60     @Override
61     public String toString() {
62         return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
63     }
64
65     @Override
66     public int compareTo(@Nonnull final RouteKey otherRouteKey) {
67         final int routeIdCompareTo = this.routerId.compareTo(otherRouteKey.getRouteId());
68         return routeIdCompareTo == 0 ? this.remotePathId.compareTo(otherRouteKey.getExternalPathId())
69                 : routeIdCompareTo;
70     }
71 }