Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / examples / java / dht / kademlia / Bucket.java
1 /* Copyright (c) 2012-2019. 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 dht.kademlia;
8 import java.util.ArrayList;
9
10 public class Bucket {
11   private ArrayList<Integer> nodes;
12   private int id;
13
14   public Bucket(int id) {
15     this.nodes = new ArrayList<>();
16     this.id = id;
17   }
18
19   public int getId() {
20     return this.id;
21   }
22
23   public int size() {
24     return nodes.size();
25   }
26
27   public boolean contains(int id) {
28     return nodes.contains(id);
29   }
30
31   /* Add a node to the front of the bucket */
32   public void add(int id) {
33     nodes.add(0,id);
34   }
35
36   /* Push a node to the front of a bucket */
37   public void pushToFront(int id) {
38     int i = nodes.indexOf(id);
39     nodes.remove(i);
40     nodes.add(0, id);
41   }
42
43   public int getNode(int id) {
44     return nodes.get(id);
45   }
46
47   /* Add the content of the bucket into a answer object. */
48   public void addToAnswer(Answer answer, int destination) {
49     for (int nodeId : this.nodes) {
50       answer.getNodes().add(new Contact(nodeId,nodeId ^ destination));
51     }
52   }
53
54   @Override
55   public String toString() {
56     return "Bucket [id= " + id + " nodes=" + nodes + "]";
57   }
58 }