Initial code drop
[bgpcep.git] / bgp / concepts / src / main / java / org / opendaylight / protocol / bgp / concepts / AbstractNextHop.java
1 /*
2  * Copyright (c) 2013 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.concepts;
9
10 import org.opendaylight.protocol.concepts.NetworkAddress;
11 import com.google.common.base.Objects;
12 import com.google.common.base.Objects.ToStringHelper;
13 import com.google.common.base.Preconditions;
14
15 /**
16  * Abstract implementation of a NextHop class. This is a useful base class
17  * implementing all the required semantics for a NextHop. Subclasses only
18  * need to supply an appropriate public constructor.
19  *
20  * @param <T> template reference to subclass
21  */
22 public abstract class AbstractNextHop<T extends NetworkAddress<T>> implements NextHop<T> {
23         private static final long serialVersionUID = 2462286640242941943L;
24         private final T global;
25         private final T linkLocal;
26
27         protected AbstractNextHop(final T global, final T linkLocal) {
28                 this.global = Preconditions.checkNotNull(global);
29                 this.linkLocal = linkLocal;
30         }
31
32         @Override
33         public T getGlobal() {
34                 return global;
35         }
36
37         @Override
38         public T getLinkLocal() {
39                 return linkLocal;
40         }
41
42         @Override
43         public int hashCode() {
44                 final int prime = 31;
45                 int result = 1;
46                 result = prime * result + ((global == null) ? 0 : global.hashCode());
47                 result = prime * result
48                                 + ((linkLocal == null) ? 0 : linkLocal.hashCode());
49                 return result;
50         }
51
52         @Override
53         public boolean equals(final Object obj) {
54                 if (this == obj)
55                         return true;
56                 if (obj == null)
57                         return false;
58                 if (getClass() != obj.getClass())
59                         return false;
60                 final AbstractNextHop<?> other = (AbstractNextHop<?>) obj;
61                 if (global == null) {
62                         if (other.global != null)
63                                 return false;
64                 } else if (!global.equals(other.global))
65                         return false;
66                 if (linkLocal == null) {
67                         if (other.linkLocal != null)
68                                 return false;
69                 } else if (!linkLocal.equals(other.linkLocal))
70                         return false;
71                 return true;
72         }
73
74         @Override
75         public final String toString() {
76                 return addToStringAttributes(Objects.toStringHelper(this)).toString();
77         }
78
79         protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
80                 toStringHelper.add("global", global);
81                 toStringHelper.add("linkLocal", linkLocal);
82                 return toStringHelper;
83         }
84
85 }