2 * Copyright (c) 2021 PANTHEON.tech, 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 com.google.common.annotations.Beta;
11 import java.io.DataInput;
12 import java.io.DataOutput;
13 import java.io.IOException;
14 import java.util.TreeSet;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.yangtools.concepts.Immutable;
17 import org.opendaylight.yangtools.concepts.WritableObject;
20 public final class ImmutableUnsignedLongSet extends UnsignedLongSet implements Immutable, WritableObject {
21 private static final @NonNull ImmutableUnsignedLongSet EMPTY = new ImmutableUnsignedLongSet(new TreeSet<>());
23 private ImmutableUnsignedLongSet(final TreeSet<Entry> ranges) {
27 static @NonNull ImmutableUnsignedLongSet of(final TreeSet<Entry> ranges) {
28 return ranges.isEmpty() ? EMPTY : new ImmutableUnsignedLongSet(ranges);
31 public static @NonNull ImmutableUnsignedLongSet of() {
36 public ImmutableUnsignedLongSet immutableCopy() {
40 public static @NonNull ImmutableUnsignedLongSet readFrom(final DataInput in) throws IOException {
41 return readFrom(in, in.readInt());
44 public static @NonNull ImmutableUnsignedLongSet readFrom(final DataInput in, final int size) throws IOException {
49 final var ranges = new TreeSet<Entry>();
50 for (int i = 0; i < size; ++i) {
51 ranges.add(Entry.readUnsigned(in));
53 return new ImmutableUnsignedLongSet(ranges);
57 public void writeTo(final DataOutput out) throws IOException {
62 public void writeRangesTo(final @NonNull DataOutput out, final int size) throws IOException {
64 throw new IOException("Mismatched size: expected " + size() + ", got " + size);
69 private void writeRanges(final @NonNull DataOutput out) throws IOException {
70 for (var range : trustedRanges()) {
71 range.writeUnsigned(out);