View Javadoc

1   /*
2   Copyright (C) 2007 Dirk Huenniger
3   
4   This library is free software; you can redistribute it and/or
5   modify it under the terms of the GNU Lesser General Public
6   License as published by the Free Software Foundation; either
7   version 2.1 of the License, or (at your option) any later version.
8   
9   This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  Lesser General Public License for more details.
13  
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   */
18  package org.indi.reactor;
19  
20  import java.util.Date;
21  
22  /**
23   * A timer class. It includes a timer method to be called when the timer expires
24   * as well as the expiration Date and supports an ordering of objects of this
25   * class with respect to the expiration date via implementation of the
26   * comparable interface.
27   * 
28   * @author Dirk Hünniger
29   * 
30   */
31  public class TimerHandler implements Comparable<TimerHandler>, TimerCallback {
32      /**
33       * The expiration date of the timer.
34       */
35      private final Date expirationDate;
36  
37      /**
38       * Class constructor
39       * 
40       * @param expirationDate
41       *                the expiration date of the timer (the Date when the
42       *                onTimer method is to be called by the reactor)
43       */
44      public TimerHandler(Date expirationDate) {
45  	this.expirationDate = expirationDate;
46      }
47  
48      /**
49       * Callback method called by the reactor when the timer expires.
50       */
51      public void onTimer() {
52  
53      }
54  
55      /**
56       * 
57       * @return The expiration date of the timer
58       */
59      public Date getExpirationDate() {
60  	return this.expirationDate;
61      }
62  
63      /**
64       * Equals method overloaded as part of the contract of the comparable
65       * interface to allow an ordering of TimerHandler objects with respect to
66       * expiration date
67       */
68      @Override
69      public boolean equals(Object o) {
70  	return (o == null) ? false
71  		: (o instanceof TimerHandler) ? (0 == compareTo((TimerHandler) o))
72  			: false;
73      }
74  
75      /**
76       * Method implemented as part of the comparable interface to allow an
77       * ordering of TimerHandler objects with respect to the expiration date.
78       */
79      public int compareTo(TimerHandler th) {
80  	return this.expirationDate.compareTo(th.getExpirationDate());
81      }
82  }