1 /* Copyright (c) 2012-2014. 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. */
9 import java.util.ArrayList;
10 import java.util.Collections;
13 * Answer to a "FIND_NODE" query. Contains the nodes closest to
18 * Id of the node we're trying to find
20 private int destinationId;
22 * Closest nodes in the answer.
24 private ArrayList<Contact> nodes;
29 public Answer(int destinationId) {
30 this.destinationId = destinationId;
31 nodes = new ArrayList<Contact>();
34 * Returns the destination id
36 int getDestinationId() {
40 * Returns the list of the nodes in the answer
42 ArrayList<Contact> getNodes() {
46 * Returns the answer array size
52 * Remove an element from the answer.
54 public void remove(int index) {
58 * Add a contact to the answer.
60 public void add(Contact contact) {
64 * Merge the contents of this answer with another answer
66 public int merge(Answer answer) {
69 for (Contact c: answer.getNodes()) {
70 if (!nodes.contains(c)) {
75 Collections.sort(nodes);
77 while (answer.size() > Common.BUCKET_SIZE) {
78 answer.remove(answer.size() - 1);
83 * Returns if the destination has been found
85 public boolean destinationFound() {
86 if (nodes.size() < 1) {
89 Contact tail = nodes.get(0);
90 return tail.getDistance() == 0;
93 public String toString() {
94 return "Answer [destinationId=" + destinationId + ", nodes=" + nodes