1 /* 2 * Copyright (c) 1998-2002 The Jgroup Team. 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License version 2 as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU Lesser General Public License for more details. 12 * 13 * You should have received a copy of the GNU Lesser General Public License 14 * along with this program; if not, write to the Free Software 15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 16 * 17 */ 18 19 package jgroup.relacs.mss; 20 21 import java.util.concurrent.Future; 22 23 24 /** 25 * The <code>ScheduledEvent</code> class 26 * 27 * @author Alberto Montresor 28 * @author Hein Meling 29 * @since Jgroup 0.1 30 */ 31 final class ScheduledEvent 32 implements Comparable, MssTag 33 { 34 35 //////////////////////////////////////////////////////////////////////////////////////////// 36 // Fields 37 //////////////////////////////////////////////////////////////////////////////////////////// 38 39 /** Timeout value */ 40 private long timeout; 41 42 /** Tag associated to this event */ 43 private byte tag; 44 45 /** Object associated to this event */ 46 private Object obj; 47 48 private Future<?> handle; 49 50 51 //////////////////////////////////////////////////////////////////////////////////////////// 52 // Constructors 53 //////////////////////////////////////////////////////////////////////////////////////////// 54 55 /** 56 * 57 */ 58 ScheduledEvent(byte tag) 59 { 60 this(tag, null); 61 } 62 63 /** 64 * 65 */ 66 ScheduledEvent(byte tag, Object obj) 67 { 68 this.tag = tag; 69 this.obj = obj; 70 } 71 72 73 //////////////////////////////////////////////////////////////////////////////////////////// 74 // Methods 75 //////////////////////////////////////////////////////////////////////////////////////////// 76 77 /** 78 * Set the timeout of this event. 79 */ 80 void setTimeout(long timeout) 81 { 82 this.timeout = timeout; 83 } 84 85 /** 86 * Set the timeout of this event and schedule the timer task. 87 */ 88 public void setTimeout(long timeout, Future<?> handle) 89 { 90 this.timeout = timeout; 91 this.handle = handle; 92 } 93 94 public void abortTimeout() 95 { 96 handle.cancel(false); 97 } 98 99 /** 100 * Returns the actual timeout associated to this event. 101 */ 102 long getTimeout() 103 { 104 return timeout; 105 } 106 107 /** 108 * Returns the event object associated to this event. 109 */ 110 Object getData() 111 { 112 return obj; 113 } 114 115 /** 116 * Returns the tag associated to this event 117 */ 118 byte getTag() 119 { 120 return tag; 121 } 122 123 //////////////////////////////////////////////////////////////////////////////////////////// 124 // Methods from Object and Comparable 125 //////////////////////////////////////////////////////////////////////////////////////////// 126 127 /** 128 * Compares this object with the specified object for order. 129 */ 130 public int compareTo(Object obj) 131 { 132 if (this == obj) { 133 return 0; 134 } else { 135 ScheduledEvent event = (ScheduledEvent) obj; 136 if (timeout < event.timeout) { 137 return -1; 138 } else if (timeout == event.timeout) { 139 return 0; 140 } else { 141 return 1; 142 } 143 } 144 } 145 146 /** 147 * 148 */ 149 public int hashCode() 150 { 151 return (int) timeout; 152 } 153 154 /** 155 * Compares two objects for content equality. 156 * 157 * @param obj the object to compare with 158 * @return true if these objects are equal; false otherwise. 159 */ 160 public boolean equals(Object obj) 161 { 162 if (this == obj) { 163 return true; 164 } else { 165 if ( (obj != null) && (obj instanceof ScheduledEvent) ) { 166 ScheduledEvent event = (ScheduledEvent) obj; 167 return (event.timeout == timeout); 168 } else { 169 return false; 170 } 171 } 172 } 173 174 /** 175 * Returns a string representation of this object 176 */ 177 public String toString() 178 { 179 StringBuilder buffer = new StringBuilder(); 180 buffer.append("["); 181 if (tag >= TAGTYPE.length || tag < 0) { 182 buffer.append("INVALID TAG"); 183 } else { 184 buffer.append(TAGTYPE[tag]); 185 } 186 buffer.append(", timeout="); 187 buffer.append(timeout); 188 // buffer.append(timeout - System.currentTimeMillis()); 189 if (obj != null) { 190 buffer.append(", obj="); 191 buffer.append(obj); 192 } 193 buffer.append("]"); 194 return buffer.toString(); 195 } 196 197 } // END ScheduledEvent