/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved.
 * <a>http://www.crunii.com</a>
 */
package com.crunii.micro.common.concurrent;

import java.util.concurrent.Callable;
import java.util.concurrent.Executors;

/**
 * @author 田平 create 2021/12/21 14:11
 */
public class PriorityCallable<V> implements Callable<V>, Comparable<PriorityCallable> {

	/**
	 * 越小优先级越高
	 */
	private int priority;

	private Callable<V> call;

	public PriorityCallable(Callable<V> call, int priority) {
		this.call = call;
		this.priority = priority;
	}

	public PriorityCallable(Runnable run, V result, int priority) {
		this.call = Executors.callable(run, result);
		this.priority = priority;
	}

	public int getPriority() {
		return priority;
	}

	@Override
	public int compareTo(PriorityCallable o) {
		return Integer.compare(priority, o.getPriority());
	}

	@Override
	public V call() throws Exception {
		return call.call();
	}

}
