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.util.Arrays;
18 import java.util.Collections;
19 import java.util.Comparator;
23 * A map of Router identifier to an offset. Used to maintain a simple
24 * offset-based lookup across multiple {@link RouteEntry} objects,
25 * which share either contributors or consumers.
27 * We also provide utility reformat methods, which provide access to
28 * array members and array management features.
30 final class OffsetMap {
31 static final OffsetMap EMPTY = new OffsetMap(Collections.<UnsignedInteger>emptySet());
32 private static final LoadingCache<Set<UnsignedInteger>, OffsetMap> OFFSETMAPS = CacheBuilder.newBuilder().weakValues().build(new CacheLoader<Set<UnsignedInteger>, OffsetMap>() {
34 public OffsetMap load(final Set<UnsignedInteger> key) throws Exception {
35 return new OffsetMap(key);
38 private static final Comparator<UnsignedInteger> IPV4_COMPARATOR = new Comparator<UnsignedInteger>() {
40 public int compare(final UnsignedInteger o1, final UnsignedInteger o2) {
41 return o1.compareTo(o2);
44 private final UnsignedInteger[] routerIds;
46 private OffsetMap(final Set<UnsignedInteger> routerIds) {
47 final UnsignedInteger[] array = routerIds.toArray(new UnsignedInteger[0]);
48 Arrays.sort(array, IPV4_COMPARATOR);
49 this.routerIds = array;
52 UnsignedInteger getRouterId(final int offset) {
53 Preconditions.checkArgument(offset >= 0);
54 return this.routerIds[offset];
57 int offsetOf(final UnsignedInteger routerId) {
58 return Arrays.binarySearch(this.routerIds, routerId, IPV4_COMPARATOR);
62 return this.routerIds.length;
65 OffsetMap with(final UnsignedInteger routerId) {
66 // TODO: we could make this faster if we had an array-backed Set and requiring
67 // the caller to give us the result of offsetOf() -- as that indicates
68 // where to insert the new routerId while maintaining the sorted nature
70 final Builder<UnsignedInteger> b = ImmutableSet.builder();
71 b.add(this.routerIds);
74 return OFFSETMAPS.getUnchecked(b.build());
77 <T> T getValue(final T[] array, final int offset) {
78 Preconditions.checkArgument(offset >= 0, "Invalid negative offset {}", offset);
82 <T> void setValue(final T[] array, final int offset, final T value) {
83 Preconditions.checkArgument(offset >= 0, "Invalid negative offset {}", offset);
84 array[offset] = value;
87 <T> T[] expand(final OffsetMap oldOffsets, final T[] oldArray, final int offset) {
88 @SuppressWarnings("unchecked")
89 final T[] ret = (T[]) new Object[this.routerIds.length];
90 final int oldSize = oldOffsets.routerIds.length;
92 System.arraycopy(oldArray, 0, ret, 0, offset);
93 System.arraycopy(oldArray, offset, ret, offset + 1, oldSize - offset);