TakeTask.java

package org.microspace.event;

import java.math.BigInteger;

import org.microspace.table.column.Accessor;
import org.microspace.table.query.TableQuery;

/**
 * The TakeTask is uniquely identified by the underlying message Thread id. It
 * is used internally by TakeScheduler
 * 
 * @author gaspar.sinai gaspar.sinai@microspace.org
 * @version 2016-06-25
 */
public class TakeTask<T, M> {

	private final TakeInterest<T, M> takeInterest;
	private final M message;
	private final Object messsageId;
	private final long createdTime;
	private Long assignedTime;
	private BigInteger sequenceNumber;

	TakeTask(TakeInterest<T, M> takeInterest, M message, BigInteger sequenceNumber) {
		this.takeInterest = takeInterest;
		this.message = message;
		messsageId = takeInterest.getAccessor().getPrimaryKeyGetSetPair()
				.get(message);
		createdTime = System.currentTimeMillis();
		this.sequenceNumber = sequenceNumber;
	}
	/**
	 * Obtain the time when this task was assigned to a handler.
	 * 
	 * @return The the time in milliseconds since Unix epoch, when the task was assigned.
	 * Return null if the task is not assigned yet.
	 */
	public Long getAssignedTime() {
		return assignedTime;
	}

	void setAssignedTime(Long assignedTime) {
		this.assignedTime = assignedTime;
	}
	/**
	 * Get the time when this Task was created and queue up.
	 * @return The time in milliseconds since Unix epoch when this task was created.)
	 */
	public long getCreatedTime() {
		return createdTime;
	}
	
	public BigInteger getSequenceNumber () {
		return sequenceNumber;
	}

	void handleTake() {
		takeInterest.getHandler().handleTake(message);
	}

	/**
	 * Obtain the underlying query of this message.
	 * 
	 * @return the query
	 */
	public TableQuery<M> getQuery() {
		return takeInterest.getQuery();
	}

	/**
	 * Obtain the message of this task.
	 * 
	 * @return The message.
	 */
	public M getMessage() {
		return message;
	}

	/**
	 * Obtain the message Id of this message.
	 * 
	 * @return The messageId.
	 */
	public Object getMessageId() {
		return this.messsageId;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	boolean matchByIdOf(M in) {
		Object idIn = getAccessor().getPrimaryKeyGetSetPair().get(in);
		switch (getAccessor().getPrimaryKeyGetSetPair().getIndexType()) {
		case HASHED:
			if (idIn.equals(getMessageId()))
				return true;
			break;
		case SORTED:
			if (((Comparable) idIn).compareTo((Comparable) getMessageId()) == 0)
				return true;
		case AUTO:
			break;
		default:
			break;
		}
		return false;
	}

	/**
	 * Obtain the virtual ThreadId of the message.
	 * 
	 * @return The threadId
	 */
	public T getThreadId() {
		return takeInterest.getThreadId(message);
	}

	/**
	 * A take operation will be performed before executing this tasks.
	 * 
	 * @return true if take is performed.
	 */
	public boolean isPerformTake() {
		return takeInterest.isPerformTake();
	}

	/**
	 * Updatable items will be compressed if they share the same id.
	 * 
	 * @return true if such events will be merged.
	 */
	public boolean isMergeMessages() {
		return takeInterest.isMergeMessages();
	}

	/**
	 * Get the accessor object for this class.
	 * 
	 * @return The Accessor.
	 */
	public Accessor<M> getAccessor() {
		return takeInterest.getAccessor();
	}
}