Initial code drop
[bgpcep.git] / bgp / concepts / src / main / java / org / opendaylight / protocol / bgp / concepts / BGPTableType.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.Identifier;
11 import com.google.common.base.Preconditions;
12
13 /**
14  * Utility class identifying a BGP table type. A table type is formed by two identifiers: AFI and SAFI.
15  */
16 public final class BGPTableType implements Comparable<BGPTableType>, Identifier {
17
18         private static final long serialVersionUID = -5502662876916458740L;
19
20         private final BGPSubsequentAddressFamily safi;
21
22         private final BGPAddressFamily afi;
23
24         /**
25          * Creates BGP Table type.
26          * 
27          * @param afi Address Family Identifier
28          * @param safi Subsequent Address Family Identifier
29          */
30         public BGPTableType(final BGPAddressFamily afi, final BGPSubsequentAddressFamily safi) {
31                 this.afi = Preconditions.checkNotNull(afi, "Address family may not be null");
32                 this.safi = Preconditions.checkNotNull(safi, "Subsequent address family may not be null");
33         }
34
35         /**
36          * Returns Address Family Identifier.
37          * 
38          * @return afi AFI
39          */
40         public BGPAddressFamily getAddressFamily() {
41                 return this.afi;
42         }
43
44         /**
45          * Returns Subsequent Address Family Identifier.
46          * 
47          * @return safi SAFI
48          */
49         public BGPSubsequentAddressFamily getSubsequentAddressFamily() {
50                 return this.safi;
51         }
52
53         @Override
54         public int hashCode() {
55                 int ret = 3 * this.afi.hashCode();
56                 ret += this.safi.hashCode();
57                 return ret;
58         }
59
60         @Override
61         public boolean equals(final Object obj) {
62                 if (obj != null && obj instanceof BGPTableType) {
63                         final BGPTableType o = (BGPTableType) obj;
64                         return this.afi.equals(o.afi) && this.safi.equals(o.safi);
65                 }
66                 return false;
67         }
68
69         @Override
70         public int compareTo(final BGPTableType other) {
71                 if (other == null)
72                         return 1;
73
74                 final int c = this.afi.compareTo(other.afi);
75                 if (c != 0)
76                         return c;
77                 return this.safi.compareTo(other.safi);
78         }
79
80         @Override
81         public String toString() {
82                 return this.afi.toString() + "." + this.safi.toString();
83         }
84 }