Coverage Report - weka.associations.Item
 
Classes in this File Line Coverage Branch Coverage Complexity
Item
0%
0/31
0%
0/10
1.643
 
 1  
 /*
 2  
  *   This program is free software: you can redistribute it and/or modify
 3  
  *   it under the terms of the GNU General Public License as published by
 4  
  *   the Free Software Foundation, either version 3 of the License, or
 5  
  *   (at your option) any later version.
 6  
  *
 7  
  *   This program is distributed in the hope that it will be useful,
 8  
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 9  
  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 10  
  *   GNU General Public License for more details.
 11  
  *
 12  
  *   You should have received a copy of the GNU General Public License
 13  
  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 14  
  */
 15  
 
 16  
 /*
 17  
  *    Item.java
 18  
  *    Copyright (C) 2010-2012 University of Waikato, Hamilton, New Zealand
 19  
  *
 20  
  */
 21  
 package weka.associations;
 22  
 
 23  
 import java.io.Serializable;
 24  
 
 25  
 import weka.core.Attribute;
 26  
 
 27  
 /**
 28  
  * Class that encapsulates information about an individual item. An item
 29  
  * is a value of a nominal attribute, so this class has a backing Attribute.
 30  
  * 
 31  
  * @author Mark Hall (mhall{[at]}pentaho{[dot]}com).
 32  
  * @version $Revision: 8034 $
 33  
  */
 34  0
 public abstract class Item implements Serializable, Comparable<Item> {
 35  
   
 36  
   /** For serialization */
 37  
   private static final long serialVersionUID = -430198211081183575L;
 38  
 
 39  
   /** The frequency of this item */
 40  
   protected int m_frequency;
 41  
   
 42  
   /** The attribute that backs this item */
 43  
   protected Attribute m_attribute;
 44  
     
 45  0
   public Item(Attribute att) {
 46  0
     m_attribute = att;
 47  0
   }
 48  
     
 49  
   /**
 50  
    * Increase the frequency of this item.
 51  
    * 
 52  
    * @param f the amount to increase the frequency by.
 53  
    */
 54  
   public void increaseFrequency(int f) {
 55  0
     m_frequency += f;
 56  0
   }
 57  
   
 58  
   /**
 59  
    * Decrease the frequency of this item.
 60  
    * 
 61  
    * @param f the amount by which to decrease the frequency.
 62  
    */
 63  
   public void decreaseFrequency(int f) {
 64  0
     m_frequency -= f;
 65  0
   }
 66  
   
 67  
   /**
 68  
    * Increment the frequency of this item.
 69  
    */
 70  
   public void increaseFrequency() {
 71  0
     m_frequency++;
 72  0
   }
 73  
   
 74  
   /**
 75  
    * Decrement the frequency of this item.
 76  
    */
 77  
   public void decreaseFrequency() {
 78  0
     m_frequency--;
 79  0
   }
 80  
   
 81  
   /**
 82  
    * Get the frequency of this item.
 83  
    * 
 84  
    * @return the frequency.
 85  
    */
 86  
   public int getFrequency() {
 87  0
     return m_frequency;
 88  
   }
 89  
   
 90  
   /**
 91  
    * Get the attribute that this item originates from.
 92  
    * 
 93  
    * @return the corresponding attribute.
 94  
    */
 95  
   public Attribute getAttribute() {
 96  0
     return m_attribute;
 97  
   }
 98  
   
 99  
   /**
 100  
    * Get this item's value as a String.
 101  
    * 
 102  
    * @return this item's value as a String.
 103  
    */
 104  
   public abstract String getItemValueAsString();
 105  
   
 106  
   /**
 107  
    * Get this item's comparison operator as a String.
 108  
    * 
 109  
    * @return this item's comparison operator as a String.
 110  
    */
 111  
   public abstract String getComparisonAsString();
 112  
     
 113  
   /**
 114  
    * A string representation of this item. (i.e. 
 115  
    * <attribute name> <comparison operator> <item value>).
 116  
    * 
 117  
    * @return a string representation of this item.
 118  
    */
 119  
   public String toString() {
 120  0
     return toString(false);
 121  
   }
 122  
   
 123  
   /**
 124  
    * A string representation of this item, (i.e.
 125  
    * <attribute name> <comparison operator> <item value>).
 126  
    * This default implementation just prints the attribute
 127  
    * name and (optionally) frequency information.
 128  
    * 
 129  
    * @param freq true if the frequency should be included.
 130  
    * @return a string representation of this item. 
 131  
    */
 132  
   public String toString(boolean freq) {
 133  0
     String result = m_attribute.name();
 134  0
     if (freq) {
 135  0
       result += ":" + m_frequency;
 136  
     }
 137  0
     return result;
 138  
   }
 139  
     
 140  
   /**
 141  
    * Ensures that items will be sorted in descending order of frequency.
 142  
    * Ties are ordered by attribute name.
 143  
    * 
 144  
    * @param comp the Item to compare against.
 145  
    */
 146  
   public int compareTo(Item comp) {
 147  0
     if (m_frequency == comp.getFrequency()) {
 148  
       // sort by name
 149  0
       return -1 * m_attribute.name().compareTo(comp.getAttribute().name());
 150  
     }
 151  0
     if (comp.getFrequency() < m_frequency) {
 152  0
       return -1;
 153  
     }
 154  0
     return 1;
 155  
   }
 156  
   
 157  
   /**
 158  
    * Equals. Just compares attribute.
 159  
    * @return true if this Item is equal to the argument.
 160  
    */
 161  
   public boolean equals(Object compareTo) {
 162  0
     if (!(compareTo instanceof Item)) {
 163  0
       return false;
 164  
     }
 165  
     
 166  0
     Item b = (Item)compareTo;
 167  0
     if (m_attribute.equals(b.getAttribute())) {
 168  0
       return true;
 169  
     }
 170  
     
 171  0
     return false;
 172  
   }
 173  
   
 174  
   public int hashCode() {
 175  0
     return (m_attribute.name().hashCode() ^ 
 176  
         m_attribute.numValues()) * m_frequency;
 177  
   }  
 178  
 }