001: package net.sf.saxon.exslt;
002:
003: import net.sf.saxon.om.AxisIteratorImpl;
004: import net.sf.saxon.om.Item;
005: import net.sf.saxon.om.SequenceIterator;
006: import net.sf.saxon.trans.XPathException;
007: import net.sf.saxon.value.DoubleValue;
008:
009: /**
010: * This class implements extension functions in the
011: * http://exslt.org/random namespace.
012: *
013: * @author Martin Szugat
014: * @version 1.0, 30.06.2004
015: * Rewritten by Michael Kay to generate a SequenceIterator
016: */
017: public abstract class Random {
018:
019: /**
020: * Returns a sequence of random numbers
021: * between 0 and 1.
022: * @param numberOfItems number of random items
023: * in the sequence.
024: * @param seed the initial seed.
025: * @return sequence of random numbers as an iterator.
026: * @throws IllegalArgumentException
027: * <code>numberOfItems</code> is not positive.
028: */
029: public static SequenceIterator randomSequence(int numberOfItems,
030: double seed) throws IllegalArgumentException {
031: if (numberOfItems < 1) {
032: throw new IllegalArgumentException(
033: "numberOfItems supplied to randomSequence() must be positive");
034: }
035: long javaSeed = Double.doubleToLongBits(seed);
036: return new RandomIterator(numberOfItems, javaSeed);
037: }
038:
039: /**
040: * Returns a sequence of random numbers
041: * between 0 and 1.
042: * @param numberOfItems number of random items
043: * in the sequence.
044: * @return sequence of random numbers.
045: * @throws IllegalArgumentException
046: * <code>numberOfItems</code> is not positive.
047: */
048: public static SequenceIterator randomSequence(int numberOfItems)
049: throws IllegalArgumentException {
050: return randomSequence(numberOfItems, System.currentTimeMillis());
051: }
052:
053: /**
054: * Returns a single random number X
055: * between 0 and 1.
056: * @return sequence random number.
057: */
058: public static DoubleValue randomSequence() throws XPathException {
059: return (DoubleValue) randomSequence(1).next();
060: }
061:
062: /**
063: * Iterator over a sequence of random numbers
064: */
065:
066: private static class RandomIterator extends AxisIteratorImpl {
067:
068: private int count;
069: private long seed;
070: private java.util.Random generator;
071:
072: public RandomIterator(int count, long seed) {
073: this .count = count;
074: this .seed = seed;
075: generator = new java.util.Random(seed);
076: }
077:
078: /**
079: * Get the next item in the sequence. <BR>
080: * @return the next item, or null if there are no more items.
081: */
082:
083: public Item next() {
084: if (position++ >= count) {
085: current = null;
086: position = -1;
087: return null;
088: } else {
089: current = new DoubleValue(generator.nextDouble());
090: return current;
091: }
092: }
093:
094: /**
095: * Get another SequenceIterator that iterates over the same items as the original,
096: * but which is repositioned at the start of the sequence.
097: *
098: * @return a SequenceIterator that iterates over the same items,
099: * positioned before the first item
100: */
101:
102: public SequenceIterator getAnother() {
103: return new RandomIterator(count, seed);
104: }
105: }
106: }
107:
108: //
109: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
110: // you may not use this file except in compliance with the License. You may obtain a copy of the
111: // License at http://www.mozilla.org/MPL/
112: //
113: // Software distributed under the License is distributed on an "AS IS" basis,
114: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
115: // See the License for the specific language governing rights and limitations under the License.
116: //
117: // The Original Code is: all this file.
118: //
119: // The Initial Developer of the Original Code is Martin Szugat.
120: //
121: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
122: //
123: // Contributor(s): Michael H. Kay.
124: //
|