FileDocCategorySizeDatePackage
Scheduler.javaAPI DocExample6644Wed Sep 19 09:20:16 BST 2001javaxml2

Scheduler

public class Scheduler extends Object
Scheduler is a class that allows addition, removal, and retrieval of a list of events, sorted by their occurrence time.

Fields Summary
private static Vector
events
List of event names (for sorting)
private static Hashtable
eventDetails
Event details (name, time)
private static boolean
eventsSorted
Flag to indicate if events are sorted
Constructors Summary
public Scheduler()

This will initialize the storage.

       

                
      
        eventsSorted = true;
    
Methods Summary
public booleanaddEvent(java.lang.String eventName, java.util.Date eventTime)

This will add the requested event.

param
eventName String name of event to add.
param
eventTime Date of event.
return
boolean - indication of if event was added.

      
        // Add this event to the list of events
        if (!events.contains(eventName)) {
            events.addElement(eventName);
            eventDetails.put(eventName, eventTime);
            eventsSorted = false;
            
            // Start thread on server sorting
            SortEventsThread sorter = new SortEventsThread();
            sorter.start();                        
        }
      
        return true;
    
public java.util.VectorgetListOfEvents()

This returns the current listing of events.

return
Vector - list of events.

     
        Vector list = new Vector();
        
        // Create a Date Formatter
        SimpleDateFormat fmt = 
            new SimpleDateFormat("hh:mm a MM/dd/yyyy");        
        
        // Add each event to the list
        for (int i=0; i<events.size(); i++) {
            String eventName = (String)events.elementAt(i);
            list.addElement("Event \"" + eventName + 
                            "\" scheduled for " +                           
                            fmt.format(
                                (Date)eventDetails.get(eventName)));
        }
        
        return list;
    
public synchronized booleanremoveEvent(java.lang.String eventName)

This will remove the requested event.

param
eventName String name of event to remove.
return
boolean - indication of if event was removed.

        events.remove(eventName);
        eventDetails.remove(eventName);
      
        return true;
    
private synchronized voidsortEvents()

Sort the events in the current list.

        
        if (eventsSorted) {
            return;
        }
      
        // Create array of events as they are (unsorted)
        String[] eventNames = new String[events.size()];
        events.copyInto(eventNames);
                
        // Bubble sort these
        String tmpName;
        Date date1, date2;
        for (int i=0; i<eventNames.length - 1; i++) {
            for (int j=0; j<eventNames.length - i - 1; j++) {
                // Compare the dates for these events                
                date1 = (Date)eventDetails.get(eventNames[j]);
                date2 = (Date)eventDetails.get(eventNames[j+1]);
                if (date1.compareTo(date2) > 0) {
                    
                    // Swap if needed
                    tmpName = eventNames[j];
                    eventNames[j] = eventNames[j+1];
                    eventNames[j+1] = tmpName;
                      
                }
            }
        }
        
        // Put into new Vector (ordered)
        Vector sortedEvents = new Vector();
        for (int i=0; i<eventNames.length; i++) {
            sortedEvents.addElement(eventNames[i]);
        }
        
        // Update the global events
        events = sortedEvents;
        eventsSorted = true;