// 
// Decompiled by Procyon v0.6.0
// 

package com.hypixel.hytale.builtin.hytalegenerator.vectorproviders;

import com.hypixel.hytale.math.vector.Vector3d;
import com.hypixel.hytale.builtin.hytalegenerator.threadindexer.WorkerIndexer;
import javax.annotation.Nonnull;

public class CacheVectorProvider extends VectorProvider
{
    @Nonnull
    private final VectorProvider vectorProvider;
    @Nonnull
    private final WorkerIndexer.Data<Cache> threadData;
    
    public CacheVectorProvider(@Nonnull final VectorProvider vectorProvider, final int threadCount) {
        if (threadCount <= 0) {
            throw new IllegalArgumentException("threadCount must be greater than 0");
        }
        this.vectorProvider = vectorProvider;
        this.threadData = new WorkerIndexer.Data<Cache>(threadCount, Cache::new);
    }
    
    @Nonnull
    @Override
    public Vector3d process(@Nonnull final Context context) {
        final Cache cache = this.threadData.get(context.workerId);
        if (cache.position != null && cache.position.equals(context.position)) {
            return cache.value;
        }
        if (cache.position == null) {
            cache.position = new Vector3d();
        }
        cache.position.assign(context.position);
        return cache.value = this.vectorProvider.process(context);
    }
    
    public static class Cache
    {
        Vector3d position;
        Vector3d value;
    }
}
