001    /*
002     * Queues.java
003     *
004     * Created on July 12, 2004, 2:11 PM
005     *
006     * This file is part of the STAR Scheduler.
007     * Copyright (c) 2002-2006 STAR Collaboration - Brookhaven National Laboratory
008     *
009     * STAR Scheduler is free software; you can redistribute it and/or modify
010     * it under the terms of the GNU General Public License as published by
011     * the Free Software Foundation; either version 2 of the License, or
012     * (at your option) any later version.
013     *
014     * STAR Scheduler is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017     * GNU General Public License for more details.
018     *
019     * You should have received a copy of the GNU General Public License
020     * along with STAR Scheduler; if not, write to the Free Software
021     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
022     */
023    
024    package gov.bnl.star.offline.scheduler;
025    
026    
027    import gov.bnl.star.offline.scheduler.Queue;
028    import java.util.List;
029    import java.util.Collections;
030    import java.util.logging.Level;
031    import org.apache.log4j.Logger;
032    
033    import java.util.*;
034    
035    /**
036     * A toolkit for working with lists of queue objects.
037     * @author  Levente Hajdu
038     */
039    public class Queues {
040        
041        static private Logger log = Logger.getLogger(Queues.class.getName());
042        List qlist = (List) ComponentLibrary.getInstance().getComponent("queues");
043        
044        
045        /** Creates a new instance of Queues */
046        public Queues() {
047            
048        }
049        
050        public List getAllQueues(){
051            return qlist;
052        }
053        
054       //done
055        public boolean isInit(){
056            if(qlist == null) return false;
057            return true;   
058        }
059        
060        //done
061        public Queue getQueue(String QueueID){
062            for(int i = 0; i < qlist.size(); i++){
063                Queue queue = (Queue) qlist.get(i);
064                if (queue.getID().compareTo(QueueID) == 0) return queue;
065            }
066            System.out.println("Could not find queue ID =\"" + QueueID + "\".");
067            log.warn("Could not find queue ID =\"" + QueueID + "\".");
068            return null;
069        }
070        
071        //done
072        public List getQueues(List QueusIDs){
073            
074            List queueslist = new ArrayList();
075            
076            for(int i = 0; QueusIDs.size() > i; i++){
077                queueslist.add(getQueue( (String) QueusIDs.get(i))); 
078            }
079            
080            return queueslist;
081        }
082        
083        /** @return true if the queue has the same QueueID */
084        public boolean hasQueue(String QueueID){
085            
086            for(int i = 0; i < qlist.size(); i++){
087                Queue queue = (Queue) qlist.get(i);
088                if (queue.getID() == QueueID) return true;
089            }
090            
091            return false;
092        }
093        
094        /**Will order queues by there search order priority
095         * @return a list of queue objects*/
096        public List OrderQueuesBySearchOrderPriority(List queueList){ //note : this is overloaded
097            Collections.sort(queueList, new QueueComparator());
098            return queueList;
099            
100        }
101        
102        /**Will order queues by there search order priority*/
103        public void OrderQueuesBySearchOrderPriority(){ //note : this is overloaded
104            Collections.sort(qlist, new QueueComparator());
105        }
106        
107        /** Queue comparator class */
108        public class QueueComparator implements java.util.Comparator {
109           
110            public int compare(Object o1, Object o2) {
111                Queue a = (Queue) o1;
112                Queue b = (Queue) o2;
113                return a.getSearchOrderPriority() - b.getSearchOrderPriority();
114            }
115        }
116        
117        
118        /** rotate queues with the same level search order priority */
119        public List rotateSameLevelSearchOrderPriority(List Queues){
120            
121            QueueComparator comp = new QueueComparator(); 
122            for (int nQueues = 0; nQueues + 1 < Queues.size(); nQueues++) {
123                if((comp.compare((Queue) Queues.get(nQueues),(Queue) Queues.get(nQueues + 1))) == 0){
124                    Queue buffer = (Queue) Queues.get(nQueues + 1);
125                    Queues.set(nQueues + 1, Queues.get(nQueues));
126                    Queues.set(nQueues, buffer); 
127                    
128                    //This should go into the log file
129                    //Queue test = (Queue) Queues.get(nQueues + 1);
130                    //System.out.println("Swaping (Static cycling)" + buffer.getName() + " with " + test.getName());
131                }
132            
133            }
134            return Queues;
135        }
136        
137    }