Fix NoSuchElementException in toString()
[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.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;
20
21 /**
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.
25  *
26  * @author Robert Varga
27  */
28 @Beta
29 public final class UnsignedLongRangeSet implements Mutable {
30     // FIXME: this is just to get us started
31     private final RangeSet<UnsignedLong> rangeset;
32
33     private UnsignedLongRangeSet(final RangeSet<UnsignedLong> rangeset) {
34         this.rangeset = requireNonNull(rangeset);
35     }
36
37     public static UnsignedLongRangeSet create() {
38         return new UnsignedLongRangeSet(TreeRangeSet.create());
39     }
40
41     public static UnsignedLongRangeSet create(final RangeSet<UnsignedLong> input) {
42         return new UnsignedLongRangeSet(TreeRangeSet.create(input));
43     }
44
45     public RangeSet<UnsignedLong> toImmutable() {
46         return ImmutableRangeSet.copyOf(rangeset);
47     }
48
49     public void add(final long longBits) {
50         add(UnsignedLong.fromLongBits(longBits));
51     }
52
53     public void add(final UnsignedLong value) {
54         rangeset.add(Range.closedOpen(value, UnsignedLong.ONE.plus(value)));
55     }
56
57     public boolean contains(final UnsignedLong value) {
58         return rangeset.contains(value);
59     }
60
61     public boolean contains(final long longBits) {
62         return contains(UnsignedLong.fromLongBits(longBits));
63     }
64
65     public UnsignedLongRangeSet copy() {
66         return new UnsignedLongRangeSet(TreeRangeSet.create(rangeset));
67     }
68
69     @Override
70     public String toString() {
71         return MoreObjects.toStringHelper(this)
72                 .omitNullValues()
73                 .add("span", rangeset.isEmpty() ? null : rangeset.span())
74                 .add("rangeSize", rangeset.asRanges().size())
75                 .toString();
76     }
77 }