Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[simgrid.git] / examples / java / dht / kademlia / Contact.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
9 public class Contact implements Comparable<Object> {
10   private int id;
11   private int distance;
12
13   public Contact(int id, int distance) {
14     this.id = id;
15     this.distance = distance;
16   }
17
18   public int getId() {
19     return id;
20   }
21
22   public int getDistance() {
23     return distance;
24   }
25
26   @Override
27   public boolean equals(Object x) {
28     return x != null && x.equals(id);
29   }
30
31   @Override
32   public int hashCode() {
33     int hash = 1;
34     hash = hash * 17 + id;
35     hash = hash * 31 + distance;
36     return hash;
37   }
38
39   @Override
40   public int compareTo(Object o) {
41     Contact c = (Contact)o;
42     if (distance < c.distance) {
43       return -1;
44     }
45     else if (distance == c.distance) {
46       return 0;
47     }
48     else {
49       return 1;
50     }
51   }
52
53   @Override
54   public String toString() {
55     return "Contact [id=" + id + ", distance=" + distance + "]";
56   }
57
58 }