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

package com.hypixel.hytale.metrics.metric;

import com.hypixel.hytale.codec.KeyedCodec;
import com.hypixel.hytale.codec.builder.BuilderCodec;
import javax.annotation.Nonnull;
import com.hypixel.hytale.codec.Codec;

public class Metric
{
    public static final Codec<Metric> CODEC;
    private long min;
    private final AverageCollector average;
    private long max;
    
    public Metric() {
        this.average = new AverageCollector();
        this.clear();
    }
    
    public void add(final long value) {
        if (value < this.min) {
            this.min = value;
        }
        this.average.add((double)value);
        if (value > this.max) {
            this.max = value;
        }
    }
    
    public void remove(final long value) {
        this.average.remove((double)value);
    }
    
    public long getMin() {
        return this.min;
    }
    
    public double getAverage() {
        return this.average.get();
    }
    
    public long getMax() {
        return this.max;
    }
    
    public void clear() {
        this.min = Long.MAX_VALUE;
        this.average.clear();
        this.max = Long.MIN_VALUE;
    }
    
    public void resetMinMax() {
        this.min = Long.MAX_VALUE;
        this.max = Long.MIN_VALUE;
    }
    
    public void calculateMinMax(final long value) {
        if (value < this.min) {
            this.min = value;
        }
        if (value > this.max) {
            this.max = value;
        }
    }
    
    public void addToAverage(final long value) {
        this.average.add((double)value);
    }
    
    public void set(@Nonnull final Metric metric) {
        this.min = metric.min;
        this.average.set(metric.average.get());
        this.max = metric.max;
    }
    
    @Nonnull
    @Override
    public String toString() {
        return "Metric{min=" + this.min + ", average=" + this.average.get() + ", max=" + this.max;
    }
    
    static {
        CODEC = BuilderCodec.builder(Metric.class, Metric::new).append(new KeyedCodec<Long>("Min", Codec.LONG), (metric, s) -> metric.min = s, metric -> metric.min).add().append(new KeyedCodec("Average", Codec.DOUBLE), (metric, s) -> metric.average.set(s), metric -> metric.average.get()).add().append(new KeyedCodec("Max", Codec.LONG), (metric, s) -> metric.max = s, metric -> metric.max).add().build();
    }
}
