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

org.jhotdraw8.icollection.impl.champ.SequencedEntry Maven / Gradle / Ivy

/*
 * @(#)SequencedEntry.java
 * Copyright © 2023 The authors and contributors of JHotDraw. MIT License.
 */

package org.jhotdraw8.icollection.impl.champ;

import org.jspecify.annotations.Nullable;

import java.io.Serial;
import java.util.AbstractMap;
import java.util.Objects;

/**
 * A {@code SequencedEntry} stores an entry of a map and a sequence number.
 * 

* {@code hashCode} and {@code equals} are based on the key and the value * of the entry - the sequence number is not included. */ public class SequencedEntry extends AbstractMap.SimpleImmutableEntry implements SequencedData { @Serial private static final long serialVersionUID = 0L; private final int sequenceNumber; public SequencedEntry(@Nullable K key) { super(key, null); sequenceNumber = NO_SEQUENCE_NUMBER; } public SequencedEntry(@Nullable K key, @Nullable V value, int sequenceNumber) { super(key, value); this.sequenceNumber = sequenceNumber; } public static boolean keyEquals(SequencedEntry a, SequencedEntry b) { return Objects.equals(a.getKey(), b.getKey()); } public static int entryKeyHash(SequencedEntry a) { return Objects.hashCode(a.getKey()); } public static int keyHash(Object a) { return Objects.hashCode(a); } public static SequencedEntry update(SequencedEntry oldK, SequencedEntry newK) { return Objects.equals(oldK.getValue(), newK.getValue()) ? oldK : new SequencedEntry<>(oldK.getKey(), newK.getValue(), oldK.getSequenceNumber()); } public static SequencedEntry updateAndMoveToFirst(SequencedEntry oldK, SequencedEntry newK) { return Objects.equals(oldK.getValue(), newK.getValue()) && oldK.sequenceNumber == newK.sequenceNumber + 1 ? oldK : oldK.sequenceNumber == newK.sequenceNumber + 1 ? new SequencedEntry<>(oldK.getKey(), newK.getValue(), oldK.sequenceNumber) : newK; } public static SequencedEntry updateAndMoveToLast(SequencedEntry oldK, SequencedEntry newK) { return Objects.equals(oldK.getValue(), newK.getValue()) && oldK.sequenceNumber == newK.sequenceNumber - 1 ? oldK : oldK.sequenceNumber == newK.sequenceNumber - 1 ? new SequencedEntry<>(oldK.getKey(), newK.getValue(), oldK.sequenceNumber) : newK; } public int getSequenceNumber() { return sequenceNumber; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy