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.rib.impl;
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;
24 * A map of Router identifier to an offset. Used to maintain a simple
25 * offset-based lookup across multiple {@link AbstractRouteEntry} objects,
26 * which share either contributors or consumers.
28 * We also provide utility reformat methods, which provide access to
29 * array members and array management features.
31 final class OffsetMap {
32 static final OffsetMap EMPTY = new OffsetMap(Collections.<UnsignedInteger>emptySet());
33 private static final LoadingCache<Set<UnsignedInteger>, OffsetMap> OFFSETMAPS = CacheBuilder.newBuilder().weakValues().build(new CacheLoader<Set<UnsignedInteger>, OffsetMap>() {
35 public OffsetMap load(final Set<UnsignedInteger> key) throws Exception {
36 return new OffsetMap(key);
39 private static final Comparator<UnsignedInteger> IPV4_COMPARATOR = new Comparator<UnsignedInteger>() {
41 public int compare(final UnsignedInteger o1, final UnsignedInteger o2) {
42 return o1.compareTo(o2);
45 private final UnsignedInteger[] routerIds;
47 private OffsetMap(final Set<UnsignedInteger> routerIds) {
48 final UnsignedInteger[] array = routerIds.toArray(new UnsignedInteger[0]);
49 Arrays.sort(array, IPV4_COMPARATOR);
50 this.routerIds = array;
53 UnsignedInteger getRouterId(final int offset) {
54 Preconditions.checkArgument(offset >= 0);
55 return this.routerIds[offset];
58 int offsetOf(final UnsignedInteger routerId) {
59 return Arrays.binarySearch(this.routerIds, routerId, IPV4_COMPARATOR);
63 return this.routerIds.length;
66 OffsetMap with(final UnsignedInteger routerId) {
67 // TODO: we could make this faster if we had an array-backed Set and requiring
68 // the caller to give us the result of offsetOf() -- as that indicates
69 // where to insert the new routerId while maintaining the sorted nature
71 final Builder<UnsignedInteger> b = ImmutableSet.builder();
72 b.add(this.routerIds);
75 return OFFSETMAPS.getUnchecked(b.build());
78 <T> T getValue(final T[] array, final int offset) {
79 Preconditions.checkArgument(offset >= 0, "Invalid negative offset {}", offset);
83 <T> void setValue(final T[] array, final int offset, final T value) {
84 Preconditions.checkArgument(offset >= 0, "Invalid negative offset {}", offset);
85 array[offset] = value;
88 <T> T[] expand(final OffsetMap oldOffsets, final T[] oldArray, final int offset) {
89 @SuppressWarnings("unchecked")
90 final T[] ret = (T[]) Array.newInstance(oldArray.getClass().getComponentType(), this.routerIds.length);
91 final int oldSize = oldOffsets.routerIds.length;
93 System.arraycopy(oldArray, 0, ret, 0, offset);
94 System.arraycopy(oldArray, offset, ret, offset + 1, oldSize - offset);