1 /* Copyright (c) 2012-2014, 2016. 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<Contact>();
22 int getDestinationId() {
26 ArrayList<Contact> getNodes() {
34 public void remove(int index) {
38 public void add(Contact contact) {
42 /* Merge the contents of this answer with another answer */
43 public int merge(Answer answer) {
46 for (Contact c: answer.getNodes()) {
47 if (!nodes.contains(c)) {
52 Collections.sort(nodes);
54 while (answer.size() > Common.BUCKET_SIZE) {
55 answer.remove(answer.size() - 1);
60 /* Returns if the destination has been found */
61 public boolean destinationFound() {
62 if (nodes.size() < 1) {
65 Contact tail = nodes.get(0);
66 return tail.getDistance() == 0;
70 public String toString() {
71 return "Answer [destinationId=" + destinationId + ", nodes=" + nodes + "]";