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.base.MoreObjects;
14 import com.google.common.collect.ImmutableRangeSet;
15 import com.google.common.collect.Range;
16 import com.google.common.collect.RangeSet;
17 import com.google.common.collect.TreeRangeSet;
18 import com.google.common.primitives.UnsignedLong;
19 import org.opendaylight.yangtools.concepts.Mutable;
22 * Utility {@link RangeSet}-like class, specialized for holding {@link UnsignedLong}. It does not directly implement
23 * the {@link RangeSet} interface, but allows converting to and from it. Internal implementation takes advantage of
24 * knowing that {@link UnsignedLong} is a discrete type and that it can be stored in a long.
26 * @author Robert Varga
29 public final class UnsignedLongRangeSet implements Mutable {
30 // FIXME: this is just to get us started
31 private final RangeSet<UnsignedLong> rangeset;
33 private UnsignedLongRangeSet(final RangeSet<UnsignedLong> rangeset) {
34 this.rangeset = requireNonNull(rangeset);
37 public static UnsignedLongRangeSet create() {
38 return new UnsignedLongRangeSet(TreeRangeSet.create());
41 public static UnsignedLongRangeSet create(final RangeSet<UnsignedLong> input) {
42 return new UnsignedLongRangeSet(TreeRangeSet.create(input));
45 public RangeSet<UnsignedLong> toImmutable() {
46 return ImmutableRangeSet.copyOf(rangeset);
49 public void add(final long longBits) {
50 add(UnsignedLong.fromLongBits(longBits));
53 public void add(final UnsignedLong value) {
54 rangeset.add(Range.closedOpen(value, UnsignedLong.ONE.plus(value)));
57 public boolean contains(final UnsignedLong value) {
58 return rangeset.contains(value);
61 public boolean contains(final long longBits) {
62 return contains(UnsignedLong.fromLongBits(longBits));
65 public UnsignedLongRangeSet copy() {
66 return new UnsignedLongRangeSet(TreeRangeSet.create(rangeset));
70 public String toString() {
71 return MoreObjects.toStringHelper(this)
72 .add("span", rangeset.span())
73 .add("rangeSize", rangeset.asRanges().size())