KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > scheduler > TimedTaskList


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 //
25
// Copyright 2001 iPlanet/ Sun Microsystems, Inc. All Rights Reserved.
26
//
27
// Author : darpan
28
// Module : Utils
29
//
30

31 package com.sun.enterprise.util.scheduler;
32
33 import com.sun.enterprise.util.collection.DListNode;
34
35 /**
36 * Maintains a list of elements in an increasing order. Each element is a
37 * TaskData object encapsulating an absolute execute time and the servicable
38 * task object.
39 * <BR> <I>$Source: /cvs/glassfish/appserv-commons/src/java/com/sun/enterprise/util/scheduler/TimedTaskList.java,v $</I>
40 * @author $Author: tcfujii $
41 * @version 1.0 $Revision: 1.3 $ $Date: 2005/12/25 04:12:30 $
42 * @see PeriodicallyServicable
43 */

44 public class TimedTaskList
45 {
46     /** First dummy node. */
47     private DListNode first;
48     /** Last dummy node. */
49     private DListNode last;
50     /** Maintains the current size of the list. */
51     private int size = 0;
52     
53     /**
54     * Constructor for TimedTaskList, created a new DList object.
55     */

56     protected TimedTaskList()
57     {
58         first = new DListNode(null);
59         last = new DListNode(null);
60         first.next = last;
61         last.prev = first;
62         first.prev = last.next = null;
63     }
64
65     /**
66     * Return the size of the task list.
67     * @return 0 if empty, otherwise a number indicating the number of elements.
68     */

69     protected int size()
70     {
71         return size;
72     }
73
74     /**
75     * Remove first task from the ordered list, and return it.
76     * @return first task on the queue.
77     */

78     protected TaskData getFirstTask()
79     {
80         DListNode node = first;
81         node = node.next;
82         if(null!=node.next) // not the end
83
{
84             node.delink();
85             --size;
86             return (TaskData) node.object;
87         }
88         return null;
89     }
90
91     /**
92     * Add a new servicable task into the task list (ordered in increasing order).
93     * @param taskObj servicable object to add.
94     * @param startingTime wait for how long to start considering this task.
95     * @param currentTime current time when executing other tasks.
96     * @return boolean true on success, false otherwise.
97     */

98     protected boolean addTask(PeriodicallyServicable taskObj, int startingTime, long currentTime)
99     {
100         TaskData task = new TaskData();
101         task.obj = taskObj;
102         // decrement frequency time as it will be added on insertTask( ) anyway
103
task.abs_execute_time = currentTime + startingTime - taskObj.getFrequency();
104         return insertTask(task);
105     }
106
107     /**
108     * Insert task back into the task list (ordered in increasing order).
109     * @param task object to execute in an order.
110     * @return boolean true on success, false otherwise.
111     */

112     protected boolean insertTask(TaskData task)
113     {
114         task.abs_execute_time += ((PeriodicallyServicable)task.obj).getFrequency();
115         DListNode addingNode = new DListNode(task);
116         DListNode node = first;
117         ++size;
118         for(int i=0; i<size-1; i++)
119         {
120             node = node.next;
121             long nodeTime = ((TaskData)node.object).abs_execute_time;
122             if(nodeTime > task.abs_execute_time)
123             {
124                 node.insertBefore(addingNode);
125                 return true;
126             }
127         }
128         node.insertAfter(addingNode);
129         return true;
130       }
131
132     /**
133     * Removes the Servicable object from the task list permanently.
134     * @param obj servicable object to remove.
135     * @return boolean true on success, false otherwise.
136     */

137     protected boolean removeTask(PeriodicallyServicable obj)
138     {
139         DListNode node = first;
140
141         for(int i=0; i<size; i++)
142         {
143             node = node.next;
144             PeriodicallyServicable nodeObj = ((TaskData)node.object).obj;
145
146             if(nodeObj.equals(obj))
147             {
148                 node.delink();
149                 --size;
150                 return true;
151             }
152         }
153         return false;
154     }
155
156     /**
157     * Prints information about the list and its contents.
158     * @return String
159     */

160     public String JavaDoc toString()
161     {
162         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(100);
163         sb.append(" [TimedTaskList: ");
164         sb.append( size + " elements: ");
165         DListNode node = first;
166         while(null!=node.next && null!=node.next.object)
167         {
168             node = node.next;
169             sb.append((int) ( ((TaskData)node.object).abs_execute_time / 1000) );
170             sb.append(",");
171         }
172         sb.append("] ");
173         return sb.toString();
174     }
175 }
176
Popular Tags