KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > drftpd > slaveselection > filter > ScoreChart


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.drftpd.slaveselection.filter;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Collection JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 import net.sf.drftpd.NoAvailableSlaveException;
25 import net.sf.drftpd.ObjectNotFoundException;
26 import net.sf.drftpd.master.RemoteSlave;
27
28 import org.apache.log4j.Logger;
29
30 /**
31  * @author mog
32  * @version $Id: ScoreChart.java,v 1.4 2004/05/20 14:09:00 zubov Exp $
33  */

34 public class ScoreChart {
35     public static class SlaveScore implements Comparable JavaDoc {
36         private RemoteSlave _rslave;
37         private long _score;
38
39         public SlaveScore(RemoteSlave rslave) {
40             _rslave = rslave;
41         }
42
43         public void addScore(long score) {
44             //logger.debug("Added "+score+" to "+getRSlave().getName());
45
_score += score;
46         }
47         public int compareTo(Object JavaDoc o) {
48             SlaveScore s = (SlaveScore) o;
49             //int thisVal = this.value;
50
//int anotherVal = anotherInteger.value;
51
return (getScore()<s.getScore()? -1 : (getScore()==s.getScore()? 0 : 1));
52         }
53         public RemoteSlave getRSlave() {
54             return _rslave;
55         }
56         public long getScore() {
57             return _score;
58         }
59         public String JavaDoc toString() {
60             return "SlaveScore[rslave="+getRSlave().getName()+",score="+getScore()+"]";
61         }
62     }
63
64     private static final Logger logger = Logger.getLogger(ScoreChart.class);
65
66     private ArrayList JavaDoc _scoreChart;
67
68     /**
69      * @param slaves Collection of ONLINE slaves
70      * (this is not verified by scorechart, you can use offline slaves for JUnit tests etc.)
71      */

72     public ScoreChart(Collection JavaDoc slaves) {
73         _scoreChart = new ArrayList JavaDoc();
74         for (Iterator JavaDoc iter = slaves.iterator(); iter.hasNext();) {
75             RemoteSlave rslave = (RemoteSlave) iter.next();
76             _scoreChart.add(new SlaveScore(rslave));
77         }
78     }
79     public RemoteSlave getBestSlave() throws NoAvailableSlaveException {
80         return getBestSlaveScore().getRSlave();
81     }
82
83     public SlaveScore getBestSlaveScore() throws NoAvailableSlaveException {
84         SlaveScore bestscore;
85         Iterator JavaDoc iter = getSlaveScores().iterator();
86         if (!iter.hasNext())
87             throw new NoAvailableSlaveException();
88         bestscore = (SlaveScore) iter.next();
89         logger.debug(bestscore);
90         for (; iter.hasNext();) {
91             SlaveScore score = (SlaveScore) iter.next();
92             logger.debug(score);
93             if (score.getScore() > bestscore.getScore()) {
94                 bestscore = score;
95             }
96         }
97         if (bestscore == null) throw new NoAvailableSlaveException();
98         return bestscore;
99     }
100
101     public SlaveScore getSlaveScore(RemoteSlave rslave)
102         throws ObjectNotFoundException {
103         for (Iterator JavaDoc iter = _scoreChart.iterator(); iter.hasNext();) {
104             SlaveScore score = (SlaveScore) iter.next();
105             if (score.getRSlave().equals(rslave))
106                 return score;
107         }
108         throw new ObjectNotFoundException(rslave.getName()+" not in ScoreChart");
109     }
110
111     public Collection JavaDoc getSlaveScores() {
112         return _scoreChart;
113     }
114
115     public void removeSlaveScore(RemoteSlave rslave) {
116         for (Iterator JavaDoc iter = _scoreChart.iterator(); iter.hasNext();) {
117             SlaveScore score = (SlaveScore) iter.next();
118             if(score.getRSlave().equals(rslave))
119                 iter.remove();
120         }
121         
122     }
123
124 }
125
Popular Tags