2 * Copyright (c) 2015 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.bgp.mode.impl.base;
10 import com.google.common.base.Preconditions;
11 import com.google.common.cache.CacheBuilder;
12 import com.google.common.cache.CacheLoader;
13 import com.google.common.cache.LoadingCache;
14 import com.google.common.collect.ImmutableSet;
15 import com.google.common.collect.ImmutableSet.Builder;
16 import com.google.common.primitives.UnsignedInteger;
17 import java.lang.reflect.Array;
18 import java.util.Arrays;
19 import java.util.Collections;
20 import java.util.Comparator;
22 import javax.annotation.Nonnull;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * A map of Router identifier to an offset. Used to maintain a simple
28 * offset-based lookup across multiple RouteEntry objects,
29 * which share either contributors or consumers.
30 * We also provide utility reformat methods, which provide access to
31 * array members and array management features.
33 final class OffsetMap {
34 private static final Logger LOG = LoggerFactory.getLogger(OffsetMap.class);
35 private static final String NEGATIVEOFFSET = "Invalid negative offset %s";
36 private static final String INVALIDOFFSET = "Invalid offset %s for %s router IDs";
37 private static final LoadingCache<Set<UnsignedInteger>, OffsetMap> OFFSETMAPS = CacheBuilder.newBuilder()
38 .weakValues().build(new CacheLoader<Set<UnsignedInteger>, OffsetMap>() {
40 public OffsetMap load(@Nonnull final Set<UnsignedInteger> key) {
41 return new OffsetMap(key);
44 private static final Comparator<UnsignedInteger> COMPARATOR = UnsignedInteger::compareTo;
45 private static final UnsignedInteger[] EMPTY_KEYS = new UnsignedInteger[0];
46 static final OffsetMap EMPTY = new OffsetMap(Collections.emptySet());
47 private final UnsignedInteger[] routeKeys;
49 private OffsetMap(final Set<UnsignedInteger> routerIds) {
50 final UnsignedInteger[] array = routerIds.toArray(EMPTY_KEYS);
51 Arrays.sort(array, COMPARATOR);
52 this.routeKeys = array;
55 UnsignedInteger getRouterKey(final int offset) {
56 Preconditions.checkArgument(offset >= 0);
57 return this.routeKeys[offset];
60 public int offsetOf(final UnsignedInteger key) {
61 return Arrays.binarySearch(this.routeKeys, key, COMPARATOR);
65 return this.routeKeys.length;
68 public OffsetMap with(final UnsignedInteger key) {
69 // TODO: we could make this faster if we had an array-backed Set and requiring
70 // the caller to give us the result of offsetOf() -- as that indicates
71 // where to insert the new routerId while maintaining the sorted nature
73 final Builder<UnsignedInteger> builder = ImmutableSet.builder();
74 builder.add(this.routeKeys);
77 return OFFSETMAPS.getUnchecked(builder.build());
80 public OffsetMap without(final UnsignedInteger key) {
81 final Builder<UnsignedInteger> builder = ImmutableSet.builder();
82 final int index = indexOfRouterId(key);
84 LOG.trace("Router key {} not found", key);
86 builder.add(removeValue(this.routeKeys, index, EMPTY_KEYS));
88 return OFFSETMAPS.getUnchecked(builder.build());
91 private int indexOfRouterId(final UnsignedInteger key) {
92 for (int i = 0; i < this.routeKeys.length; i++) {
93 if (key.equals(this.routeKeys[i])) {
100 public <T> T getValue(final T[] array, final int offset) {
101 Preconditions.checkArgument(offset >= 0, NEGATIVEOFFSET, offset);
102 Preconditions.checkArgument(offset < this.routeKeys.length, INVALIDOFFSET, offset, this.routeKeys.length);
103 return array[offset];
106 public <T> void setValue(final T[] array, final int offset, final T value) {
107 Preconditions.checkArgument(offset >= 0, NEGATIVEOFFSET, offset);
108 Preconditions.checkArgument(offset < this.routeKeys.length, INVALIDOFFSET, offset, this.routeKeys.length);
109 array[offset] = value;
112 <T> T[] expand(final OffsetMap oldOffsets, final T[] oldArray, final int offset) {
113 @SuppressWarnings("unchecked")
114 final T[] ret = (T[]) Array.newInstance(oldArray.getClass().getComponentType(), this.routeKeys.length);
115 final int oldSize = oldOffsets.routeKeys.length;
117 System.arraycopy(oldArray, 0, ret, 0, offset);
118 System.arraycopy(oldArray, offset, ret, offset + 1, oldSize - offset);
123 public <T> T[] removeValue(final T[] oldArray, final int offset, final T[] emptyArray) {
124 final int length = oldArray.length;
125 Preconditions.checkArgument(offset >= 0, NEGATIVEOFFSET, offset);
126 Preconditions.checkArgument(offset < this.routeKeys.length, INVALIDOFFSET, offset, length);
128 final int newLength = length - 1;
129 if (newLength == 0) {
130 Preconditions.checkArgument(emptyArray.length == 0);
134 final T[] ret = (T[]) Array.newInstance(oldArray.getClass().getComponentType(), newLength);
135 System.arraycopy(oldArray, 0, ret, 0, offset);
136 if (offset < newLength) {
137 System.arraycopy(oldArray, offset + 1, ret, offset, newLength - offset);
144 return this.size() == 0;