001/* Extension of Comparator interface for CalendarQueue
002
003 Copyright (c) 1998-2013 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.actor.util;
029
030import java.util.Comparator;
031
032///////////////////////////////////////////////////////////////////
033//// CQComparator
034
035/**
036 This interface extends the java.util.Comparator interface, which
037 defines the compare() method. The extension defines additional methods
038 that specifically support the CalendarQueue class.  That class needs
039 to associate an entry in the queue with a virtual bin number, and
040 needs to be able to periodically recompute the width of its bins.
041 Thus, merely being able to compare entries is not sufficient.
042
043 @author Lukito Muliadi, Edward A. Lee
044 @version $Id$
045 @since Ptolemy II 0.2
046 @Pt.ProposedRating Green (eal)
047 @Pt.AcceptedRating Yellow (liuj)
048 @see ptolemy.actor.util.CalendarQueue
049 @see java.util.Comparator
050 */
051public interface CQComparator extends Comparator {
052    ///////////////////////////////////////////////////////////////////
053    ////                         public methods                    ////
054
055    /** Given an entry, return a virtual bin number for the entry.
056     *  The virtual bin number is a quantized version of whatever
057     *  key is being used to compare entries.
058     *  The calculation performed should be something like:
059     *  <p>
060     *  <i>(entry - zeroReference) / binWidth</i>,
061     *  </p>
062     *  with the result cast to long.
063     *  <p>
064     *  Because of the way this is used by CalendarQueue, it is OK
065     *  to return the low order 64 bits of the result if the result
066     *  does not fit in 64 bits. The result will be masked anyway
067     *  to get fewer low order bits that represent the bin number.
068     *  As a net result, time stamps that differ by exactly 2^64 times
069     *  the time resolution will appear in the event queue to be occurring
070     *  at the same time.
071     *  <p>
072     *  Classes that implement this interface will in general need
073     *  to perform a downcast on the arguments (of type Object) to the
074     *  appropriate user defined classes. If the arguments are not of
075     *  appropriate type, the implementation should throw a
076     *  ClassCastException.
077     *
078     *  @param entry An object that can be inserted in a calendar queue.
079     *  @return The index of the bin.
080     */
081    public long getVirtualBinNumber(Object entry);
082
083    /** Given an array of entries, set an appropriate bin width for a
084     *  calendar queue to hold these entries.  This method assumes that the
085     *  entries provided are all different, and are in increasing order.
086     *  Ideally, the bin width is chosen so that
087     *  the average number of entries in non-empty bins is equal to one.
088     *  If the argument is null set the default bin width.
089     *  @param entryArray An array of entries.
090     */
091    public void setBinWidth(Object[] entryArray);
092
093    /** Set the zero reference, to be used in calculating the virtual
094     *  bin number.
095     *  @param zeroReference The starting point for bins.
096     */
097    public void setZeroReference(Object zeroReference);
098}