⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 similarset.java

📁 pastry的java实现的2.0b版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*************************************************************************"FreePastry" Peer-to-Peer Application Development Substrate Copyright 2002, Rice University. All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:- Redistributions of source code must retain the above copyrightnotice, this list of conditions and the following disclaimer.- Redistributions in binary form must reproduce the above copyrightnotice, this list of conditions and the following disclaimer in thedocumentation and/or other materials provided with the distribution.- Neither  the name  of Rice  University (RICE) nor  the names  of itscontributors may be  used to endorse or promote  products derived fromthis software without specific prior written permission.This software is provided by RICE and the contributors on an "as is"basis, without any representations or warranties of any kind, expressor implied including, but not limited to, representations orwarranties of non-infringement, merchantability or fitness for aparticular purpose. In no event shall RICE or contributors be liablefor any direct, indirect, incidental, special, exemplary, orconsequential damages (including, but not limited to, procurement ofsubstitute goods or services; loss of use, data, or profits; orbusiness interruption) however caused and on any theory of liability,whether in contract, strict liability, or tort (including negligenceor otherwise) arising in any way out of the use of this software, evenif advised of the possibility of such damage.********************************************************************************/package rice.pastry.leafset;import rice.pastry.*;import java.util.*;import java.io.*;/** * A set of nodes, ordered by numerical distance of their Id from the local Id * * @version $Id: SimilarSet.java 3274 2006-05-15 16:17:47Z jeffh $ * @author Andrew Ladd * @author Peter Druschel */public class SimilarSet extends Observable implements NodeSetEventSource, Serializable,  Observer {  private NodeHandle ln;  private boolean clockwise;  private NodeHandle[] nodes;  private int theSize;  private LeafSet leafSet;  transient ArrayList listeners = new ArrayList();  /**   * 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 minDist = ln.getNodeId().distance(nid);//    int min = -1;////    for (int i = 0; i < theSize; i++) {//      Id.Distance d = nodes[i].getNodeId().distance(nid);//      int cmp = d.compareTo(minDist);//      if ((!clockwise && cmp < 0) || (clockwise && cmp <= 0)) {//        minDist = d;//        min = i;//      }//    }////    return min;//  }  transient Id.Distance d1 = new Id.Distance();  transient Id.Distance d = new Id.Distance();  private final static long serialVersionUID = 2289610430696506873L;  /**   * Constructor for SimilarSet.   *   * @param that DESCRIBE THE PARAMETER   * @param ls DESCRIBE THE PARAMETER   */  private SimilarSet(SimilarSet that, LeafSet ls) {    this.ln = that.ln;    this.clockwise = that.clockwise;    this.nodes = new NodeHandle[that.nodes.length];    System.arraycopy(that.nodes, 0, nodes, 0, nodes.length);    this.theSize = that.theSize;    this.leafSet = ls;  }  /**   * Constructor.   *   * @param localNode the local node   * @param size the size of the similar set.   * @param cw true if this is the clockwise leafset half   * @param leafSet DESCRIBE THE PARAMETER   */  public SimilarSet(LeafSet leafSet, NodeHandle localNode, int size, boolean cw) {    this.leafSet = leafSet;    ln = localNode;    clockwise = cw;    theSize = 0;    nodes = new NodeHandle[size];  }  /**   * Constructor for SimilarSet.   *   * @param leafSet DESCRIBE THE PARAMETER   * @param localNode DESCRIBE THE PARAMETER   * @param size DESCRIBE THE PARAMETER   * @param cw DESCRIBE THE PARAMETER   * @param handles DESCRIBE THE PARAMETER   */  public SimilarSet(LeafSet leafSet, NodeHandle localNode, int size, boolean cw, NodeHandle[] handles) {    this.leafSet = leafSet;    ln = localNode;    clockwise = cw;    theSize = handles.length;    nodes = handles;  }  /**   * Finds the NodeHandle associated with the Id.   *   * @param nid a node id.   * @return the handle associated with that id or null if no such handle is   *      found.   */  public NodeHandle get(Id nid) {    for (int i = 0; i < theSize; i++) {      if (nodes[i].getNodeId().equals(nid)) {        return nodes[i];      }    }    return null;  }  /**   * DESCRIBE THE METHOD   *   * @param nh DESCRIBE THE PARAMETER   * @return DESCRIBE THE RETURN VALUE   */  public NodeHandle get(NodeHandle nh) {    for (int i = 0; i < theSize; i++) {      if (nodes[i].equals(nh)) {        return nodes[i];      }    }    return null;  }  /**   * Gets the ith element in the set.   *   * @param i an index. i == -1 refers to the local node   * @return the handle associated with that id or null if no such handle is   *      found.   */  public NodeHandle get(int i) {    if (i < -1 || i >= theSize) {      return null;    }    if (i == -1) {      return ln;    }    return nodes[i];  }  /**   * Gets the index of the element with the given node id.   *   * @param nid the node id.   * @return the index or -1 if the element does not exist.   */  public int getIndex(Id nid) {    for (int i = 0; i < theSize; i++) {      if (nodes[i].getNodeId().equals(nid)) {        return i;      }    }    return -1;  }  /**   * Gets the Index attribute of the SimilarSet object   *   * @param nh DESCRIBE THE PARAMETER   * @return The Index value   */  public int getIndex(NodeHandle nh) {    for (int i = 0; i < theSize; i++) {      if (nodes[i].equals(nh)) {        return i;      }    }    return -1;  }  /**   * Finds the NodeHandle associated with the Id.   *   * @param id a node id.   * @return the handle associated with that id or null if no such handle is   *      found.   */  public rice.p2p.commonapi.NodeHandle getHandle(rice.p2p.commonapi.Id id) {    return getHandle((Id) id);  }  /**   * Gets the ith element in the set.   *   * @param i an index.   * @return the handle associated with that id or null if no such handle is   *      found.   */  public rice.p2p.commonapi.NodeHandle getHandle(int i) {    return get(i);  }  /**   * Gets the index of the element with the given node id.   *   * @param id the id.   * @return the index or throws a NoSuchElementException.   * @exception NoSuchElementException DESCRIBE THE EXCEPTION   */  public int getIndexHandle(rice.p2p.commonapi.Id id)     throws NoSuchElementException {    return getIndex((Id) id);  }  /**   * This is thread safe, in that it won't throw an error if not properly   * synchronized.   *   * @return   */  public Collection getCollection() {    ArrayList al = new ArrayList();    for (int i = 0; i < theSize; i++) {      NodeHandle nh = nodes[i];      if (nh != null) {        al.add(nh);      }    }    return al;  }  /**   * swap two elements   *   * @param i the index of the first element   * @param j the indes of the second element   */  protected void swap(int i, int j) {    NodeHandle handle = nodes[i];    nodes[i] = nodes[j];    nodes[j] = handle;  }  /**   * Test if a NodeHandle belongs into the set. Predicts if a put would succeed.   *   * @param handle the handle to test.   * @return true if a put would succeed, false otherwise.   */  public boolean test(NodeHandle handle) {    Id nid = handle.getNodeId();    if (nid.equals(ln.getNodeId())) {      return false;    }    for (int i = 0; i < theSize; i++) {      if (nid.equals(nodes[i].getNodeId())) {        return false;      }    }    if (theSize < nodes.length) {      return true;    }    if (clockwise) {      if (!nid.isBetween(ln.getNodeId(), nodes[theSize - 1].getNodeId())) {        return false;      }    } else {      if (!nid.isBetween(nodes[theSize - 1].getNodeId(), ln.getNodeId())) {        return false;      }    }    return true;  }  /**   * Puts a NodeHandle into the set.   *   * @param handle the handle to put.   * @return true if the put succeeded, false otherwise.   */  public boolean put(NodeHandle handle) {    Id nid = handle.getNodeId();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -