Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Energy, onHostDestruction: ensured ptr existence
[simgrid.git] / contrib / psg / src / peersim / vector / VectorComparator.java
1 /*
2  * Copyright (c) 2006 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 java.lang.reflect.*;
22 import java.util.*;
23
24 import peersim.core.*;
25
26 /**
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.
33  * <br>
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>:
37  * <PRE>
38  * Comparator c = new VectorComparator(pid, "getValue");
39  * Array.sort(Node[] array, c);
40  * </PRE>
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
45  * classes.
46  *
47  * @author Alberto Montresor
48  * @version $Revision: 1.1 $
49  */
50 public class VectorComparator implements Comparator
51 {
52
53 //--------------------------------------------------------------------------
54 //Fields
55 //--------------------------------------------------------------------------
56
57 /** Protocol identifier of the protocol to be observed */
58 private final int pid;
59
60 /** The getter to be used to obtain comparable values */
61 private final Method method;
62
63 //--------------------------------------------------------------------------
64 //Initialization
65 //--------------------------------------------------------------------------
66
67 public VectorComparator(int pid, String methodName)
68 {
69         this.pid = pid;
70         Node n = Network.prototype;
71         if (n == null) {
72                 throw new IllegalStateException("No prototype node can be used to search methods");
73         }
74         Object p = n.getProtocol(pid);
75         Class c = p.getClass();
76         try {
77                 method = GetterSetterFinder.getGetterMethod(c, methodName);
78         } catch (NoSuchMethodException e) {
79                 throw new IllegalArgumentException(e.getMessage());
80         }
81 }
82
83
84 public int compare(Object o1, Object o2)
85 {
86         try {
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());
94         }
95 }
96
97 }