Package weka.core

Class InstanceComparator

  • All Implemented Interfaces:
    java.io.Serializable, java.util.Comparator<Instance>, RevisionHandler

    public class InstanceComparator
    extends java.lang.Object
    implements java.util.Comparator<Instance>, java.io.Serializable, RevisionHandler
    A comparator for the Instance class. it can be used with or without the class label. Missing values are sorted at the beginning.
    Can be used as comparator in the sorting and binary search algorithms of Arrays and Collections. Relational values are compared instance by instance with a nested InstanceComparator.
    Version:
    $Revision: 14912 $
    Author:
    FracPete (fracpete at cs dot waikato dot ac dot nz)
    See Also:
    Instance, Arrays, Collections, Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      InstanceComparator()
      Initializes the comparator and includes the class in the comparison and all attributes included.
      InstanceComparator​(boolean includeClass)
      Initializes the comparator with all attributes included.
      InstanceComparator​(boolean includeClass, java.lang.String range, boolean invert)
      Initializes the comparator.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compare​(Instance inst1, Instance inst2)
      compares the two instances, returns -1 if o1 is smaller than o2, 0 if equal and +1 if greater.
      boolean getIncludeClass()
      Returns whether the class is included in the comparison.
      boolean getInvert()
      Returns whether the matching sense of the attribute range is inverted.
      java.lang.String getRange()
      Returns the attribute range to use in the comparison.
      java.lang.String getRevision()
      Returns the revision string.
      static void main​(java.lang.String[] args)
      for testing only.
      void setIncludeClass​(boolean includeClass)
      Sets whether the class should be included in the comparison.
      void setInvert​(boolean invert)
      Sets whether to invert the matching sense of the attribute range.
      void setRange​(java.lang.String value)
      Sets the attribute range to use for comparison.
      • Methods inherited from class java.lang.Object

        equals, 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

      • InstanceComparator

        public InstanceComparator()
        Initializes the comparator and includes the class in the comparison and all attributes included.
      • InstanceComparator

        public InstanceComparator​(boolean includeClass)
        Initializes the comparator with all attributes included.
        Parameters:
        includeClass - whether to include the class in the comparison
      • InstanceComparator

        public InstanceComparator​(boolean includeClass,
                                  java.lang.String range,
                                  boolean invert)
        Initializes the comparator.
        Parameters:
        includeClass - whether to include the class in the comparison
        range - the attribute range string
        invert - whether to invert the matching sense of the att range
    • Method Detail

      • setIncludeClass

        public void setIncludeClass​(boolean includeClass)
        Sets whether the class should be included in the comparison.
        Parameters:
        includeClass - true if to include the class in the comparison
      • getIncludeClass

        public boolean getIncludeClass()
        Returns whether the class is included in the comparison.
        Returns:
        true if the class is included
      • setRange

        public void setRange​(java.lang.String value)
        Sets the attribute range to use for comparison.
        Parameters:
        value - the attribute range
      • getRange

        public java.lang.String getRange()
        Returns the attribute range to use in the comparison.
        Returns:
        the attribute range
      • setInvert

        public void setInvert​(boolean invert)
        Sets whether to invert the matching sense of the attribute range.
        Parameters:
        invert - true if to invert the matching sense
      • getInvert

        public boolean getInvert()
        Returns whether the matching sense of the attribute range is inverted.
        Returns:
        true if the matching sense is inverted
      • compare

        public int compare​(Instance inst1,
                           Instance inst2)
        compares the two instances, returns -1 if o1 is smaller than o2, 0 if equal and +1 if greater. The method assumes that both instance objects have the same attributes, they don't have to belong to the same dataset.
        Specified by:
        compare in interface java.util.Comparator<Instance>
        Parameters:
        inst1 - the first instance to compare
        inst2 - the second instance to compare
        Returns:
        returns -1 if inst1 is smaller than inst2, 0 if equal and +1 if greater
      • getRevision

        public java.lang.String getRevision()
        Returns the revision string.
        Specified by:
        getRevision in interface RevisionHandler
        Returns:
        the revision
      • main

        public static void main​(java.lang.String[] args)
                         throws java.lang.Exception
        for testing only. takes an ARFF-filename as first argument to perform some tests.
        Throws:
        java.lang.Exception