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

package com.hypixel.hytale.server.worldgen.util.condition;

import it.unimi.dsi.fastutil.ints.IntSets;
import com.hypixel.hytale.procedurallib.condition.IIntCondition;
import javax.annotation.Nullable;
import it.unimi.dsi.fastutil.ints.IntSet;
import java.util.function.Supplier;
import it.unimi.dsi.fastutil.ints.IntConsumer;

public class IntConditionBuilder implements IntConsumer
{
    private final Supplier<IntSet> setSupplier;
    private final int nullValue;
    private int first;
    @Nullable
    private IntSet set;
    
    public IntConditionBuilder(final Supplier<IntSet> setSupplier, final int nullValue) {
        this.set = null;
        this.setSupplier = setSupplier;
        this.nullValue = nullValue;
        this.first = nullValue;
    }
    
    @Override
    public void accept(final int value) {
        this.add(value);
    }
    
    public boolean add(final int value) {
        if (value == this.first || value == this.nullValue) {
            return false;
        }
        if (this.first == this.nullValue) {
            this.first = value;
            return true;
        }
        if (this.set == null) {
            (this.set = this.setSupplier.get()).add(this.first);
        }
        return this.set.add(value);
    }
    
    public IIntCondition buildOrDefault(final IIntCondition defaultCondition) {
        if (this.first == this.nullValue) {
            return defaultCondition;
        }
        IntSet set = this.set;
        if (set == null) {
            set = IntSets.singleton(this.first);
        }
        return new HashSetIntCondition(set);
    }
}
