/***************************************************************************
 *   Copyright (C) 2009 by Fabrizio Montesi <famontesi@gmail.com>          *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program 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 General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 *                                                                         *
 *   For details about the authors of this software, see the AUTHORS file. *
 ***************************************************************************/

package jolie.runtime;

import java.lang.ref.WeakReference;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author Fabrizio Montesi
 */
public abstract class TimeoutHandler implements Runnable
{
	private final long time;
	private volatile boolean cancelled = false;

	public TimeoutHandler( long timeout )
	{
		this.time = System.currentTimeMillis() + timeout;
	}

	public long time()
	{
		return time;
	}
	
	public void cancel()
	{
		cancelled = true;
	}
	
	public void run()
	{
		if ( !cancelled ) {
			onTimeout();
		}
	}

	protected abstract void onTimeout();

	/**
	 * Note: this comparator imposes orderings that are inconsistent with equals.
	 */
	public static class Comparator implements java.util.Comparator< WeakReference< TimeoutHandler > >
	{
		public int compare( WeakReference< TimeoutHandler > wt1, WeakReference< TimeoutHandler > wt2 )
		{
			TimeoutHandler t1 = wt1.get();
			TimeoutHandler t2 = wt2.get();
			if ( t1 == null ) {
				return -1;
			} else if ( t2 == null ) {
				return 1;
			}
			
			// Both are not null after this

			if ( t1.time < t2.time ) {
				return -1;
			} else if ( t1.time == t2.time ) {
				return 0;
			} else {
				return 1;
			}
		}
	}
}

@@ -93,16 +93,21 @@
 (C) 2009
+-2016
  by Fabr
@@ -140,21 +140,16 @@
 ail.com%3E
-     
      *%0A 
@@ -1598,58 +1598,8 @@
 nce;
-%0Aimport java.util.concurrent.atomic.AtomicBoolean;
 %0A%0A/*
@@ -2252,16 +2252,28 @@
  %3E %3E%0A%09%7B%0A
+%09%09@Override%0A
 %09%09public
@@ -2387,16 +2387,34 @@
 ler t1 =
+ ( wt1 != null ) ?
  wt1.get
@@ -2411,24 +2411,31 @@
  ? wt1.get()
+ : null
 ;%0A%09%09%09Timeout
@@ -2446,16 +2446,34 @@
 ler t2 =
+ ( wt2 != null ) ?
  wt2.get
@@ -2474,16 +2474,23 @@
 t2.get()
+ : null
 ;%0A%09%09%09if 

Fix null pointer exceptions for TimeoutHandler comparisons (issue #69).