Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Creating the JaceP2P repository.
[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
17         @SuppressWarnings("unchecked")
18         public synchronized ListeTask clone() {
19                 ListeTask l = new ListeTask();
20                 l.liste = (Vector<TaskId>) liste.clone();
21                 l.task = task;
22                 return (l);
23         }
24
25         // methods
26         public synchronized void addTask(TaskId tsk) {
27                 liste.addElement(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.out
40                                                 .println("There is no task affected to this host stub !!!!! ");
41                                 return null;
42                         }
43                 }
44         }
45
46         public synchronized TaskId getTaskIdOfRank(int rank) {
47                 int is = -1;
48                 if (liste.isEmpty()) {
49                         return null;
50                 } else {
51                         is = existRank(rank);
52                         if (is != -1) {
53                                 return (TaskId) liste.get(is);
54                         } else {
55                                 System.out.println("TaskId of rank=" + rank + " doesn't exist");
56                                 viewAll();
57                                 return null;
58                         }
59                 }
60         }
61
62         public synchronized int getSize() {
63                 return liste.size();
64         }
65
66         private synchronized int existHostStub(JaceInterface host) {
67                 int existe = -1;
68                 int index = 0;
69                 while ((existe == -1) && (index < liste.size())) {
70                         if (host.equals(((TaskId) liste.get(index)).getHostStub())) {
71                                 existe = index;
72                         } else
73                                 index++;
74                 }
75                 return existe;
76         }
77
78         private synchronized int existRank(int rank) {
79                 int existe = -1;
80                 int index = 0;
81                 while ((existe == -1) && (index < liste.size())) {
82                         if (((TaskId) liste.get(index)).getRank() == rank) {
83                                 existe = index;
84                         } else
85                                 index++;
86                 }
87                 return existe;
88         }
89
90         public synchronized TaskId get(int i) {
91                 return (TaskId) liste.get(i);
92         }
93
94         public synchronized void viewAll() {
95                 TaskId maTaskId = null;
96                 System.out.println("** Tasks **\n");
97                 if (liste.size() == 0) {
98                         System.out.println("No task");
99                 } else {
100                         for (int i = 0; i < liste.size(); i++) {
101                                 maTaskId = get(i);
102                                 System.out.println("\tTaskId " + i + ", rank : "
103                                                 + maTaskId.getRank() + ", hostName : "
104                                                 + maTaskId.getHostName());
105                         }
106                 }
107                 
108                 System.out.println("*****  *****\n");
109         }
110
111 }
112
113 /** ! **/
114