2 * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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.controller.cluster.datastore.utils;
10 import static java.util.Objects.requireNonNull;
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;
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.
25 * @author Robert Varga
28 public final class UnsignedLongRangeSet implements Mutable {
29 // FIXME: this is just to get us started
30 private final RangeSet<UnsignedLong> rangeset;
32 private UnsignedLongRangeSet(final RangeSet<UnsignedLong> rangeset) {
33 this.rangeset = requireNonNull(rangeset);
36 public static UnsignedLongRangeSet create() {
37 return new UnsignedLongRangeSet(TreeRangeSet.create());
40 public static UnsignedLongRangeSet create(final RangeSet<UnsignedLong> input) {
41 return new UnsignedLongRangeSet(TreeRangeSet.create(input));
44 public RangeSet<UnsignedLong> toImmutable() {
45 return ImmutableRangeSet.copyOf(rangeset);
48 public void add(final long longBits) {
49 add(UnsignedLong.fromLongBits(longBits));
52 public void add(final UnsignedLong value) {
53 rangeset.add(Range.closedOpen(value, UnsignedLong.ONE.plus(value)));
56 public boolean contains(final UnsignedLong value) {
57 return rangeset.contains(value);
60 public boolean contains(final long longBits) {
61 return contains(UnsignedLong.fromLongBits(longBits));
64 public UnsignedLongRangeSet copy() {
65 return new UnsignedLongRangeSet(TreeRangeSet.create(rangeset));