Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correction of some bugs and performance enhancement.
[jaceP2P.git] / src / jaceP2P / ListeTask.java
1 package jaceP2P;
2
3 import java.util.ArrayList;
4
5
6 public class ListeTask implements java.io.Serializable, java.lang.Cloneable 
7 {
8         private static final long serialVersionUID = 1L ;
9
10         // attributes
11         protected ArrayList<TaskId> liste = new ArrayList<TaskId>() ;
12         protected TaskId task ;
13
14         // constructor
15         public ListeTask() {} 
16
17         @SuppressWarnings("unchecked")
18         public synchronized ListeTask clone() {
19                 ListeTask l = new ListeTask();
20                 l.liste = (ArrayList<TaskId>) liste.clone();
21                 l.task = task;
22                 return (l);
23         }
24
25         // methods
26         public synchronized void addTask(TaskId tsk) {
27                 liste.add( tsk ) ;
28         }
29
30         public synchronized TaskId getTaskIdOfHostStub(JaceInterface hostStub) {
31                 int is = -1;
32                 if (liste.isEmpty()) {
33                         return null;
34                 } else {
35                         is = existHostStub(hostStub);
36                         if (is != -1) {
37                                 return (TaskId) liste.get(is);
38                         } else {
39                                 System.err.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