001    package rac;
002    
003    import lrs.*;
004    
005    /*
006     * Implements a factory for restricted access containers, including a
007     * container that returns a random item.
008     * @author Mathias Ricken - Copyright 2008 - All rights reserved.
009     */
010    public class RandomRACFactory<T> extends ALRSRACFactory<T> {
011        /**
012         * Create a container that returns a random item.
013         */
014        public IRAContainer<T> makeRAC() {
015            return new LRSRAContainer<T>(new IAlgo<T,LRStruct<T>,T>() {
016                public LRStruct<T> emptyCase(LRStruct<T> host, T... input) {
017                    return host.insertFront(input[0]);
018                }
019                
020                public LRStruct<T> nonEmptyCase(LRStruct<T> host, T... input) {
021                    /*
022                     * Math.Random returns a value between 0.0 and 1.0.
023                     */
024                    if (0.5 > Math.random())
025                        return host.insertFront(input[0]);
026                    else
027                        return host.getRest().execute(this, input);
028                }
029            });
030        }
031    }
032