package net.tomp2p.message;

import java.util.Collection;
import java.util.Iterator;

import net.tomp2p.peers.PeerAddress;

public class NeighborSet {
    private final int neighborLimit;
    private final Collection<PeerAddress> neighbors;

    public NeighborSet(final int neighborLimit, final Collection<PeerAddress> neighbors) {
        this.neighborLimit = neighborLimit;
        this.neighbors = neighbors;
        // remove neighbors that are over the limit
        int serializedSize = 1;
        // no need to cut if we don't provide a limit
        if (neighborLimit < 0) {
            return;
        }
        for (Iterator<PeerAddress> iterator = neighbors.iterator(); iterator.hasNext();) {
            PeerAddress neighbor = iterator.next();
            serializedSize += neighbor.size();
            if (serializedSize > neighborLimit) {
                iterator.remove();
            }
        }
    }

    public Collection<PeerAddress> neighbors() {
        return neighbors;
    }

    public void addResult(Collection<PeerAddress> neighbors) {
        this.neighbors.addAll(neighbors);
    }

    public void add(PeerAddress neighbor) {
        this.neighbors.add(neighbor);
    }

    public int size() {
        return this.neighbors.size();
    }

    public int neighborLimit() {
        return neighborLimit;
    }
}

@@ -103,26 +103,92 @@
 p.peers.
-PeerAddres
+Number160;%0Aimport net.tomp2p.peers.PeerAddress;%0Aimport net.tomp2p.utils.Util
 s;%0A%0Apubl
@@ -1400,10 +1400,727 @@
 ;%0A    %7D%0A
+    %0A    @Override%0A    public boolean equals(Object obj) %7B%0A    %09if (!(obj instanceof NeighborSet)) %7B%0A            return false;%0A        %7D%0A        if (obj == this) %7B%0A            return true;%0A        %7D%0A        final NeighborSet other = (NeighborSet) obj;%0A%0A        boolean t1 = this.neighborLimit == other.neighborLimit;%0A        boolean t2 = Utils.isSameSets(this.neighbors, other.neighbors);%0A        %0A        return t1 && t2;%0A    %7D%0A    %0A    @Override%0A    public int hashCode() %7B%0A    %09// TODO check correctness%0A    %09int hash = 5;%0A        hash = 89 * hash + (this.neighbors != null ? this.neighbors.hashCode() : 0);%0A        hash = 89 * hash + (this.neighborLimit %5E (this.neighborLimit %3E%3E%3E 32));%0A        return hash;%0A    %7D%0A
 %7D%0A

implement equals() and hashCode() for NeighborSet