package net.minecraft.world.entity.ai.memory;

import com.mojang.datafixers.kinds.Applicative;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import com.mojang.datafixers.kinds.App;
import java.util.Optional;
import com.mojang.serialization.Codec;

public class ExpirableValue<T> {
    private final T value;
    private long timeToLive;
    
    public ExpirableValue(final T object, final long long2) {
        this.value = object;
        this.timeToLive = long2;
    }
    
    public void tick() {
        if (this.canExpire()) {
            --this.timeToLive;
        }
    }
    
    public static <T> ExpirableValue<T> of(final T object) {
        return new ExpirableValue<T>(object, Long.MAX_VALUE);
    }
    
    public static <T> ExpirableValue<T> of(final T object, final long long2) {
        return new ExpirableValue<T>(object, long2);
    }
    
    public T getValue() {
        return this.value;
    }
    
    public boolean hasExpired() {
        return this.timeToLive <= 0L;
    }
    
    @Override
    public String toString() {
        return this.value.toString() + (this.canExpire() ? (" (ttl: " + this.timeToLive + ")") : "");
    }
    
    public boolean canExpire() {
        return this.timeToLive != Long.MAX_VALUE;
    }
    
    public static <T> Codec<ExpirableValue<T>> codec(final Codec<T> codec) {
        return (Codec<ExpirableValue<T>>)RecordCodecBuilder.create(instance -> ((RecordCodecBuilder.Instance)instance).group((App)codec.fieldOf("value").forGetter(awo -> awo.value), (App)Codec.LONG.optionalFieldOf("ttl").forGetter(awo -> awo.canExpire() ? Optional.<Long>of(awo.timeToLive) : Optional.<Long>empty())).apply(instance, (object, optional) -> new ExpirableValue(object, optional.orElse(Long.MAX_VALUE))));
    }
}
