2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.concepts;
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.
15 * @param <T> template parameter reference to subclass
17 public abstract class AbstractPrefix<T extends NetworkAddress<T>> implements Comparable<AbstractPrefix<T>>, Prefix<T> {
19 private static final long serialVersionUID = -384546010175152481L;
21 private final T address;
22 private final int length;
25 * Create a new prefix object.
27 * @param address Base network address
28 * @param length Length of significant part of address, in bits
30 protected AbstractPrefix(final T address, final int length) {
31 this.address = address.applyMask(length);
36 public T getAddress() {
41 public int getLength() {
46 public int compareTo(final AbstractPrefix<T> p) {
47 final int i = this.address.compareTo(p.getAddress());
50 if (this.length < p.getLength())
52 if (this.length > p.getLength())
58 public boolean contains(final NetworkAddress<?> address) {
60 * Fastest way of checking this is to apply this prefixe's
61 * mask and check for equality.
63 final NetworkAddress<?> masked = address.applyMask(this.length);
64 return this.address.equals(masked);
70 * Returned string will be in the CIDR format.
73 public String toString() {
74 final StringBuilder sb = new StringBuilder(this.address.toString());
76 sb.append(this.length);
81 public boolean equals(final Object obj) {
86 if (this.getClass() != obj.getClass())
88 final AbstractPrefix<?> other = (AbstractPrefix<?>) obj;
89 if (this.address == null) {
90 if (other.address != null)
92 } else if (!this.address.equals(other.address))
94 if (this.length != other.length)
100 public int hashCode() {
101 final int prime = 31;
103 result = prime * result + ((this.address == null) ? 0 : this.address.hashCode());
104 result = prime * result + this.length;