public abstract class AddPathAbstractRouteEntry extends AbstractRouteEntry {
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 ContainerNode[] values = new ContainerNode[0];
- protected Long[] pathsId = new Long[0];
+ protected ContainerNode[] 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;
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);
}
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 {
private static final Logger LOG = LoggerFactory.getLogger(BaseAbstractRouteEntry.class);
- private static final ContainerNode[] EMPTY_ATTRIBUTES = new ContainerNode[0];
private OffsetMap offsets = OffsetMap.EMPTY;
private ContainerNode[] 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();
}
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;
public abstract class AbstractRouteEntry implements RouteEntry {
protected static final NodeIdentifier ROUTES_IDENTIFIER = new NodeIdentifier(Routes.QNAME);
+ protected static final ContainerNode[] EMPTY_ATTRIBUTES = new ContainerNode[0];
+
private static final Logger LOG = LoggerFactory.getLogger(AbstractRouteEntry.class);
protected static void fillLocRib(final YangInstanceIdentifier routeTarget, final NormalizedNode<?, ?> value,