public abstract class AddPathAbstractRouteEntry extends AbstractRouteEntry<AddPathBestPath> {
private static final Logger LOG = LoggerFactory.getLogger(AddPathAbstractRouteEntry.class);
+ private static final Long[] EMPTY_PATHS_ID = new Long[0];
+
private List<AddPathBestPath> bestPath;
private List<AddPathBestPath> bestPathRemoved;
protected OffsetMap offsets = OffsetMap.EMPTY;
- protected Attributes[] values = new Attributes[0];
- protected Long[] pathsId = new Long[0];
+ protected Attributes[] values = EMPTY_ATTRIBUTES;
+ protected Long[] pathsId = EMPTY_PATHS_ID;
private long pathIdCounter = 0L;
private boolean oldNonAddPathBestPathTheSame;
private List<AddPathBestPath> newBestPathToBeAdvertised;
*/
protected final boolean removeRoute(final RouteKey key, final int offset) {
final long pathId = this.offsets.getValue(this.pathsId, offset);
- this.values = this.offsets.removeValue(this.values, offset);
- this.pathsId = this.offsets.removeValue(this.pathsId, offset);
+ this.values = this.offsets.removeValue(this.values, offset, EMPTY_ATTRIBUTES);
+ this.pathsId = this.offsets.removeValue(this.pathsId, offset, EMPTY_PATHS_ID);
this.offsets = this.offsets.without(key);
if (this.removedPaths == null) {
this.removedPaths = new ArrayList<>();
}
});
private static final Comparator<RouteKey> COMPARATOR = RouteKey::compareTo;
+ private static final RouteKey[] EMPTY_KEYS = new RouteKey[0];
static final OffsetMap EMPTY = new OffsetMap(Collections.emptySet());
private final RouteKey[] routeKeys;
private OffsetMap(final Set<RouteKey> routerIds) {
- final RouteKey[] array = routerIds.toArray(new RouteKey[0]);
+ final RouteKey[] array = routerIds.toArray(EMPTY_KEYS);
Arrays.sort(array, COMPARATOR);
this.routeKeys = array;
}
if (index < 0) {
LOG.trace("Router key not found", key);
} else {
- builder.add(removeValue(this.routeKeys, index));
+ builder.add(removeValue(this.routeKeys, index, EMPTY_KEYS));
}
return OFFSETMAPS.getUnchecked(builder.build());
}
return ret;
}
- public <T> T[] removeValue(final T[] oldArray, final int offset) {
+ public <T> T[] removeValue(final T[] oldArray, final int offset, final T[] emptyArray) {
final int length = oldArray.length;
Preconditions.checkArgument(offset >= 0, NEGATIVEOFFSET, offset);
Preconditions.checkArgument(offset < this.routeKeys.length, INVALIDOFFSET, offset, length);
- final T[] ret = (T[]) Array.newInstance(oldArray.getClass().getComponentType(), length - 1);
+ final int newLength = length - 1;
+ if (newLength == 0) {
+ Preconditions.checkArgument(emptyArray.length == 0);
+ return emptyArray;
+ }
+
+ final T[] ret = (T[]) Array.newInstance(oldArray.getClass().getComponentType(), newLength);
System.arraycopy(oldArray, 0, ret, 0, offset);
- if (offset < length - 1) {
- System.arraycopy(oldArray, offset + 1, ret, offset, length - offset - 1);
+ if (offset < newLength) {
+ System.arraycopy(oldArray, offset + 1, ret, offset, newLength - offset);
}
return ret;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Route;
final class ComplexRouteEntry extends AbstractAllPathsRouteEntry {
- private static final Route[] EMPTY_VALUES = new Route[0];
private Route[] values = EMPTY_VALUES;
ComplexRouteEntry(final BGPPeerTracker peerTracker) {
final RouteKey key = new RouteKey(routerId, remotePathId);
final OffsetMap map = getOffsets();
final int offset = map.offsetOf(key);
- this.values = map.removeValue(this.values, offset);
+ this.values = map.removeValue(this.values, offset, EMPTY_VALUES);
return removeRoute(key, offset);
}
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Route;
final class ComplexRouteEntry extends AbstractNPathsRouteEntry {
- private static final Route[] EMPTY_VALUES = new Route[0];
private Route[] values = EMPTY_VALUES;
ComplexRouteEntry(final long npaths, final BGPPeerTracker peerTracker) {
final RouteKey key = new RouteKey(routerId, remotePathId);
final OffsetMap map = getOffsets();
final int offset = map.offsetOf(key);
- this.values = map.removeValue(this.values, offset);
+ this.values = map.removeValue(this.values, offset, EMPTY_VALUES);
return removeRoute(key, offset);
}
@NotThreadSafe
abstract class BaseAbstractRouteEntry extends AbstractRouteEntry<BaseBestPath> {
private static final Logger LOG = LoggerFactory.getLogger(BaseAbstractRouteEntry.class);
- private static final Attributes[] EMPTY_ATTRIBUTES = new Attributes[0];
private OffsetMap offsets = OffsetMap.EMPTY;
private Attributes[] values = EMPTY_ATTRIBUTES;
private BaseBestPath bestPath;
* @return true if its the last route
*/
protected final boolean removeRoute(final UnsignedInteger routerId, final int offset) {
- this.values = this.offsets.removeValue(this.values, offset);
+ this.values = this.offsets.removeValue(this.values, offset, EMPTY_ATTRIBUTES);
this.offsets = this.offsets.without(routerId);
return this.offsets.isEmpty();
}
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Route;
final class ComplexRouteEntry extends BaseAbstractRouteEntry {
- private static final Route[] EMPTY_VALUES = new Route[0];
private Route[] values = EMPTY_VALUES;
ComplexRouteEntry(final BGPPeerTracker peerTracker) {
public boolean removeRoute(final UnsignedInteger routerId, final long remotePathId) {
final OffsetMap map = getOffsets();
final int offset = map.offsetOf(routerId);
- this.values = map.removeValue(this.values, offset);
+ this.values = map.removeValue(this.values, offset, EMPTY_VALUES);
return removeRoute(routerId, offset);
}
}
});
private static final Comparator<UnsignedInteger> COMPARATOR = UnsignedInteger::compareTo;
+ private static final UnsignedInteger[] EMPTY_KEYS = new UnsignedInteger[0];
static final OffsetMap EMPTY = new OffsetMap(Collections.emptySet());
private final UnsignedInteger[] routeKeys;
private OffsetMap(final Set<UnsignedInteger> routerIds) {
- final UnsignedInteger[] array = routerIds.toArray(new UnsignedInteger[0]);
+ final UnsignedInteger[] array = routerIds.toArray(EMPTY_KEYS);
Arrays.sort(array, COMPARATOR);
this.routeKeys = array;
}
if (index < 0) {
LOG.trace("Router key not found", key);
} else {
- builder.add(removeValue(this.routeKeys, index));
+ builder.add(removeValue(this.routeKeys, index, EMPTY_KEYS));
}
return OFFSETMAPS.getUnchecked(builder.build());
}
return ret;
}
- public <T> T[] removeValue(final T[] oldArray, final int offset) {
+ public <T> T[] removeValue(final T[] oldArray, final int offset, final T[] emptyArray) {
final int length = oldArray.length;
Preconditions.checkArgument(offset >= 0, NEGATIVEOFFSET, offset);
Preconditions.checkArgument(offset < this.routeKeys.length, INVALIDOFFSET, offset, length);
- final T[] ret = (T[]) Array.newInstance(oldArray.getClass().getComponentType(), length - 1);
+ final int newLength = length - 1;
+ if (newLength == 0) {
+ Preconditions.checkArgument(emptyArray.length == 0);
+ return emptyArray;
+ }
+
+ final T[] ret = (T[]) Array.newInstance(oldArray.getClass().getComponentType(), newLength);
System.arraycopy(oldArray, 0, ret, 0, offset);
- if (offset < length - 1) {
- System.arraycopy(oldArray, offset + 1, ret, offset, length - offset - 1);
+ if (offset < newLength) {
+ System.arraycopy(oldArray, offset + 1, ret, offset, newLength - offset);
}
return ret;
import org.opendaylight.protocol.bgp.rib.spi.BGPPeerTracker;
import org.opendaylight.protocol.bgp.rib.spi.Peer;
import org.opendaylight.protocol.bgp.rib.spi.RIBSupport;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.message.rev171207.path.attributes.Attributes;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerId;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.PeerRole;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.Route;
import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev180329.rib.TablesKey;
public abstract class AbstractRouteEntry<T extends BestPath> implements RouteEntry {
+ protected static final Attributes[] EMPTY_ATTRIBUTES = new Attributes[0];
+ protected static final Route[] EMPTY_VALUES = new Route[0];
protected final BGPPeerTracker peerTracker;
public AbstractRouteEntry(final BGPPeerTracker peerTracker) {