/*
 * Copyright (c) 2017 Hugo Dupanloup (Yeregorix)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package net.smoofyuniverse.common.task.impl;

import net.smoofyuniverse.common.task.IncrementalListener;
import net.smoofyuniverse.common.task.ProgressListener;

public class ProgressIncrementalListener implements IncrementalListener {
	public final ProgressListener delegate;
	public final long maximum;
	public final boolean limit;
	private long total;

	public ProgressIncrementalListener(ProgressListener delegate, long maximum, boolean limit) {
		if (delegate == null)
			throw new IllegalArgumentException("delegate");

		this.delegate = delegate;
		this.maximum = maximum <= 0 ? 0 : maximum;
		this.limit = limit;

		this.delegate.setProgress(this.maximum == 0 ? ProgressListener.INDETERMINATE : 0);
	}

	@Override
	public long getTotal() {
		return this.total;
	}

	@Override
	public void increment(long value) {
		if (value < 0)
			throw new IllegalArgumentException("negative value");

		this.total += value;
		if (this.maximum != 0) {
			if (this.total >= this.maximum) {
				this.delegate.setProgress(1);
				this.delegate.cancel();
			} else {
				this.delegate.setProgress(this.total / (double) this.maximum);
			}
		}
	}

	@Override
	public boolean isCancellable() {
		return this.delegate.isCancellable();
	}

	@Override
	public void setCancellable(boolean value) {
		this.delegate.setCancellable(value);
	}

	@Override
	public boolean isCancelled() {
		return this.delegate.isCancelled();
	}

	@Override
	public void setCancelled(boolean value) {
		this.delegate.setCancelled(value);
	}
}

/*
 * Copyright (c) 2017 Hugo Dupanloup (Yeregorix)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package net.smoofyuniverse.common.task.impl;

import net.smoofyuniverse.common.task.IncrementalListener;
import net.smoofyuniverse.common.task.ProgressListener;

public class ProgressIncrementalListener implements IncrementalListener {
	public final ProgressListener delegate;
	public final long maximum;
	public final boolean limit;
	private long total;

	public ProgressIncrementalListener(ProgressListener delegate, long maximum, boolean limit) {
		if (delegate == null)
			throw new IllegalArgumentException("delegate");

		this.delegate = delegate;
		this.maximum = maximum <= 0 ? 0 : maximum;
		this.limit = limit;

		this.delegate.setProgress(this.maximum == 0 ? ProgressListener.INDETERMINATE : 0);
	}

	@Override
	public long getTotal() {
		return this.total;
	}

	@Override
	public void increment(long value) {
		if (value < 0)
			throw new IllegalArgumentException("negative value");

		this.total += value;
		if (this.maximum != 0) {
			if (this.total >= this.maximum) {
				this.delegate.setProgress(1);
				if (this.limit)
					this.delegate.cancel();
			} else {
				this.delegate.setProgress(this.total / (double) this.maximum);
			}
		}
	}

	@Override
	public boolean isCancellable() {
		return this.delegate.isCancellable();
	}

	@Override
	public void setCancellable(boolean value) {
		this.delegate.setCancellable(value);
	}

	@Override
	public boolean isCancelled() {
		return this.delegate.isCancelled();
	}

	@Override
	public void setCancelled(boolean value) {
		this.delegate.setCancelled(value);
	}
}

Fix ProgressIncrementalListener limit