001    package rac;
002    
003    import lrs.*;
004    import lrs.visitor.*;
005    
006    import java.util.*;
007    
008    /*
009     * Implements a factory for restricted access containers that
010     * return the ``highest priority'' item.
011     * @author Mathias Ricken - Copyright 2008 - All rights reserved.
012     */
013    public class PQComparatorRACFactory<T extends Comparable<T>> extends ALRSRACFactory<T> {
014        private Comparator<T> _comp;
015    
016        /**
017         * Used when the items in the container are Comparable objects.
018         */
019        public PQComparatorRACFactory() {
020            _comp = new Comparator<T>() {
021                public int compare(T x, T y) {
022                    /*
023                     * Intentionally reverse the ordering so that the
024                     * largest item will be first.
025                     */
026                    return y.compareTo(x);
027                }
028            };
029        }
030    
031        /**
032         * Used when we want to prioritize the items according to a given Comparator.
033         * @param comp the item that is smallest according to comp has the highest
034         * priority.
035         */
036        public PQComparatorRACFactory(Comparator<T> comp) {
037            _comp = comp;
038        }
039    
040        /**
041         * Create a container that returns the item with the highest priority
042         * according to a given Comparator.
043         */
044        public IRAContainer<T> makeRAC() {
045            return new LRSRAContainer<T>(new InsertInOrder<T>(_comp));
046        }
047    
048    }