Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Minor corrections.
[jaceP2P.git] / src / jaceP2P / ListeTask.java
1 package jaceP2P;
2
3 import java.util.Vector;
4
5 public class ListeTask implements java.io.Serializable, java.lang.Cloneable 
6 {
7         private static final long serialVersionUID = 1L ;
8
9         // attributes
10         protected Vector<TaskId> liste = new Vector<TaskId>() ;
11         protected TaskId task ;
12
13         // constructor
14         public ListeTask() {} 
15
16         @SuppressWarnings("unchecked")
17         public synchronized ListeTask clone() {
18                 ListeTask l = new ListeTask();
19                 l.liste = (Vector<TaskId>) liste.clone();
20                 l.task = task;
21                 return (l);
22         }
23
24         // methods
25         public synchronized void addTask(TaskId tsk) {
26                 liste.addElement(tsk);
27         }
28
29         public synchronized TaskId getTaskIdOfHostStub(JaceInterface hostStub) {
30                 int is = -1;
31                 if (liste.isEmpty()) {
32                         return null;
33                 } else {
34                         is = existHostStub(hostStub);
35                         if (is != -1) {
36                                 return (TaskId) liste.get(is);
37                         } else {
38                                 System.err
39                                                 .println("There is no task affected to this host stub !");
40                                 return null;
41                         }
42                 }
43         }
44
45         public synchronized TaskId getTaskIdOfRank(int rank) {
46                 int is = -1;
47                 if (liste.isEmpty()) {
48                         return null;
49                 } else {
50                         is = existRank(rank);
51                         if (is != -1) {
52                                 return (TaskId) liste.get(is);
53                         } else {
54                                 System.err.println("TaskId of rank=" + rank + " doesn't exist");
55                                 viewAll();
56                                 return null;
57                         }
58                 }
59         }
60
61         public synchronized int getSize() {
62                 return liste.size();
63         }
64
65         private synchronized int existHostStub(JaceInterface host) {
66                 int existe = -1;
67                 int index = 0;
68                 while ((existe == -1) && (index < liste.size())) {
69                         if (host.equals(((TaskId) liste.get(index)).getHostStub())) {
70                                 existe = index;
71                         } else
72                                 index++;
73                 }
74                 return existe;
75         }
76
77         private synchronized int existRank(int rank) {
78                 int existe = -1;
79                 int index = 0;
80                 while ((existe == -1) && (index < liste.size())) {
81                         if (((TaskId) liste.get(index)).getRank() == rank) {
82                                 existe = index;
83                         } else
84                                 index++;
85                 }
86                 return existe;
87         }
88
89         public synchronized TaskId get(int i) {
90                 return (TaskId) liste.get(i);
91         }
92
93         public synchronized void viewAll() {
94                 TaskId maTaskId = null;
95                 System.out.println("** Tasks **\n");
96                 if (liste.size() == 0) {
97                         System.out.println("No task");
98                 } else {
99                         for (int i = 0; i < liste.size(); i++) {
100                                 maTaskId = get(i);
101                                 System.out.println("\tTaskId " + i + ", rank : "
102                                                 + maTaskId.getRank() + ", hostName : "
103                                                 + maTaskId.getHostName());
104                         }
105                 }
106                 
107                 System.out.println("*****  *****\n");
108         }
109
110 }
111
112 /** ! **/
113