All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.util.PrioritizedFutureTask Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.util;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

import com.hfg.exception.InvalidValueException;

//------------------------------------------------------------------------------
/**
 * A FutureTask that allows a priority to be specified.
 *
 * @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class PrioritizedFutureTask extends FutureTask implements Comparable>
{
   private Priority mPriority = Priority.MEDIUM;
   private long mSubmissionTime;

   //--------------------------------------------------------------------------
   /**
    * Creates a {@code PrioritizedFutureTask} that will, upon running, execute the
    * given {@code Callable}.
    *
    * @param  inCallable the callable task
    * @throws NullPointerException if the callable is null
    */
   public PrioritizedFutureTask(Callable inCallable)
   {
      super(inCallable);
      init();
   }

   //--------------------------------------------------------------------------
   /**
    * Creates a {@code PrioritizedFutureTask} that will, upon running, execute the
    * given {@code Runnable}, and arrange that {@code get} will return the
    * given result on successful completion.
    *
    * @param inRunnable the runnable task
    * @param inResult the result to return on successful completion. If
    * you don't need a particular result, consider using
    * constructions of the form:
    * {@code Future f = new FutureTask(runnable, null)}
    * @throws NullPointerException if the runnable is null
    */
   public PrioritizedFutureTask(Runnable inRunnable, V inResult)
   {
      super(inRunnable, inResult);
      init();
   }

   private void init()
   {
      mSubmissionTime = System.currentTimeMillis();
   }

   //--------------------------------------------------------------------------
   public PrioritizedFutureTask setPriority(Priority inValue)
   {
      if (null == inValue)
      {
         throw new InvalidValueException("The priority cannot be set to null!");
      }

      mPriority = inValue;
      return this;
   }

   //--------------------------------------------------------------------------
   public Priority getPriority()
   {
      return mPriority;
   }

   //--------------------------------------------------------------------------
   public long getSubmissionTime()
   {
      return mSubmissionTime;
   }

   //--------------------------------------------------------------------------
   @Override
   public boolean equals(Object inObj2)
   {
      return (inObj2 instanceof PrioritizedFutureTask
              && 0 == compareTo((PrioritizedFutureTask) inObj2));
   }

   //--------------------------------------------------------------------------
   @Override
   public int hashCode()
   {
      return getPriority().hashCode() + Long.hashCode(getSubmissionTime());
   }

   //--------------------------------------------------------------------------
   @Override
   public int compareTo(PrioritizedFutureTask inObj2)
   {
      int result = -1;

      if (inObj2 != null)
      {
         result = CompareUtil.compare(getPriority().ordinal(), inObj2.getPriority().ordinal());

         if (0 == result)
         {
            // This will maintain the FIFO ordering within a priority grouping
            result = CompareUtil.compare(getSubmissionTime(), inObj2.getSubmissionTime());
         }
      }

      return result;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy