com.swirlds.merkledb.collections.ImmutableIndexedObjectListUsingMap Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swirlds-merkledb Show documentation
Show all versions of swirlds-merkledb Show documentation
Swirlds is a software platform designed to build fully-distributed applications that harness the power of the cloud without servers. Now you can develop applications with fairness in decision making, speed, trust and reliability, at a fraction of the cost of traditional server-based platforms.
/*
* Copyright (C) 2021-2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.swirlds.merkledb.collections;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Collection;
import java.util.Collections;
import java.util.Objects;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.stream.Stream;
/**
* An implementation of ImmutableIndexedObjectList that stores its objects in a {@link TreeMap}.
*
* Serves as a reference implementation to check correctness of {@link ImmutableIndexedObjectListUsingArray},
* whose performance profile is better suited for production.
*
* @param
* the type of the IndexedObject in the list
*/
@SuppressWarnings("unused")
public class ImmutableIndexedObjectListUsingMap implements ImmutableIndexedObjectList {
/**
* Maps from self-reported index to list object.
*/
private final SortedMap dataMap;
/**
* Creates a new ImmutableIndexedObjectList from an existing array of objects.
*/
public ImmutableIndexedObjectListUsingMap(final T[] objects) {
Objects.requireNonNull(objects);
final SortedMap map = new TreeMap<>();
for (final T object : objects) {
if (object != null) {
map.put(object.getIndex(), object);
}
}
dataMap = Collections.unmodifiableSortedMap(map);
}
/**
* {@inheritDoc}
*/
@Override
public ImmutableIndexedObjectListUsingMap withAddedObject(final T newT) {
if (newT == null) {
return this;
}
final SortedMap map = new TreeMap<>(dataMap);
map.put(newT.getIndex(), newT);
return new ImmutableIndexedObjectListUsingMap<>(Collections.unmodifiableSortedMap(map));
}
/**
* {@inheritDoc}
*/
@Override
public ImmutableIndexedObjectListUsingMap withDeletedObjects(@NonNull final Collection objectsToDelete) {
if (objectsToDelete.isEmpty() || dataMap.isEmpty()) {
return this;
}
final SortedMap map = new TreeMap<>(dataMap);
for (final T object : objectsToDelete) {
if (object != null) {
map.remove(object.getIndex());
}
}
return new ImmutableIndexedObjectListUsingMap<>(Collections.unmodifiableSortedMap(map));
}
/**
* {@inheritDoc}
*/
@Override
public T getLast() {
return dataMap.isEmpty() ? null : dataMap.get(dataMap.lastKey());
}
/**
* {@inheritDoc}
*/
@Override
public T get(final int objectIndex) {
if (objectIndex < 0) {
throw new IndexOutOfBoundsException("Cannot use negative index " + objectIndex);
}
return dataMap.get(objectIndex);
}
/**
* {@inheritDoc}
*/
@Override
public Stream stream() {
return dataMap.values().stream();
}
@Override
public String toString() {
return prettyPrintedIndices();
}
private ImmutableIndexedObjectListUsingMap(final SortedMap dataMap) {
this.dataMap = dataMap;
}
/**
* Get the number of items in this list
*/
@Override
public int size() {
return dataMap.size();
}
}