1 /* Copyright (c) 2012-2019. The SimGrid Team.
2 * All rights reserved. */
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. */
8 import java.util.ArrayList;
9 import java.util.Collections;
11 /* Answer to a "FIND_NODE" query. Contains the nodes closest to an id given */
13 private int destinationId;
14 /* Closest nodes in the answer. */
15 private ArrayList<Contact> nodes;
17 public Answer(int destinationId) {
18 this.destinationId = destinationId;
19 nodes = new ArrayList<>();
22 protected int getDestinationId() {
26 protected ArrayList<Contact> getNodes() {
30 protected int size() {
35 if (nodes.size() > Common.BUCKET_SIZE)
36 nodes.subList(nodes.size() - Common.BUCKET_SIZE, nodes.size()).clear();
39 public void add(Contact contact) {
43 /* Merge the contents of this answer with another answer */
44 public int merge(Answer answer) {
47 for (Contact c: answer.getNodes()) {
48 if (!nodes.contains(c)) {
53 Collections.sort(nodes);
60 /* Returns if the destination has been found */
61 public boolean destinationFound() {
62 if (nodes.isEmpty()) {
65 Contact tail = nodes.get(0);
66 return tail.getDistance() == 0;
70 public String toString() {
71 return "Answer [destinationId=" + destinationId + ", nodes=" + nodes + "]";