Merge "BUG-2383 : clean up transaction chains in Peers"
[bgpcep.git] / bgp / rib-impl / src / main / java / org / opendaylight / protocol / bgp / rib / impl / OffsetMap.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.bgp.rib.impl;
9
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;
21 import java.util.Set;
22
23 /**
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.
27  *
28  * We also provide utility reformat methods, which provide access to
29  * array members and array management features.
30  */
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>() {
34         @Override
35         public OffsetMap load(final Set<UnsignedInteger> key) throws Exception {
36             return new OffsetMap(key);
37         }
38     });
39     private static final Comparator<UnsignedInteger> IPV4_COMPARATOR = new Comparator<UnsignedInteger>() {
40         @Override
41         public int compare(final UnsignedInteger o1, final UnsignedInteger o2) {
42             return o1.compareTo(o2);
43         }
44     };
45     private final UnsignedInteger[] routerIds;
46
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;
51     }
52
53     UnsignedInteger getRouterId(final int offset) {
54         Preconditions.checkArgument(offset >= 0);
55         return this.routerIds[offset];
56     }
57
58     int offsetOf(final UnsignedInteger routerId) {
59         return Arrays.binarySearch(this.routerIds, routerId, IPV4_COMPARATOR);
60     }
61
62     int size() {
63         return this.routerIds.length;
64     }
65
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
70         //       of the array
71         final Builder<UnsignedInteger> b = ImmutableSet.builder();
72         b.add(this.routerIds);
73         b.add(routerId);
74
75         return OFFSETMAPS.getUnchecked(b.build());
76     }
77
78     <T> T getValue(final T[] array, final int offset) {
79         Preconditions.checkArgument(offset >= 0, "Invalid negative offset {}", offset);
80         return array[offset];
81     }
82
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;
86     }
87
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;
92
93         System.arraycopy(oldArray, 0, ret, 0, offset);
94         System.arraycopy(oldArray, offset, ret, offset + 1, oldSize - offset);
95
96         return ret;
97     }
98 }