Logo AND Algorithmique Numérique Distribuée

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