Class TimedEvent.TimeComparator

  • All Implemented Interfaces:
    java.util.Comparator, CQComparator
    Enclosing class:
    TimedEvent

    public static class TimedEvent.TimeComparator
    extends java.lang.Object
    implements CQComparator
    This class implements the CQComparator interface. It compares instances of TimedEvent. Therefore, all arguments passed to its methods have to be of type TimedEvent (or TimedEvent[] for the getBinWidth() method). If this is violated, ClassCastException will be thrown.
    • Constructor Summary

      Constructors 
      Constructor Description
      TimeComparator()
      Construct a TimeComparator object.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compare​(java.lang.Object object1, java.lang.Object object2)
      Compare the two arguments.
      long getVirtualBinNumber​(java.lang.Object entry)
      Given an entry, return a virtual bin number for the entry.
      void setBinWidth​(java.lang.Object[] entryArray)
      Given an array of TimedEvent objects, find the appropriate bin width.
      void setZeroReference​(java.lang.Object zeroReference)
      Set the zero reference, to be used in calculating the virtual bin number.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
      • Methods inherited from interface java.util.Comparator

        equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
    • Constructor Detail

      • TimeComparator

        public TimeComparator()
        Construct a TimeComparator object.
    • Method Detail

      • compare

        public int compare​(java.lang.Object object1,
                           java.lang.Object object2)
        Compare the two arguments. Return a negative integer, zero, or a positive integer depending on whether the first argument is less than, equal to, or greater than the second. Both arguments have to be instances of TimedEvent, otherwise a ClassCastException will be thrown.
        Specified by:
        compare in interface java.util.Comparator
        Parameters:
        object1 - The first event.
        object2 - The second event.
        Returns:
        -1, 0, or +1 depending on whether the first argument is less than, equal to, or greater than the second.
        Throws:
        java.lang.ClassCastException - If either argument is not an instance of TimedEvent.
      • getVirtualBinNumber

        public long getVirtualBinNumber​(java.lang.Object entry)
        Given an entry, return a virtual bin number for the entry. The calculation performed is:

        (entry.timeStamp - zeroReference) / binWidth,

        with the result cast to long. If the arguments are not instances of TimedEvent, then a ClassCastException will be thrown. If the bin number is larger than what can be represented in a long, then the low-order 64 bits will be returned. Note that this could change the sign of the result, but the way this is used in the CalendarQueue class, this is OK. It is converted to a bin number by masking some number of low-order bits, so the result will be unaffected by the sign error.
        Specified by:
        getVirtualBinNumber in interface CQComparator
        Parameters:
        entry - The entry.
        Returns:
        The virtual bin number for the entry, according to the current zero reference and the bin width.
        Throws:
        java.lang.ClassCastException - If the arguments are not instances of TimedEvent.
      • setBinWidth

        public void setBinWidth​(java.lang.Object[] entryArray)
        Given an array of TimedEvent objects, find the appropriate bin width. By 'appropriate', we mean that the bin width is chosen such that on average the number of entries in all non-empty bins is equal to one. If the argument is null, return the default bin width, which is 1.0 for this implementation. Otherwise, the statistics of the elements of the array are analyzed to determine a reasonable bin width.
        Specified by:
        setBinWidth in interface CQComparator
        Parameters:
        entryArray - An array of TimedEvent objects.
        Throws:
        java.lang.ClassCastException - If one of the array elements is not an instance of TimedEvent.
      • setZeroReference

        public void setZeroReference​(java.lang.Object zeroReference)
        Set the zero reference, to be used in calculating the virtual bin number.
        Specified by:
        setZeroReference in interface CQComparator
        Parameters:
        zeroReference - The starting point for bins.
        Throws:
        java.lang.ClassCastException - If the argument is not an instance of TimedEvent.