Revert "BUG-47 : unfinished PCEP migration to generated DTOs."
[bgpcep.git] / concepts / src / main / java / org / opendaylight / protocol / concepts / AbstractPrefix.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.concepts;
9
10 /**
11  * Abstract base class for implementing Prefix classes. This class correctly
12  * implements all required methods, so to implement a Prefix class, you
13  * only need to subclass it and publish an appropriate contructor.
14  *
15  * @param <T> template parameter reference to subclass
16  */
17 public abstract class AbstractPrefix<T extends NetworkAddress<T>> implements Comparable<AbstractPrefix<T>>, Prefix<T> {
18
19         private static final long serialVersionUID = -384546010175152481L;
20
21         private final T address;
22         private final int length;
23
24         /**
25          * Create a new prefix object.
26          *
27          * @param address Base network address
28          * @param length Length of significant part of address, in bits
29          */
30         protected AbstractPrefix(final T address, final int length) {
31                 this.address = address.applyMask(length);
32                 this.length = length;
33         }
34
35         @Override
36         public T getAddress() {
37                 return this.address;
38         }
39
40         @Override
41         public int getLength() {
42                 return this.length;
43         }
44
45         @Override
46         public int compareTo(final AbstractPrefix<T> p) {
47                 final int i = this.address.compareTo(p.getAddress());
48                 if (i != 0)
49                         return i;
50                 if (this.length < p.getLength())
51                         return -1;
52                 if (this.length > p.getLength())
53                         return 1;
54                 return 0;
55         }
56
57         @Override
58         public boolean contains(final NetworkAddress<?> address) {
59                 /*
60                  * Fastest way of checking this is to apply this prefixe's
61                  * mask and check for equality.
62                  */
63                 final NetworkAddress<?> masked = address.applyMask(this.length);
64                 return this.address.equals(masked);
65         }
66
67         /**
68          * {@inheritDoc}
69          *
70          * Returned string will be in the CIDR format.
71          */
72         @Override
73         public String toString() {
74                 final StringBuilder sb = new StringBuilder(this.address.toString());
75                 sb.append('/');
76                 sb.append(this.length);
77                 return sb.toString();
78         }
79
80         @Override
81         public boolean equals(final Object obj) {
82                 if (this == obj)
83                         return true;
84                 if (obj == null)
85                         return false;
86                 if (this.getClass() != obj.getClass())
87                         return false;
88                 final AbstractPrefix<?> other = (AbstractPrefix<?>) obj;
89                 if (this.address == null) {
90                         if (other.address != null)
91                                 return false;
92                 } else if (!this.address.equals(other.address))
93                         return false;
94                 if (this.length != other.length)
95                         return false;
96                 return true;
97         }
98
99         @Override
100         public int hashCode() {
101                 final int prime = 31;
102                 int result = 1;
103                 result = prime * result + ((this.address == null) ? 0 : this.address.hashCode());
104                 result = prime * result + this.length;
105                 return result;
106         }
107 }
108