Introduce a dedicated RouterId class
[bgpcep.git] / bgp / path-selection-mode / src / main / java / org / opendaylight / protocol / bgp / mode / impl / add / 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.mode.impl.add;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.cache.CacheBuilder;
13 import com.google.common.cache.CacheLoader;
14 import com.google.common.cache.LoadingCache;
15 import com.google.common.collect.ImmutableSet;
16 import com.google.common.collect.ImmutableSet.Builder;
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 import javax.annotation.Nonnull;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
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.
32  */
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<RouteKey>, OffsetMap> OFFSETMAPS = CacheBuilder.newBuilder().weakValues()
38         .build(new CacheLoader<Set<RouteKey>, OffsetMap>() {
39                 @Override
40                 public OffsetMap load(@Nonnull final Set<RouteKey> key) {
41                     return new OffsetMap(key);
42                 }
43             });
44     private static final Comparator<RouteKey> COMPARATOR = RouteKey::compareTo;
45     private static final RouteKey[] EMPTY_KEYS = new RouteKey[0];
46     static final OffsetMap EMPTY = new OffsetMap(Collections.emptySet());
47
48     private final RouteKey[] routeKeys;
49
50     private OffsetMap(final Set<RouteKey> routerIds) {
51         final RouteKey[] array = routerIds.toArray(EMPTY_KEYS);
52         Arrays.sort(array, COMPARATOR);
53         this.routeKeys = array;
54     }
55
56     int offsetOf(final RouteKey key) {
57         return Arrays.binarySearch(this.routeKeys, key, COMPARATOR);
58     }
59
60     int size() {
61         return this.routeKeys.length;
62     }
63
64     OffsetMap with(final RouteKey key) {
65         // TODO: we could make this faster if we had an array-backed Set and requiring
66         //       the caller to give us the result of offsetOf() -- as that indicates
67         //       where to insert the new routerId while maintaining the sorted nature
68         //       of the array
69         final Builder<RouteKey> builder = ImmutableSet.builder();
70         builder.add(this.routeKeys);
71         builder.add(key);
72
73         return OFFSETMAPS.getUnchecked(builder.build());
74     }
75
76     OffsetMap without(final RouteKey key) {
77         final Builder<RouteKey> builder = ImmutableSet.builder();
78         final int index = indexOfRouterId(key);
79         if (index < 0) {
80             LOG.trace("Router key {} not found", key);
81         } else {
82             builder.add(removeValue(this.routeKeys, index, EMPTY_KEYS));
83         }
84         return OFFSETMAPS.getUnchecked(builder.build());
85     }
86
87     private int indexOfRouterId(final RouteKey key) {
88         for (int i = 0; i < this.routeKeys.length; i++) {
89             if (key.equals(this.routeKeys[i])) {
90                 return i;
91             }
92         }
93         return -1;
94     }
95
96     RouteKey getKey(final int offset) {
97         return routeKeys[offset];
98     }
99
100     <T> T getValue(final T[] array, final int offset) {
101         checkArgument(offset >= 0, NEGATIVEOFFSET, offset);
102         checkArgument(offset < this.routeKeys.length, INVALIDOFFSET, offset, this.routeKeys.length);
103         return array[offset];
104     }
105
106     <T> void setValue(final T[] array, final int offset, final T value) {
107         checkArgument(offset >= 0, NEGATIVEOFFSET, offset);
108         checkArgument(offset < this.routeKeys.length, INVALIDOFFSET, offset, this.routeKeys.length);
109         array[offset] = value;
110     }
111
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;
116
117         System.arraycopy(oldArray, 0, ret, 0, offset);
118         System.arraycopy(oldArray, offset, ret, offset + 1, oldSize - offset);
119
120         return ret;
121     }
122
123     <T> T[] removeValue(final T[] oldArray, final int offset, final T[] emptyArray) {
124         final int length = oldArray.length;
125         checkArgument(offset >= 0, NEGATIVEOFFSET, offset);
126         checkArgument(offset < this.routeKeys.length, INVALIDOFFSET, offset, length);
127
128         final int newLength = length - 1;
129         if (newLength == 0) {
130             checkArgument(emptyArray.length == 0);
131             return emptyArray;
132         }
133
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);
138         }
139
140         return ret;
141     }
142
143     boolean isEmpty() {
144         return this.size() == 0;
145     }
146 }