package com.jscape.inet.ssh.protocol.v2.connection;

import static com.jscape.util.Parameters.assertGreaterOrEqual;

/**
 * @author Alex Usun
 */
public class Window {

    private final long initialSize;
    private volatile long size;

    public Window(long size) {
        assertGreaterOrEqual(size, 0, "Bad size value.");
        this.initialSize = size;

        this.size = size;
    }

    public void increase(long value) {
        this.size += value;
    }

    public void decrease(int value) {
        this.size -= Math.min(this.size, value);
    }

    public long size() {
        return this.size;
    }

    public long space() {
        return Math.max(this.initialSize - this.size, 0);
    }

    @Override
    public String toString() {
        return "Window {"
                + "initialSize=" + this.initialSize
                + ", size=" + this.size
                + '}';
    }
}
