Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
04581f329205e940a095cfff059eeb51f78392a9
[simgrid.git] / simgrid-java / examples / kademlia / Bucket.java
1 /* Copyright (c) 2012. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6 package kademlia;
7
8 import java.util.ArrayList;
9
10 /**
11  * Stores the information held in a bucket
12  */
13 public class Bucket {
14         private ArrayList<Integer> nodes;
15         private int id;
16         
17         /**
18          * Constructor
19          */
20         public Bucket(int id) {
21                 this.nodes = new ArrayList<Integer>();
22                 this.id = id;
23         }
24         /**
25          * Returns the bucket's id.
26          */
27         public int getId() {
28                 return this.id;
29         }
30         /**
31          * Returns how many nodes there is in the bucket
32          */
33         public int size() {
34                 return nodes.size();
35         }
36         /**
37          * Returns if the bucket contains the element
38          */
39         public boolean contains(int id) {
40                 return nodes.contains(id);
41         }
42         /**
43          * Add an to the front of the bucket
44          */
45         public void add(int id) {
46                 nodes.add(0,id);
47         }
48         /**
49          * Pushs an element into the front of a bucket.
50          */
51         public void pushToFront(int id) {
52                 int i = nodes.indexOf(id);
53                 nodes.remove(i);
54                 nodes.add(0, id);
55         }
56         /**
57          * Returns a node
58          */
59         public int getNode(int id) {
60                 return nodes.get(id);
61         }
62         /**
63          * Adds the content of the bucket into a answer object.
64          */
65         public void addToAnswer(Answer answer, int destination) {
66                 for (int id : this.nodes) {
67                         answer.getNodes().add(new Contact(id,id ^ destination));
68                 }
69         }
70         
71         @Override
72         public String toString() {
73                 return "Bucket [id= " + id + " nodes=" + nodes + "]";
74         }
75         
76 }