a1499c5882dd1e7a6b37585885ba65170968a8e1
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / utils / UnsignedLongRangeSet.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.controller.cluster.datastore.utils;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.collect.ImmutableRangeSet;
14 import com.google.common.collect.Range;
15 import com.google.common.collect.RangeSet;
16 import com.google.common.collect.TreeRangeSet;
17 import com.google.common.primitives.UnsignedLong;
18 import org.opendaylight.yangtools.concepts.Mutable;
19
20 /**
21  * Utility {@link RangeSet}-like class, specialized for holding {@link UnsignedLong}. It does not directly implement
22  * the {@link RangeSet} interface, but allows converting to and from it. Internal implementation takes advantage of
23  * knowing that {@link UnsignedLong} is a discrete type and that it can be stored in a long.
24  *
25  * @author Robert Varga
26  */
27 @Beta
28 public final class UnsignedLongRangeSet implements Mutable {
29     // FIXME: this is just to get us started
30     private final RangeSet<UnsignedLong> rangeset;
31
32     private UnsignedLongRangeSet(final RangeSet<UnsignedLong> rangeset) {
33         this.rangeset = requireNonNull(rangeset);
34     }
35
36     public static UnsignedLongRangeSet create() {
37         return new UnsignedLongRangeSet(TreeRangeSet.create());
38     }
39
40     public static UnsignedLongRangeSet create(final RangeSet<UnsignedLong> input) {
41         return new UnsignedLongRangeSet(TreeRangeSet.create(input));
42     }
43
44     public RangeSet<UnsignedLong> toImmutable() {
45         return ImmutableRangeSet.copyOf(rangeset);
46     }
47
48     public void add(final long longBits) {
49         add(UnsignedLong.fromLongBits(longBits));
50     }
51
52     public void add(final UnsignedLong value) {
53         rangeset.add(Range.closedOpen(value, UnsignedLong.ONE.plus(value)));
54     }
55
56     public boolean contains(final UnsignedLong value) {
57         return rangeset.contains(value);
58     }
59
60     public boolean contains(final long longBits) {
61         return contains(UnsignedLong.fromLongBits(longBits));
62     }
63
64     public UnsignedLongRangeSet copy() {
65         return new UnsignedLongRangeSet(TreeRangeSet.create(rangeset));
66     }
67 }