Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: net/jxta/impl/rendezvous/rpv/PeerViewRandomWithReplaceStrategy.java


1   /*
2    * Copyright (c) 2002 Sun Microsystems, Inc.  All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, are permitted provided that the following conditions
6    * are met:
7    *
8    * 1. Redistributions of source code must retain the above copyright
9    *    notice, this list of conditions and the following disclaimer.
10   *
11   * 2. Redistributions in binary form must reproduce the above copyright
12   *    notice, this list of conditions and the following disclaimer in
13   *    the documentation and/or other materials provided with the
14   *    distribution.
15   *
16   * 3. The end-user documentation included with the redistribution,
17   *    if any, must include the following acknowledgment:
18   *       "This product includes software developed by the
19   *       Sun Microsystems, Inc. for Project JXTA."
20   *    Alternately, this acknowledgment may appear in the software itself,
21   *    if and wherever such third-party acknowledgments normally appear.
22   *
23   * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and
24   *    "Project JXTA" must not be used to endorse or promote products
25   *    derived from this software without prior written permission.
26   *    For written permission, please contact Project JXTA at
27   *    http://www.jxta.org.
28   *
29   * 5. Products derived from this software may not be called "JXTA",
30   *    nor may "JXTA" appear in their name, without prior written
31   *    permission of Sun.
32   *
33   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36   * DISCLAIMED.  IN NO EVENT SHALL SUN MICROSYSTEMS OR
37   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44   * SUCH DAMAGE.
45   * ====================================================================
46   *
47   * This software consists of voluntary contributions made by many
48   * individuals on behalf of Project JXTA.  For more
49   * information on Project JXTA, please see
50   * <http://www.jxta.org/>.
51   *
52   * This license is based on the BSD license adopted by the Apache
53   * Foundation.
54   *
55   * $Id: PeerViewRandomWithReplaceStrategy.java,v 1.6 2004/05/18 19:22:11 bondolo Exp $
56   */
57  package net.jxta.impl.rendezvous.rpv;
58  
59  
60  import java.util.Random;
61  import java.util.SortedSet;
62  import java.util.Iterator;
63  
64  
65  /**
66   * Random with replacement
67   **/
68  class PeerViewRandomWithReplaceStrategy implements PeerViewStrategy {
69      
70      private static Random random = new Random();
71      private SortedSet set = null;
72      
73      PeerViewRandomWithReplaceStrategy(SortedSet set) {
74          this.set = set;
75      }
76      
77      /**
78       *  {@inheritDoc}
79       **/
80      public void reset() {}
81      
82      /**
83       *  {@inheritDoc}
84       **/
85      public PeerViewElement next() {
86          synchronized (set) {
87              if (set.isEmpty()) {
88                  return null;
89              }
90              
91              int i = random.nextInt(set.size());
92              
93              // return the ith element
94              int n = 0;
95              Iterator si = set.iterator();
96              
97              while (n < i) {
98                  si.next();
99                  n++;
100             }
101 
102             return (PeerViewElement) si.next();
103         }
104     }
105 }