Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill another out of date script
[simgrid.git] / contrib / psg / src / peersim / vector / SingleValueComparator.java
1 /*
2  * Copyright (c) 2003-2005 The BISON Project
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16  *
17  */
18
19 package peersim.vector;
20
21 import peersim.core.*;
22 import java.util.*;
23
24 /**
25  * This comparator class compares two node objects based on the value 
26  * maintained by one of its protocols. The protocol must implement the
27  * {@link SingleValue} interface; its identifier has to be specified when a
28  * new comparator is built.
29  *
30  * @author Alberto Montresor
31  * @version $Revision: 1.4 $
32  */
33 public class SingleValueComparator implements Comparator 
34 {
35
36 /** Protocol to be be compared */
37 private int pid;
38
39 /**
40  * Builds a new comparator that compares the double values maintained
41  * by protocol identified by <code>pid</code>.
42  */
43 public SingleValueComparator(int pid) { this.pid = pid; }
44
45 /**
46  * Compares the values of two protocols. The parameters must have dynamic type
47  * {@link Node}. The protocol {@link #pid} is accessed on both nodes. These
48  * protocols have to implement the {@link SingleValue} interface. The values
49  * held by these protocol instances are then compared.
50  */
51 public int compare(Object o1, Object o2)
52 {
53         SingleValue s1 = (SingleValue) ((Node) o1).getProtocol(pid);
54         SingleValue s2 = (SingleValue) ((Node) o2).getProtocol(pid);
55         return (int) (s1.getValue() - s2.getValue());
56 }
57         
58 }