KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > util > iterator > RandomOrderIterator


1 /*
2   (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: RandomOrderIterator.java,v 1.3 2005/02/21 12:19:17 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.util.iterator;
8
9 import java.util.*;
10 /**
11  *
12  * RandomOrderIterator - Reorders the elements returned by an Iterator.
13  *
14  * @author jjc
15  *
16  */

17 public class RandomOrderIterator extends WrappedIterator {
18     private Random rnd = new Random();
19     private Object JavaDoc buffer[];
20     // one more than the index of the last non-null element.
21
int top;
22     /**
23      * Wrap the base iterator, randomizing with a buffer of length sz.
24      */

25     public RandomOrderIterator(int sz, Iterator base) {
26         super(base);
27         buffer = new Object JavaDoc[sz];
28         top = 0;
29         fill();
30     }
31     
32     public boolean hasNext() {
33         return top > 0;
34     }
35     public Object JavaDoc next() {
36         int ix = rnd.nextInt(top);
37         Object JavaDoc rslt = buffer[ix];
38         top--;
39         buffer[ix] = buffer[top];
40         fill();
41         return rslt;
42     }
43     
44     public void remove() {
45         throw new UnsupportedOperationException JavaDoc("randomizing does not allow modification");
46     }
47     
48     private void fill() {
49        while ( top < buffer.length && super.hasNext() ) {
50          buffer[top++] = super.next();
51        }
52     }
53
54 }
55 /*
56  (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
57  All rights reserved.
58
59  Redistribution and use in source and binary forms, with or without
60  modification, are permitted provided that the following conditions
61  are met:
62
63  1. Redistributions of source code must retain the above copyright
64  notice, this list of conditions and the following disclaimer.
65
66  2. Redistributions in binary form must reproduce the above copyright
67  notice, this list of conditions and the following disclaimer in the
68  documentation and/or other materials provided with the distribution.
69
70  3. The name of the author may not be used to endorse or promote products
71  derived from this software without specific prior written permission.
72
73  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
74  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
75  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
76  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
77  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
78  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
79  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
80  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
81  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
82  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
83  */
Popular Tags