2 * Copyright (c) 2003-2005 The BISON Project
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.
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.
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.
19 package peersim.vector;
21 import peersim.core.*;
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.
30 * @author Alberto Montresor
31 * @version $Revision: 1.4 $
33 public class SingleValueComparator implements Comparator
36 /** Protocol to be be compared */
40 * Builds a new comparator that compares the double values maintained
41 * by protocol identified by <code>pid</code>.
43 public SingleValueComparator(int pid) { this.pid = pid; }
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.
51 public int compare(Object o1, Object o2)
53 SingleValue s1 = (SingleValue) ((Node) o1).getProtocol(pid);
54 SingleValue s2 = (SingleValue) ((Node) o2).getProtocol(pid);
55 return (int) (s1.getValue() - s2.getValue());