2 * Copyright (c) 2006 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 java.lang.reflect.*;
24 import peersim.core.*;
27 * This class provides a generic implementation of the
28 * <code>java.lang.Comparator<code> interface specialized
29 * for {@link Node} objects. Nodes are compared based
30 * on one of their protocols, on which a configurable
31 * method is invoked. Both the protocol id and the
32 * method are specified in the constructor.
34 * This comparator can be used, for example, to sort
35 * an array of nodes based on method <code>getValue</code>
36 * associated to the protocol <code>pid</code>:
38 * Comparator c = new VectorComparator(pid, "getValue");
39 * Array.sort(Node[] array, c);
41 * Note that differently from other classes in this package,
42 * VectorComparator is declared programmatically in the code
43 * and not in the configuration file. It is included in this
44 * package because it shares the same philosophy of the other
47 * @author Alberto Montresor
48 * @version $Revision: 1.1 $
50 public class VectorComparator implements Comparator
53 //--------------------------------------------------------------------------
55 //--------------------------------------------------------------------------
57 /** Protocol identifier of the protocol to be observed */
58 private final int pid;
60 /** The getter to be used to obtain comparable values */
61 private final Method method;
63 //--------------------------------------------------------------------------
65 //--------------------------------------------------------------------------
67 public VectorComparator(int pid, String methodName)
70 Node n = Network.prototype;
72 throw new IllegalStateException("No prototype node can be used to search methods");
74 Object p = n.getProtocol(pid);
75 Class c = p.getClass();
77 method = GetterSetterFinder.getGetterMethod(c, methodName);
78 } catch (NoSuchMethodException e) {
79 throw new IllegalArgumentException(e.getMessage());
84 public int compare(Object o1, Object o2)
87 Comparable c1 = (Comparable) method.invoke(((Node) o1).getProtocol(pid));
88 Comparable c2 = (Comparable) method.invoke(((Node) o2).getProtocol(pid));
89 return c1.compareTo(c2);
90 } catch (InvocationTargetException e) {
91 throw new RuntimeException(e.getCause().getMessage());
92 } catch (IllegalAccessException e) {
93 throw new RuntimeException(e.getCause().getMessage());