📄 similarset.java
字号:
//int index; if (!test(handle)) { return false; } if (theSize < nodes.length) { nodes[theSize] = handle; //index = theSize; theSize++; } else { theSize--; if (leafSet.isProperlyRemoved(nodes[theSize])) { if (leafSet.observe) { notifyListeners(nodes[theSize], false); } } if (leafSet.observe) { nodes[theSize].deleteObserver(this); } theSize++; nodes[theSize - 1] = handle; //index = theSize-1; } // bubble the new node into the correct position if (clockwise) { for (int i = theSize - 1; i > 0; i--) { if (nid.isBetween(ln.getNodeId(), nodes[i - 1].getNodeId())) { swap(i, i - 1); } else { break; } } } else { for (int i = theSize - 1; i > 0; i--) { if (nid.isBetween(nodes[i - 1].getNodeId(), ln.getNodeId())) { swap(i, i - 1); } else { break; } } } if (!leafSet.testOtherSet(this, handle)) { if (leafSet.observe) { notifyListeners(handle, true); } } // register as an observer, so we'll be notified if the handle is declared // dead if (leafSet.observe) { handle.addObserver(this); } return true; } /** * Generates too many objects to use this interface * * @param o The feature to be added to the Observer attribute * @deprecated use addNodeSetListener */ public void addObserver(Observer o) {// if (logger.level <= Logger.WARNING) logger.log("WARNING: Observer on RoutingTable is deprecated"); super.addObserver(o); } /** * Generates too many objects to use this interface * * @param o DESCRIBE THE PARAMETER * @deprecated use removeNodeSetListener */ public void deleteObserver(Observer o) {// if (logger.level <= Logger.WARNING) logger.log("WARNING: Observer on RoutingTable is deprecated"); super.deleteObserver(o); } /** * Adds a feature to the NodeSetListener attribute of the SimilarSet object * * @param listener The feature to be added to the NodeSetListener attribute */ public void addNodeSetListener(NodeSetListener listener) { synchronized (listeners) { listeners.add(listener); } } /** * DESCRIBE THE METHOD * * @param listener DESCRIBE THE PARAMETER */ public void removeNodeSetListener(NodeSetListener listener) { synchronized (listeners) { listeners.remove(listener); } } /** * DESCRIBE THE METHOD * * @param handle DESCRIBE THE PARAMETER * @param added DESCRIBE THE PARAMETER */ private void notifyListeners(NodeHandle handle, boolean added) { // pass the event to the Observers of this RoutingTable synchronized (listeners) { for (int i = 0; i < listeners.size(); i++) { ((NodeSetListener) listeners.get(i)).nodeSetUpdate(this, handle, added); } } // handle deprecated interface if (countObservers() > 0) { setChanged(); notifyObservers(new NodeSetUpdate(handle, added)); } } /** * Is called by the Observer pattern whenever the liveness or proximity of a * registered node handle is changed. * * @param o The node handle * @param arg the event type (PROXIMITY_CHANGE, DECLARED_LIVE, DECLARED_DEAD) */ public void update(Observable o, Object arg) { // if the node is declared dead, remove it immediately if (((Integer) arg) == NodeHandle.DECLARED_DEAD) { remove((NodeHandle) o); } if (((Integer) arg) == NodeHandle.DECLARED_LIVE) { leafSet.put((NodeHandle) o); } } /** * Verifies if the set contains this particular id. * * @param nid a node id. * @return true if that node id is in the set, false otherwise. */ public boolean member(NodeHandle nid) { for (int i = 0; i < theSize; i++) { if (nodes[i].equals(nid)) { return true; } } return false; } /** * @param nid DESCRIBE THE PARAMETER * @return DESCRIBE THE RETURN VALUE */ public boolean member(Id nid) { for (int i = 0; i < theSize; i++) { if (nodes[i].getId().equals(nid)) { return true; } } return false; } /** * Removes a node id and its handle from the set. * * @param nid the node to remove. * @return the node handle removed or null if nothing. */ public NodeHandle remove(Id nid) { for (int i = 0; i < theSize; i++) { if (nodes[i].getNodeId().equals(nid)) { return remove(i); } } return null; } /** * DESCRIBE THE METHOD * * @param nh DESCRIBE THE PARAMETER * @return DESCRIBE THE RETURN VALUE */ public NodeHandle remove(NodeHandle nh) { for (int i = 0; i < theSize; i++) { if (nodes[i].equals(nh)) { return remove(i); } } return null; } /** * Removes a node id and its handle from the set. * * @param i the index of the node to remove. * @return the node handle removed or null if nothing. */ protected NodeHandle remove(int i) { if (i < 0 || i >= theSize) { return null; } NodeHandle handle = nodes[i]; for (int j = i + 1; j < theSize; j++) { nodes[j - 1] = nodes[j]; } theSize--; if (leafSet.isProperlyRemoved(handle)) { if (leafSet.observe) { notifyListeners(handle, false); } } if (leafSet.observe) { handle.deleteObserver(this); } return handle; } /** * Gets the current size of this set. * * @return the size. */ public int size() { return theSize; } /** * Impl that doesn't produce garbage Numerically closest node to a given a * node. Returns -1 if the local Id is the most similar and returns an index * otherwise. * * @param nid a node id. * @return -1 if the local Id is most similar, else the index of the most * similar node. */ public int mostSimilar(Id nid) { if (theSize == 0) { return -1; } Id.Distance other = d; Id.Distance minDist = ln.getNodeId().distance(nid, d1); int min = -1; for (int i = 0; i < theSize; i++) { other = nodes[i].getNodeId().distance(nid, other); int cmp = other.compareTo(minDist); if ((!clockwise && cmp < 0) || (clockwise && cmp <= 0)) { // swap buffers Id.Distance tmp = minDist; minDist = other; other = tmp; min = i; } } return min; } // Common API Support /** * Puts a NodeHandle into the set. * * @param handle the handle to put. * @return true if the put succeeded, false otherwise. */ public boolean putHandle(rice.p2p.commonapi.NodeHandle handle) { return put((NodeHandle) handle); } /** * Verifies if the set contains this particular id. * * @param id a node id. * @return true if that node id is in the set, false otherwise. */ public boolean memberHandle(rice.p2p.commonapi.Id id) { return member((Id) id); } /** * Removes a node id and its handle from the set. * * @param id DESCRIBE THE PARAMETER * @return the node handle removed or null if nothing. */ public rice.p2p.commonapi.NodeHandle removeHandle(rice.p2p.commonapi.Id id) { return remove((Id) id); } /** * DESCRIBE THE METHOD * * @param newLeafSet DESCRIBE THE PARAMETER * @return DESCRIBE THE RETURN VALUE */ SimilarSet copy(LeafSet newLeafSet) { return new SimilarSet(this, newLeafSet); } /** * DESCRIBE THE METHOD * * @param in DESCRIBE THE PARAMETER * @exception IOException DESCRIBE THE EXCEPTION * @exception ClassNotFoundException DESCRIBE THE EXCEPTION */ private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { in.defaultReadObject(); d1 = new Id.Distance(); d = new Id.Distance(); listeners = new ArrayList(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -