001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.drftpd.slaveselection.filter;
019:
020: import net.sf.drftpd.NoAvailableSlaveException;
021: import net.sf.drftpd.ObjectNotFoundException;
022:
023: import org.apache.log4j.Logger;
024: import org.drftpd.master.RemoteSlave;
025:
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Iterator;
029:
030: /**
031: * @author mog
032: * @version $Id: ScoreChart.java 849 2004-12-02 05:21:41Z mog $
033: */
034: public class ScoreChart {
035: private static final Logger logger = Logger
036: .getLogger(ScoreChart.class);
037:
038: /**
039: * Contains {@link SlaveScore} elements.
040: */
041: private ArrayList<SlaveScore> _scoreChart;
042:
043: /**
044: * @param slaves Collection of ONLINE slaves
045: * (this is not verified by scorechart, you can use offline slaves for JUnit tests etc.)
046: */
047: public ScoreChart(Collection<RemoteSlave> slaves) {
048: _scoreChart = new ArrayList<SlaveScore>();
049: for (RemoteSlave rslave : slaves) {
050: _scoreChart.add(new SlaveScore(rslave));
051: }
052: }
053:
054: public RemoteSlave getBestSlave() throws NoAvailableSlaveException {
055: return getBestSlaveScore().getRSlave();
056: }
057:
058: public SlaveScore getBestSlaveScore()
059: throws NoAvailableSlaveException {
060: SlaveScore bestscore;
061: Iterator iter = getSlaveScores().iterator();
062:
063: if (!iter.hasNext()) {
064: throw new NoAvailableSlaveException();
065: }
066:
067: bestscore = (SlaveScore) iter.next();
068: logger.debug(bestscore);
069:
070: for (; iter.hasNext();) {
071: SlaveScore score = (SlaveScore) iter.next();
072: logger.debug(score);
073:
074: if (score.getScore() > bestscore.getScore()) {
075: bestscore = score;
076: }
077: }
078:
079: if (bestscore == null) {
080: throw new NoAvailableSlaveException();
081: }
082:
083: return bestscore;
084: }
085:
086: /**
087: * Returns the {@link SlaveScore} entry for the RemoteSlave rslave.
088: * @param rslave The RemoteSlave to get the SlaveScore for.
089: */
090: public SlaveScore getSlaveScore(RemoteSlave rslave)
091: throws ObjectNotFoundException {
092: for (Iterator iter = _scoreChart.iterator(); iter.hasNext();) {
093: SlaveScore score = (SlaveScore) iter.next();
094:
095: if (score.getRSlave().equals(rslave)) {
096: return score;
097: }
098: }
099:
100: throw new ObjectNotFoundException(rslave.getName()
101: + " not in ScoreChart");
102: }
103:
104: /**
105: * Returns the Collection holding the {@link SlaveScore} elements.
106: */
107: public ArrayList<SlaveScore> getSlaveScores() {
108: return _scoreChart;
109: }
110:
111: public void removeSlaveScore(RemoteSlave rslave) {
112: for (Iterator iter = _scoreChart.iterator(); iter.hasNext();) {
113: SlaveScore score = (SlaveScore) iter.next();
114:
115: if (score.getRSlave().equals(rslave)) {
116: iter.remove();
117: }
118: }
119: }
120:
121: public static class SlaveScore implements Comparable {
122: private RemoteSlave _rslave;
123: private long _score;
124:
125: public SlaveScore(RemoteSlave rslave) {
126: _rslave = rslave;
127: }
128:
129: public void addScore(long score) {
130: //logger.debug("Added "+score+" to "+getRSlave().getName());
131: _score += score;
132: }
133:
134: public int compareTo(Object o) {
135: SlaveScore s = (SlaveScore) o;
136:
137: //int thisVal = this.value;
138: //int anotherVal = anotherInteger.value;
139: return ((getScore() < s.getScore()) ? (-1)
140: : ((getScore() == s.getScore()) ? 0 : 1));
141: }
142:
143: public RemoteSlave getRSlave() {
144: return _rslave;
145: }
146:
147: public long getScore() {
148: return _score;
149: }
150:
151: public String toString() {
152: return "SlaveScore[rslave=" + getRSlave().getName()
153: + ",score=" + getScore() + "]";
154: }
155: }
156:
157: public boolean isEmpty() {
158: return getSlaveScores().isEmpty();
159: }
160: }
|