All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.minestom.server.snapshot.SnapshotUpdater Maven / Gradle / Ivy

There is a newer version: 7320437640
Show newest version
package net.minestom.server.snapshot;

import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.stream.Collectors;

/**
 * Represents the context of a snapshot build.
 * Used in {@link Snapshotable#updateSnapshot(SnapshotUpdater)} to create snapshot references and avoid circular dependencies.
 * Updaters must never leave scope, as its data may be state related (change according to the currently processed snapshot).
 * 

* Implementations do not need to be thread-safe and cannot be re-used. */ @ApiStatus.Experimental public sealed interface SnapshotUpdater permits SnapshotUpdaterImpl { /** * Updates the snapshot of the given snapshotable. *

* Method must be called during a safe-point (when the server state is stable). * * @param snapshotable the snapshot container * @param the snapshot type * @return the new updated snapshot */ static @NotNull T update(@NotNull Snapshotable snapshotable) { return SnapshotUpdaterImpl.update(snapshotable); } @NotNull AtomicReference reference(@NotNull Snapshotable snapshotable); @Contract("!null -> !null") default AtomicReference optionalReference(Snapshotable snapshotable) { return snapshotable != null ? reference(snapshotable) : null; } default @NotNull Map> referencesMap(@NotNull Collection snapshotables, @NotNull Function mappingFunction) { return snapshotables.stream().collect(Collectors.toUnmodifiableMap(mappingFunction, this::reference)); } default @NotNull Map> referencesMapLong(@NotNull Collection snapshotables, @NotNull ToLongFunction mappingFunction) { Long2ObjectOpenHashMap> map = new Long2ObjectOpenHashMap<>(snapshotables.size()); for (S snapshotable : snapshotables) { map.put(mappingFunction.applyAsLong(snapshotable), reference(snapshotable)); } map.trim(); return map; } default @NotNull Map> referencesMapInt(@NotNull Collection snapshotables, @NotNull ToIntFunction mappingFunction) { Int2ObjectOpenHashMap> map = new Int2ObjectOpenHashMap<>(snapshotables.size()); for (S snapshotable : snapshotables) { map.put(mappingFunction.applyAsInt(snapshotable), reference(snapshotable)); } map.trim(); return map; } }