Source code: net/jxta/impl/rendezvous/rpv/PeerViewSequentialStrategy.java
1 /*
2 * Copyright (c) 2002-2004 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: PeerViewSequentialStrategy.java,v 1.5 2004/05/18 19:22:11 bondolo Exp $
56 */
57 package net.jxta.impl.rendezvous.rpv;
58
59
60 import java.util.SortedSet;
61 import java.util.Iterator;
62
63
64 /**
65 * Sequential
66 */
67 class PeerViewSequentialStrategy implements PeerViewStrategy {
68
69 private SortedSet set;
70 private PeerViewElement current;
71
72 PeerViewSequentialStrategy(SortedSet aset) {
73 set = aset;
74 reset();
75 }
76
77 /**
78 * {@inheritDoc}
79 **/
80 public void reset() {
81 current = null;
82 }
83
84 /**
85 * {@inheritDoc}
86 **/
87 public PeerViewElement next() {
88
89 synchronized (set) {
90 do {
91 if (null == current) {
92 if (set.size() > 0) {
93 // no current, take the first
94 current = (PeerViewElement) set.first();
95 break;
96 } else {
97 // no first, return null
98 break;
99 }
100 } else {
101 SortedSet tail = set.tailSet(current);
102
103 Iterator fromTail = tail.iterator();
104
105 if (fromTail.hasNext()) {
106 Comparable tailFirst = (Comparable) fromTail.next();
107
108 if (0 == current.compareTo(tailFirst)) {
109 if (fromTail.hasNext()) {
110 // first in tail is current so the new current
111 // is second in tail.
112 current = (PeerViewElement) fromTail.next();
113 break;
114 } else {
115 // none left in tail after current, start over.
116 current = null;
117 continue;
118 }
119 } else {
120 // current is not in the tail set, so first in tail
121 // is new current
122 current = (PeerViewElement) tailFirst;
123 break;
124 }
125 } else {
126 // none in tail, start over.
127 current = null;
128 continue;
129 }
130 }
131 } while (null == current);
132 }
133
134 return current;
135 }
136 }