Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Création du dépôt AIL-PA
[ail-pa.git] / src / and / AIL / MessageList.java
1 package and.AIL ;
2
3 import java.util.ArrayList;
4
5 /**
6  * Class which contains and manages a messages list.
7  * @author Sébastien Miquée
8  * @version 3.0
9  */
10 public class MessageList
11 {
12         /** Local variables **/
13         private ArrayList<Message> tab[] ;
14         private int taille ;
15
16         /**
17          * Empty constructor for stub generation.
18          */
19         public MessageList() 
20         {
21         }
22
23         /**
24          * Constructor.
25          * @param nb : size of the list.
26          */
27         @SuppressWarnings( "unchecked" )
28         public MessageList( Integer nb )
29         {
30                 taille = nb.intValue() ;
31
32                 tab = new ArrayList[ taille ] ;
33                 for( int i = 0 ; i < taille ; i++ )
34                 {
35                         tab[i] = new ArrayList<Message>() ;
36                 }
37         }
38         
39         /**
40          * Search and returns, if it exists, a Message in the list _noList
41          * with the tag _tag. Returns null if there is no Message. 
42          * @param _noList : index of the list.
43          * @param _tag : the tag of the Message.
44          * @return : the Message.
45          */
46         public synchronized Message searchMsg( int _noList, int _tag )
47         {               
48                 if( _noList >= 0 && _noList < taille )
49                 {       
50                         int pos = searchPos( _noList, _tag ) ;
51                                                         
52                         if( pos != -1 )
53                         {
54                                 return tab[ _noList ].remove( pos ) ;
55                         }
56                 }
57                 
58                 return null ;
59         }
60
61         /**
62          * Add a Message _mes in the list of index _noList.
63          * @param _noList : index of the list.
64          * @param _mes : Message to add.
65          */
66         public synchronized void addMessage( int _noList, Message _mes )
67         {
68                 if( _noList >= 0 && _noList < taille )
69                 {
70                         int _t = _mes.getTag() ;
71                         /** Search if message exists **/
72                         int pos = searchPos( _noList, _t ) ;
73
74                         if( pos != -1 )
75                         {
76                                 tab[ _noList ].set( pos, _mes ) ;
77                         } else {
78                                 /** Add the message **/
79                                 tab[ _noList ].add( _mes ) ;
80                         }                       
81                 }
82         }
83
84
85         /**
86          * Search the position of a Message with a specific tag _tag.
87          * If it doesn't exist, -1 will be returned.
88          * @param id : index of the sender.
89          * @param tag : tag of the Message.
90          * @return : the position in the list.
91          */
92         private int searchPos( int _id, int _tag )
93         {
94                 int t = tab[ _id ].size() ;
95                 
96                 for( int i = 0 ; i < t ; i++ )
97                 {
98                         try {
99                                 if( tab[ _id ].get( i ).getTag() == _tag )
100                                 {
101                                         return i ;
102                                 }
103                         } catch( Exception e ) {
104                                 return -1 ;
105                         }
106                 }
107                 
108                 return -1 ;
109         }
110 }
111
112 /** La programmation est un art, respectons ceux qui la pratiquent !! **/