Remove duplicate OffsetMap code
[bgpcep.git] / bgp / path-selection-mode / src / main / java / org / opendaylight / protocol / bgp / mode / impl / add / RouteKeyOffsets.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 com.google.common.cache.CacheBuilder;
11 import com.google.common.cache.CacheLoader;
12 import com.google.common.cache.LoadingCache;
13 import com.google.common.collect.ImmutableSet;
14 import java.util.Comparator;
15 import org.opendaylight.protocol.bgp.mode.impl.AbstractOffsetMap;
16
17 /**
18  * A map of {@link RouteKey} to an offset.
19  *
20  * @see AbstractOffsetMap
21  */
22 final class RouteKeyOffsets extends AbstractOffsetMap<RouteKey, RouteKeyOffsets> {
23     private static final LoadingCache<ImmutableSet<RouteKey>, RouteKeyOffsets> OFFSETMAPS = CacheBuilder.newBuilder()
24             .weakValues().build(new CacheLoader<ImmutableSet<RouteKey>, RouteKeyOffsets>() {
25                 @Override
26                 public RouteKeyOffsets load(final ImmutableSet<RouteKey> key) {
27                     return new RouteKeyOffsets(key);
28                 }
29             });
30     private static final Comparator<RouteKey> COMPARATOR = RouteKey::compareTo;
31     private static final RouteKey[] EMPTY_KEYS = new RouteKey[0];
32
33     static final RouteKeyOffsets EMPTY = new RouteKeyOffsets(ImmutableSet.of());
34
35     private RouteKeyOffsets(final ImmutableSet<RouteKey> routeKeys) {
36         super(EMPTY_KEYS, COMPARATOR, routeKeys);
37     }
38
39     @Override
40     protected Comparator<RouteKey> comparator() {
41         return COMPARATOR;
42     }
43
44     @Override
45     protected RouteKey[] emptyKeys() {
46         return EMPTY_KEYS;
47     }
48
49     @Override
50     protected RouteKeyOffsets instanceForKeys(final ImmutableSet<RouteKey> newKeys) {
51         return OFFSETMAPS.getUnchecked(newKeys);
52     }
53 }