/*
 * @(#)Limit.java
 *
 * Copyright (c) 2002-2003 JSCAPE
 * 1147 S. 53rd Pl., Mesa, Arizona, 85206, U.S.A.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * JSCAPE. ("Confidential Information").  You shall not disclose such
 * Confidential Information and shall use it only in accordance with
 * the terms of the license agreement you entered into with JSCAPE.
 */

package com.jscape.util;

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

/**
 * @author Alex
 */
public class Limit {

    private final long value;
    private long counter;

    public Limit(long value) {
        assertGreaterOrEqual(value, 0, "Bad limit value.");
        this.value = value;
    }

    public long getValue() {
        return this.value;
    }

    public long getCounter() {
        return this.counter;
    }

    public long remained() {
        return this.value - this.counter;
    }

    public Limit increment() {
        return increment(1);
    }

    public Limit increment(long value) {
        assertGreaterOrEqual(value, 0, "Bad increment value.");

        value = Math.min(remained(), value);
        this.counter += value;

        return this;
    }

    public Limit reset() {
        this.counter = 0;
        return this;
    }

    public boolean isExceeded() {
        return this.value <= this.counter;
    }

    @Override
    public String toString() {
        return "Limit {"
                + "value=" + this.value
                + ", counter=" + this.counter
                + '}';
    }
}
