Instance.java

Index Score
weka.core
Weka

View: Reasons, Metrics, Source Code

These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.

MetricDescription
DECL_COMMENTSComments in declarations
DOC_COMMENTNumber of javadoc comment lines
COMMENTSComment lines
LINE_COMMENTNumber of line comments
JAVA0266JAVA0266 Use of System.out
SIZESize of the file in bytes
LINESNumber of lines in the source file
RETURNSNumber of return points from functions
FUNCTIONSNumber of function declarations
EXEC_COMMENTSComments in executable code
INTERFACE_COMPLEXITYInterface complexity
CYCLOMATICCyclomatic complexity
BLOCKSNumber of blocks
COMPARISONSNumber of comparison operators
EXITSProcedure exits
OPERATORSNumber of operators
PROGRAM_LENGTHHalstead program length
PARAMSNumber of formal parameter declarations
LOGICAL_LINESNumber of statements
JAVA0233JAVA0233 Definition of serialVersionUID other than 'private static final long serialVersionUID'
LOCLines of code
OPERANDSNumber of operands
ELOCEffective lines of code
WHITESPACENumber of whitespace lines
PROGRAM_VOCABHalstead program vocabulary
UNIQUE_OPERANDSNumber of unique operands
JAVA0034JAVA0034 Missing braces in if statement
LOOPSNumber of loops
UNIQUE_OPERATORSNumber of unique operators
JAVA0117JAVA0117 Missing javadoc: method 'method'
PROGRAM_VOLUMEHalstead program volume
JAVA0136JAVA0136 N methods defined in class (maximum: M)
JAVA0126JAVA0126 Method declares unchecked exception in throws
JAVA0110JAVA0110 Incorrect javadoc: no @return tag
JAVA0076JAVA0076 Use of magic number
JAVA0265JAVA0265 Use of Throwable.printStackTrace()
JAVA0108JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
JAVA0145JAVA0145 Tab character used in source file
/* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ /* * Instance.java * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand * */ package weka.core; import java.io.Serializable; import java.util.Enumeration; /** * Class for handling an instance. All values (numeric, date, nominal, string * or relational) are internally stored as floating-point numbers. If an * attribute is nominal (or a string or relational), the stored value is the * index of the corresponding nominal (or string or relational) value in the * attribute's definition. We have chosen this approach in favor of a more * elegant object-oriented approach because it is much faster. <p> * * Typical usage (code from the main() method of this class): <p> * * <code> * ... <br> * * // Create empty instance with three attribute values <br> * Instance inst = new Instance(3); <br><br> * * // Set instance's values for the attributes "length", "weight", and "position"<br> * inst.setValue(length, 5.3); <br> * inst.setValue(weight, 300); <br> * inst.setValue(position, "first"); <br><br> * * // Set instance's dataset to be the dataset "race" <br> * inst.setDataset(race); <br><br> * * // Print the instance <br> * System.out.println("The instance: " + inst); <br> * * ... <br> * </code><p> * * All methods that change an instance are safe, ie. a change of an * instance does not affect any other instances. All methods that * change an instance's attribute values clone the attribute value * vector before it is changed. If your application heavily modifies * instance values, it may be faster to create a new instance from scratch. * * @author Eibe Frank (eibe@cs.waikato.ac.nz) * @version $Revision: 1.28 $ */ public class Instance implements Copyable, Serializable, RevisionHandler { /** for serialization */ static final long serialVersionUID = 1482635194499365122L; /** Constant representing a missing value. */ protected static final double MISSING_VALUE = Double.NaN; /** * The dataset the instance has access to. Null if the instance * doesn't have access to any dataset. Only if an instance has * access to a dataset, it knows about the actual attribute types. */ protected /*@spec_public@*/ Instances m_Dataset; /** The instance's attribute values. */ protected /*@spec_public non_null@*/ double[] m_AttValues; /** The instance's weight. */ protected double m_Weight; /** * Constructor that copies the attribute values and the weight from * the given instance. Reference to the dataset is set to null. * (ie. the instance doesn't have access to information about the * attribute types) * * @param instance the instance from which the attribute * values and the weight are to be copied */ //@ ensures m_Dataset == null; public Instance(/*@non_null@*/ Instance instance) { m_AttValues = instance.m_AttValues; m_Weight = instance.m_Weight; m_Dataset = null; } /** * Constructor that inititalizes instance variable with given * values. Reference to the dataset is set to null. (ie. the instance * doesn't have access to information about the attribute types) * * @param weight the instance's weight * @param attValues a vector of attribute values */ //@ ensures m_Dataset == null; public Instance(double weight, /*@non_null@*/ double[]attValues){ m_AttValues = attValues; m_Weight = weight; m_Dataset = null; } /** * Constructor of an instance that sets weight to one, all values to * be missing, and the reference to the dataset to null. (ie. the instance * doesn't have access to information about the attribute types) * * @param numAttributes the size of the instance */ //@ requires numAttributes > 0; // Or maybe == 0 is okay too? //@ ensures m_Dataset == null; public Instance(int numAttributes) { m_AttValues = new double[numAttributes]; for (int i = 0; i < m_AttValues.length; i++) { m_AttValues[i] = MISSING_VALUE; } m_Weight = 1; m_Dataset = null; } /** * Returns the attribute with the given index. * * @param index the attribute's index * @return the attribute at the given position * @throws UnassignedDatasetException if instance doesn't have access to a * dataset */ //@ requires m_Dataset != null; public /*@pure@*/ Attribute attribute(int index) { if (m_Dataset == null) { throw new UnassignedDatasetException("Instance doesn't have access to a dataset!"); } return m_Dataset.attribute(index); } /** * Returns the attribute with the given index. Does the same * thing as attribute(). * * @param indexOfIndex the index of the attribute's index * @return the attribute at the given position * @throws UnassignedDatasetException if instance doesn't have access to a * dataset */ //@ requires m_Dataset != null; public /*@pure@*/ Attribute attributeSparse(int indexOfIndex) { if (m_Dataset == null) { throw new UnassignedDatasetException("Instance doesn't have access to a dataset!"); } return m_Dataset.attribute(indexOfIndex); } /** * Returns class attribute. * * @return the class attribute * @throws UnassignedDatasetException if the class is not set or the * instance doesn't have access to a dataset */ //@ requires m_Dataset != null; public /*@pure@*/ Attribute classAttribute() { if (m_Dataset == null) { throw new UnassignedDatasetException("Instance doesn't have access to a dataset!"); } return m_Dataset.classAttribute(); } /** * Returns the class attribute's index. * * @return the class index as an integer * @throws UnassignedDatasetException if instance doesn't have access to a dataset */ //@ requires m_Dataset != null; //@ ensures \result == m_Dataset.classIndex(); public /*@pure@*/ int classIndex() { if (m_Dataset == null) { throw new UnassignedDatasetException("Instance doesn't have access to a dataset!"); } return m_Dataset.classIndex(); } /** * Tests if an instance's class is missing. * * @return true if the instance's class is missing * @throws UnassignedClassException if the class is not set or the instance doesn't * have access to a dataset */ //@ requires classIndex() >= 0; public /*@pure@*/ boolean classIsMissing() { if (classIndex() < 0) { throw new UnassignedClassException("Class is not set!"); } return isMissing(classIndex()); } /** * Returns an instance's class value in internal format. (ie. as a * floating-point number) * * @return the corresponding value as a double (If the * corresponding attribute is nominal (or a string) then it returns the * value's index as a double). * @throws UnassignedClassException if the class is not set or the instance doesn't * have access to a dataset */ //@ requires classIndex() >= 0; public /*@pure@*/ double classValue() { if (classIndex() < 0) { throw new UnassignedClassException("Class is not set!"); } return value(classIndex()); } /** * Produces a shallow copy of this instance. The copy has * access to the same dataset. (if you want to make a copy * that doesn't have access to the dataset, use * <code>new Instance(instance)</code> * * @return the shallow copy */ //@ also ensures \result != null; //@ also ensures \result instanceof Instance; //@ also ensures ((Instance)\result).m_Dataset == m_Dataset; public /*@pure@*/ Object copy() { Instance result = new Instance(this); result.m_Dataset = m_Dataset; return result; } /** * Returns the dataset this instance has access to. (ie. obtains * information about attribute types from) Null if the instance * doesn't have access to a dataset. * * @return the dataset the instance has accesss to */ //@ ensures \result == m_Dataset; public /*@pure@*/ Instances dataset() { return m_Dataset; } /** * Deletes an attribute at the given position (0 to * numAttributes() - 1). Only succeeds if the instance does not * have access to any dataset because otherwise inconsistencies * could be introduced. * * @param position the attribute's position * @throws RuntimeException if the instance has access to a * dataset */ //@ requires m_Dataset != null; public void deleteAttributeAt(int position) { if (m_Dataset != null) { throw new RuntimeException("Instance has access to a dataset!"); } forceDeleteAttributeAt(position); } /** * Returns an enumeration of all the attributes. * * @return enumeration of all the attributes * @throws UnassignedDatasetException if the instance doesn't * have access to a dataset */ //@ requires m_Dataset != null; public /*@pure@*/ Enumeration enumerateAttributes() { if (m_Dataset == null) { throw new UnassignedDatasetException("Instance doesn't have access to a dataset!"); } return m_Dataset.enumerateAttributes(); } /** * Tests if the headers of two instances are equivalent. * * @param inst another instance * @return true if the header of the given instance is * equivalent to this instance's header * @throws UnassignedDatasetException if instance doesn't have access to any * dataset */ //@ requires m_Dataset != null; public /*@pure@*/ boolean equalHeaders(Instance inst) { if (m_Dataset == null) { throw new UnassignedDatasetException("Instance doesn't have access to a dataset!"); } return m_Dataset.equalHeaders(inst.m_Dataset); } /** * Tests whether an instance has a missing value. Skips the class attribute if set. * @return true if instance has a missing value. * @throws UnassignedDatasetException if instance doesn't have access to any * dataset */ //@ requires m_Dataset != null; public /*@pure@*/ boolean hasMissingValue() { if (m_Dataset == null) { throw new UnassignedDatasetException("Instance doesn't have access to a dataset!"); } for (int i = 0; i < numAttributes(); i++) { if (i != classIndex()) { if (isMissing(i)) { return true; } } } return false; } /** * Returns the index of the attribute stored at the given position. * Just returns the given value. * * @param position the position * @return the index of the attribute stored at the given position */ public /*@pure@*/ int index(int position) { return position; } /** * Inserts an attribute at the given position (0 to * numAttributes()). Only succeeds if the instance does not * have access to any dataset because otherwise inconsistencies * could be introduced. * * @param position the attribute's position * @throws RuntimeException if the instance has accesss to a * dataset * @throws IllegalArgumentException if the position is out of range */ //@ requires m_Dataset == null; //@ requires 0 <= position && position <= numAttributes(); public void insertAttributeAt(int position) { if (m_Dataset != null) { throw new RuntimeException("Instance has accesss to a dataset!"); } if ((position < 0) || (position > numAttributes())) { throw new IllegalArgumentException("Can't insert attribute: index out "+ "of range"); } forceInsertAttributeAt(position); } /** * Tests if a specific value is "missing". * * @param attIndex the attribute's index * @return true if the value is "missing" */ public /*@pure@*/ boolean isMissing(int attIndex) { if (Double.isNaN(m_AttValues[attIndex])) { return true; } return false; } /** * Tests if a specific value is "missing". Does * the same thing as isMissing() if applied to an Instance. * * @param indexOfIndex the index of the attribute's index * @return true if the value is "missing" */ public /*@pure@*/ boolean isMissingSparse(int indexOfIndex) { if (Double.isNaN(m_AttValues[indexOfIndex])) { return true; } return false; } /** * Tests if a specific value is "missing". * The given attribute has to belong to a dataset. * * @param att the attribute * @return true if the value is "missing" */ public /*@pure@*/ boolean isMissing(Attribute att) { return isMissing(att.index()); } /** * Tests if the given value codes "missing". * * @param val the value to be tested * @return true if val codes "missing" */ public static /*@pure@*/ boolean isMissingValue(double val) { return Double.isNaN(val); } /** * Merges this instance with the given instance and returns * the result. Dataset is set to null. * * @param inst the instance to be merged with this one * @return the merged instances */ public Instance mergeInstance(Instance inst) { int m = 0; double [] newVals = new double[numAttributes() + inst.numAttributes()]; for (int j = 0; j < numAttributes(); j++, m++) { newVals[m] = value(j); } for (int j = 0; j < inst.numAttributes(); j++, m++) { newVals[m] = inst.value(j); } return new Instance(1.0, newVals); } /** * Returns the double that codes "missing". * * @return the double that codes "missing" */ public /*@pure@*/ static double missingValue() { return MISSING_VALUE; } /** * Returns the number of attributes. * * @return the number of attributes as an integer */ //@ ensures \result == m_AttValues.length; public /*@pure@*/ int numAttributes() { return m_AttValues.length; } /** * Returns the number of class labels. * * @return the number of class labels as an integer if the * class attribute is nominal, 1 otherwise. * @throws UnassignedDatasetException if instance doesn't have access to any * dataset */ //@ requires m_Dataset != null; public /*@pure@*/ int numClasses() { if (m_Dataset == null) { throw new UnassignedDatasetException("Instance doesn't have access to a dataset!"); } return m_Dataset.numClasses(); } /** * Returns the number of values present. Always the same as numAttributes(). * * @return the number of values */ //@ ensures \result == m_AttValues.length; public /*@pure@*/ int numValues() { return m_AttValues.length; } /** * Replaces all missing values in the instance with the * values contained in the given array. A deep copy of * the vector of attribute values is performed before the * values are replaced. * * @param array containing the means and modes * @throws IllegalArgumentException if numbers of attributes are unequal */ public void replaceMissingValues(double[] array) { if ((array == null) || (array.length != m_AttValues.length)) { throw new IllegalArgumentException("Unequal number of attributes!"); } freshAttributeVector(); for (int i = 0; i < m_AttValues.length; i++) { if (isMissing(i)) { m_AttValues[i] = array[i]; } } } /** * Sets the class value of an instance to be "missing". A deep copy of * the vector of attribute values is performed before the * value is set to be missing. * * @throws UnassignedClassException if the class is not set * @throws UnassignedDatasetException if the instance doesn't * have access to a dataset */ //@ requires classIndex() >= 0; public void setClassMissing() { if (classIndex() < 0) { throw new UnassignedClassException("Class is not set!"); } setMissing(classIndex()); } /** * Sets the class value of an instance to the given value (internal * floating-point format). A deep copy of the vector of attribute * values is performed before the value is set. * * @param value the new attribute value (If the corresponding * attribute is nominal (or a string) then this is the new value's * index as a double). * @throws UnassignedClassException if the class is not set * @throws UnaddignedDatasetException if the instance doesn't * have access to a dataset */ //@ requires classIndex() >= 0; public void setClassValue(double value) { if (classIndex() < 0) { throw new UnassignedClassException("Class is not set!"); } setValue(classIndex(), value); } /** * Sets the class value of an instance to the given value. A deep * copy of the vector of attribute values is performed before the * value is set. * * @param value the new class value (If the class * is a string attribute and the value can't be found, * the value is added to the attribute). * @throws UnassignedClassException if the class is not set * @throws UnassignedDatasetException if the dataset is not set * @throws IllegalArgumentException if the attribute is not * nominal or a string, or the value couldn't be found for a nominal * attribute */ //@ requires classIndex() >= 0; public final void setClassValue(String value) { if (classIndex() < 0) { throw new UnassignedClassException("Class is not set!"); } setValue(classIndex(), value); } /** * Sets the reference to the dataset. Does not check if the instance * is compatible with the dataset. Note: the dataset does not know * about this instance. If the structure of the dataset's header * gets changed, this instance will not be adjusted automatically. * * @param instances the reference to the dataset */ public final void setDataset(Instances instances) { m_Dataset = instances; } /** * Sets a specific value to be "missing". Performs a deep copy * of the vector of attribute values before the value is set to * be missing. * * @param attIndex the attribute's index */ public final void setMissing(int attIndex) { setValue(attIndex, MISSING_VALUE); } /** * Sets a specific value to be "missing". Performs a deep copy * of the vector of attribute values before the value is set to * be missing. The given attribute has to belong to a dataset. * * @param att the attribute */ public final void setMissing(Attribute att) { setMissing(att.index()); } /** * Sets a specific value in the instance to the given value * (internal floating-point format). Performs a deep copy * of the vector of attribute values before the value is set. * * @param attIndex the attribute's index * @param value the new attribute value (If the corresponding * attribute is nominal (or a string) then this is the new value's * index as a double). */ public void setValue(int attIndex, double value) { freshAttributeVector(); m_AttValues[attIndex] = value; } /** * Sets a specific value in the instance to the given value * (internal floating-point format). Performs a deep copy * of the vector of attribute values before the value is set. * Does exactly the same thing as setValue(). * * @param indexOfIndex the index of the attribute's index * @param value the new attribute value (If the corresponding * attribute is nominal (or a string) then this is the new value's * index as a double). */ public void setValueSparse(int indexOfIndex, double value) { freshAttributeVector(); m_AttValues[indexOfIndex] = value; } /** * Sets a value of a nominal or string attribute to the given * value. Performs a deep copy of the vector of attribute values * before the value is set. * * @param attIndex the attribute's index * @param value the new attribute value (If the attribute * is a string attribute and the value can't be found, * the value is added to the attribute). * @throws UnassignedDatasetException if the dataset is not set * @throws IllegalArgumentException if the selected * attribute is not nominal or a string, or the supplied value couldn't * be found for a nominal attribute */ //@ requires m_Dataset != null; public final void setValue(int attIndex, String value) { int valIndex; if (m_Dataset == null) { throw new UnassignedDatasetException("Instance doesn't have access to a dataset!"); } if (!attribute(attIndex).isNominal() && !attribute(attIndex).isString()) { throw new IllegalArgumentException("Attribute neither nominal nor string!"); } valIndex = attribute(attIndex).indexOfValue(value); if (valIndex == -1) { if (attribute(attIndex).isNominal()) { throw new IllegalArgumentException("Value not defined for given nominal attribute!"); } else { attribute(attIndex).forceAddValue(value); valIndex = attribute(attIndex).indexOfValue(value); } } setValue(attIndex, (double)valIndex); } /** * Sets a specific value in the instance to the given value * (internal floating-point format). Performs a deep copy of the * vector of attribute values before the value is set, so if you are * planning on calling setValue many times it may be faster to * create a new instance using toDoubleArray. The given attribute * has to belong to a dataset. * * @param att the attribute * @param value the new attribute value (If the corresponding * attribute is nominal (or a string) then this is the new value's * index as a double). */ public final void setValue(Attribute att, double value) { setValue(att.index(), value); } /** * Sets a value of an nominal or string attribute to the given * value. Performs a deep copy of the vector of attribute values * before the value is set, so if you are planning on calling setValue many * times it may be faster to create a new instance using toDoubleArray. * The given attribute has to belong to a dataset. * * @param att the attribute * @param value the new attribute value (If the attribute * is a string attribute and the value can't be found, * the value is added to the attribute). * @throws IllegalArgumentException if the the attribute is not * nominal or a string, or the value couldn't be found for a nominal * attribute */ public final void setValue(Attribute att, String value) { if (!att.isNominal() && !att.isString()) { throw new IllegalArgumentException("Attribute neither nominal nor string!"); } int valIndex = att.indexOfValue(value); if (valIndex == -1) { if (att.isNominal()) { throw new IllegalArgumentException("Value not defined for given nominal attribute!"); } else { att.forceAddValue(value); valIndex = att.indexOfValue(value); } } setValue(att.index(), (double)valIndex); } /** * Modifies the instances value for an attribute (floating point * representation). Unlike in <code>setValue</code> no deep copy is * produced, i.e. the actual value is modified. * * @param attIndex the attribute's index * @param value the new attribute value (If the corresponding * attribute is nominal (or a string) then this is the new value's * index as a double). * @author Arne Muller (arne.muller@gmail.com) */ public void modifyValue(int attIndex, double value) { m_AttValues[attIndex] = value; } /** * Sets the weight of an instance. * * @param weight the weight */ public final void setWeight(double weight) { m_Weight = weight; } /** * Returns the relational value of a relational attribute. * * @param attIndex the attribute's index * @return the corresponding relation as an Instances object * @throws IllegalArgumentException if the attribute is not a * relation-valued attribute * @throws UnassignedDatasetException if the instance doesn't belong * to a dataset. */ //@ requires m_Dataset != null; public final /*@pure@*/ Instances relationalValue(int attIndex) { if (m_Dataset == null) { throw new UnassignedDatasetException("Instance doesn't have access to a dataset!"); } return relationalValue(m_Dataset.attribute(attIndex)); } /** * Returns the relational value of a relational attribute. * * @param att the attribute * @return the corresponding relation as an Instances object * @throws IllegalArgumentException if the attribute is not a * relation-valued attribute * @throws UnassignedDatasetException if the instance doesn't belong * to a dataset. */ public final /*@pure@*/ Instances relationalValue(Attribute att) { int attIndex = att.index(); if (att.isRelationValued()) { return att.relation((int) value(attIndex)); } else { throw new IllegalArgumentException("Attribute isn't relation-valued!"); } } /** * Returns the value of a nominal, string, date, or relational attribute * for the instance as a string. * * @param attIndex the attribute's index * @return the value as a string * @throws IllegalArgumentException if the attribute is not a nominal, * string, date, or relation-valued attribute. * @throws UnassignedDatasetException if the instance doesn't belong * to a dataset. */ //@ requires m_Dataset != null; public final /*@pure@*/ String stringValue(int attIndex) { if (m_Dataset == null) { throw new UnassignedDatasetException("Instance doesn't have access to a dataset!"); } return stringValue(m_Dataset.attribute(attIndex)); } /** * Returns the value of a nominal, string, date, or relational attribute * for the instance as a string. * * @param att the attribute * @return the value as a string * @throws IllegalArgumentException if the attribute is not a nominal, * string, date, or relation-valued attribute. * @throws UnassignedDatasetException if the instance doesn't belong * to a dataset. */ public final /*@pure@*/ String stringValue(Attribute att) { int attIndex = att.index(); switch (att.type()) { case Attribute.NOMINAL: case Attribute.STRING: return att.value((int) value(attIndex)); case Attribute.DATE: return att.formatDate(value(attIndex)); case Attribute.RELATIONAL: return att.relation((int) value(attIndex)).stringWithoutHeader(); default: throw new IllegalArgumentException("Attribute isn't nominal, string or date!"); } } /** * Returns the values of each attribute as an array of doubles. * * @return an array containing all the instance attribute values */ public double[] toDoubleArray() { double[] newValues = new double[m_AttValues.length]; System.arraycopy(m_AttValues, 0, newValues, 0, m_AttValues.length); return newValues; } /** * Returns the description of one instance. If the instance * doesn't have access to a dataset, it returns the internal * floating-point values. Quotes string * values that contain whitespace characters. * * @return the instance's description as a string */ public String toString() { StringBuffer text = new StringBuffer(); for (int i = 0; i < m_AttValues.length; i++) { if (i > 0) text.append(","); text.append(toString(i)); } if (m_Weight != 1.0) { text.append(",{" + Utils.doubleToString(m_Weight, 6) + "}"); } return text.toString(); } /** * Returns the description of one value of the instance as a * string. If the instance doesn't have access to a dataset, it * returns the internal floating-point value. Quotes string * values that contain whitespace characters, or if they * are a question mark. * * @param attIndex the attribute's index * @return the value's description as a string */ public final /*@pure@*/ String toString(int attIndex) { StringBuffer text = new StringBuffer(); if (isMissing(attIndex)) { text.append("?"); } else { if (m_Dataset == null) { text.append(Utils.doubleToString(m_AttValues[attIndex],6)); } else { switch (m_Dataset.attribute(attIndex).type()) { case Attribute.NOMINAL: case Attribute.STRING: case Attribute.DATE: case Attribute.RELATIONAL: text.append(Utils.quote(stringValue(attIndex))); break; case Attribute.NUMERIC: text.append(Utils.doubleToString(value(attIndex),6)); break; default: throw new IllegalStateException("Unknown attribute type"); } } } return text.toString(); } /** * Returns the description of one value of the instance as a * string. If the instance doesn't have access to a dataset it * returns the internal floating-point value. Quotes string * values that contain whitespace characters, or if they * are a question mark. * The given attribute has to belong to a dataset. * * @param att the attribute * @return the value's description as a string */ public final String toString(Attribute att) { return toString(att.index()); } /** * Returns an instance's attribute value in internal format. * * @param attIndex the attribute's index * @return the specified value as a double (If the corresponding * attribute is nominal (or a string) then it returns the value's index as a * double). */ public /*@pure@*/ double value(int attIndex) { return m_AttValues[attIndex]; } /** * Returns an instance's attribute value in internal format. * Does exactly the same thing as value() if applied to an Instance. * * @param indexOfIndex the index of the attribute's index * @return the specified value as a double (If the corresponding * attribute is nominal (or a string) then it returns the value's index as a * double). */ public /*@pure@*/ double valueSparse(int indexOfIndex) { return m_AttValues[indexOfIndex]; } /** * Returns an instance's attribute value in internal format. * The given attribute has to belong to a dataset. * * @param att the attribute * @return the specified value as a double (If the corresponding * attribute is nominal (or a string) then it returns the value's index as a * double). */ public /*@pure@*/ double value(Attribute att) { return value(att.index()); } /** * Returns the instance's weight. * * @return the instance's weight as a double */ public final /*@pure@*/ double weight() { return m_Weight; } /** * Deletes an attribute at the given position (0 to * numAttributes() - 1). * * @param position the attribute's position */ void forceDeleteAttributeAt(int position) { double[] newValues = new double[m_AttValues.length - 1]; System.arraycopy(m_AttValues, 0, newValues, 0, position); if (position < m_AttValues.length - 1) { System.arraycopy(m_AttValues, position + 1, newValues, position, m_AttValues.length - (position + 1)); } m_AttValues = newValues; } /** * Inserts an attribute at the given position * (0 to numAttributes()) and sets its value to be missing. * * @param position the attribute's position */ void forceInsertAttributeAt(int position) { double[] newValues = new double[m_AttValues.length + 1]; System.arraycopy(m_AttValues, 0, newValues, 0, position); newValues[position] = MISSING_VALUE; System.arraycopy(m_AttValues, position, newValues, position + 1, m_AttValues.length - position); m_AttValues = newValues; } /** * Private constructor for subclasses. Does nothing. */ protected Instance() { } /** * Clones the attribute vector of the instance and * overwrites it with the clone. */ private void freshAttributeVector() { m_AttValues = toDoubleArray(); } /** * Main method for testing this class. * * @param options the commandline options - ignored */ //@ requires options != null; public static void main(String[] options) { try { // Create numeric attributes "length" and "weight" Attribute length = new Attribute("length"); Attribute weight = new Attribute("weight"); // Create vector to hold nominal values "first", "second", "third" FastVector my_nominal_values = new FastVector(3); my_nominal_values.addElement("first"); my_nominal_values.addElement("second"); my_nominal_values.addElement("third"); // Create nominal attribute "position" Attribute position = new Attribute("position", my_nominal_values); // Create vector of the above attributes FastVector attributes = new FastVector(3); attributes.addElement(length); attributes.addElement(weight); attributes.addElement(position); // Create the empty dataset "race" with above attributes Instances race = new Instances("race", attributes, 0); // Make position the class attribute race.setClassIndex(position.index()); // Create empty instance with three attribute values Instance inst = new Instance(3); // Set instance's values for the attributes "length", "weight", and "position" inst.setValue(length, 5.3); inst.setValue(weight, 300); inst.setValue(position, "first"); // Set instance's dataset to be the dataset "race" inst.setDataset(race); // Print the instance System.out.println("The instance: " + inst); // Print the first attribute System.out.println("First attribute: " + inst.attribute(0)); // Print the class attribute System.out.println("Class attribute: " + inst.classAttribute()); // Print the class index System.out.println("Class index: " + inst.classIndex()); // Say if class is missing System.out.println("Class is missing: " + inst.classIsMissing()); // Print the instance's class value in internal format System.out.println("Class value (internal format): " + inst.classValue()); // Print a shallow copy of this instance Instance copy = (Instance) inst.copy(); System.out.println("Shallow copy: " + copy); // Set dataset for shallow copy copy.setDataset(inst.dataset()); System.out.println("Shallow copy with dataset set: " + copy); // Unset dataset for copy, delete first attribute, and insert it again copy.setDataset(null); copy.deleteAttributeAt(0); copy.insertAttributeAt(0); copy.setDataset(inst.dataset()); System.out.println("Copy with first attribute deleted and inserted: " + copy); // Enumerate attributes (leaving out the class attribute) System.out.println("Enumerating attributes (leaving out class):"); Enumeration enu = inst.enumerateAttributes(); while (enu.hasMoreElements()) { Attribute att = (Attribute) enu.nextElement(); System.out.println(att); } // Headers are equivalent? System.out.println("Header of original and copy equivalent: " + inst.equalHeaders(copy)); // Test for missing values System.out.println("Length of copy missing: " + copy.isMissing(length)); System.out.println("Weight of copy missing: " + copy.isMissing(weight.index())); System.out.println("Length of copy missing: " + Instance.isMissingValue(copy.value(length))); System.out.println("Missing value coded as: " + Instance.missingValue()); // Prints number of attributes and classes System.out.println("Number of attributes: " + copy.numAttributes()); System.out.println("Number of classes: " + copy.numClasses()); // Replace missing values double[] meansAndModes = {2, 3, 0}; copy.replaceMissingValues(meansAndModes); System.out.println("Copy with missing value replaced: " + copy); // Setting and getting values and weights copy.setClassMissing(); System.out.println("Copy with missing class: " + copy); copy.setClassValue(0); System.out.println("Copy with class value set to first value: " + copy); copy.setClassValue("third"); System.out.println("Copy with class value set to \"third\": " + copy); copy.setMissing(1); System.out.println("Copy with second attribute set to be missing: " + copy); copy.setMissing(length); System.out.println("Copy with length set to be missing: " + copy); copy.setValue(0, 0); System.out.println("Copy with first attribute set to 0: " + copy); copy.setValue(weight, 1); System.out.println("Copy with weight attribute set to 1: " + copy); copy.setValue(position, "second"); System.out.println("Copy with position set to \"second\": " + copy); copy.setValue(2, "first"); System.out.println("Copy with last attribute set to \"first\": " + copy); System.out.println("Current weight of instance copy: " + copy.weight()); copy.setWeight(2); System.out.println("Current weight of instance copy (set to 2): " + copy.weight()); System.out.println("Last value of copy: " + copy.toString(2)); System.out.println("Value of position for copy: " + copy.toString(position)); System.out.println("Last value of copy (internal format): " + copy.value(2)); System.out.println("Value of position for copy (internal format): " + copy.value(position)); } catch (Exception e) { e.printStackTrace(); } } /** * Returns the revision string. * * @return the revision */ public String getRevision() { return RevisionUtils.extract("$Revision: 1.28 $"); } }

The table below shows all metrics for Instance.java.

MetricValueDescription
BLOCKS108.00Number of blocks
BLOCK_COMMENT20.00Number of block comment lines
COMMENTS596.00Comment lines
COMMENT_DENSITY 1.71Comment density
COMPARISONS69.00Number of comparison operators
CYCLOMATIC118.00Cyclomatic complexity
DECL_COMMENTS92.00Comments in declarations
DOC_COMMENT521.00Number of javadoc comment lines
ELOC349.00Effective lines of code
EXEC_COMMENTS24.00Comments in executable code
EXITS72.00Procedure exits
FUNCTIONS58.00Number of function declarations
HALSTEAD_DIFFICULTY96.59Halstead difficulty
HALSTEAD_EFFORT 0.00Halstead effort
INTERFACE_COMPLEXITY136.00Interface complexity
JAVA0001 0.00JAVA0001 Package name does not contain only lower case letters
JAVA0002 1.00JAVA0002 Package name does not begin with a top level domain name or country code
JAVA0003 0.00JAVA0003 Minimize use of on-demand (.*) imports
JAVA0004 0.00JAVA0004 Unnecessary import from java.lang
JAVA0005 0.00JAVA0005 Imports not in specified order
JAVA0006 0.00JAVA0006 Empty finally block
JAVA0007 0.00JAVA0007 Should not declare public field
JAVA0008 0.00JAVA0008 Empty catch block
JAVA0009 0.00JAVA0009 Protected member in final class
JAVA0010 0.00JAVA0010 Non-instantiable class does not contain a non-private static member
JAVA0011 0.00JAVA0011 Abstract class does not contain an abstract method
JAVA0012 0.00JAVA0012 Non-constructor method with same name as declaring class
JAVA0013 0.00JAVA0013 Non-blank final field is not static
JAVA0014 0.00JAVA0014 Class with only static members has non-private constructor
JAVA0015 0.00JAVA0015 Package class contains public nested type
JAVA0016 0.00JAVA0016 Abstract class contains public constructor
JAVA0017 0.00JAVA0017 Class name does not have required form
JAVA0018 0.00JAVA0018 Method name does not have required form
JAVA0019 0.00JAVA0019 Interface name does not have required form
JAVA0020 0.00JAVA0020 Field name does not have required form
JAVA0021 0.00JAVA0021 Interface method name does not have required form
JAVA0022 0.00JAVA0022 Static final field name does not have required form
JAVA0023 0.00JAVA0023 Empty finalize method
JAVA0024 0.00JAVA0024 Empty class
JAVA0025 0.00JAVA0025 Method override is empty
JAVA0026 0.00JAVA0026 Finalize method with parameters
JAVA0029 0.00JAVA0029 Private method not used
JAVA0030 0.00JAVA0030 Private field not used
JAVA0031 0.00JAVA0031 Case statement not properly closed
JAVA0032 0.00JAVA0032 Switch statement missing default
JAVA0033 0.00JAVA0033 default: not last case in switch statement
JAVA0034 1.00JAVA0034 Missing braces in if statement
JAVA0035 0.00JAVA0035 Missing braces in for statement
JAVA0036 0.00JAVA0036 Missing braces in while statement
JAVA0038 0.00JAVA0038 Non-case label in switch statement
JAVA0039 0.00JAVA0039 Break statement with label
JAVA0040 0.00JAVA0040 Switch statement contains N cases (maximum: M)
JAVA0041 0.00JAVA0041 Nested synchronized block
JAVA0042 0.00JAVA0042 Empty synchronized statement
JAVA0043 0.00JAVA0043 Inner class does not use outer class
JAVA0044 0.00JAVA0044 Serializable class with no instance variables
JAVA0045 0.00JAVA0045 Serializable class with only transient fields
JAVA0046 0.00JAVA0046 Name of class not derived from Exception ends with 'Exception'
JAVA0047 0.00JAVA0047 Serializable class derives from invalid base class
JAVA0048 0.00JAVA0048 Name of class derived from Exception does not end with 'Exception'
JAVA0049 0.00JAVA0049 Nested block at depth N (maximum: M)
JAVA0050 0.00JAVA0050 Class derives from java.lang.Error
JAVA0051 0.00JAVA0051 Class derives from java.lang.RuntimeException
JAVA0052 0.00JAVA0052 Class derives from java.lang.Throwable
JAVA0053 0.00JAVA0053 Unused label
JAVA0054 0.00JAVA0054 Inheritance depth N exceeds maximum M
JAVA0055 0.00JAVA0055 Class should be interface
JAVA0056 0.00JAVA0056 Unnecessary abstract modifier for interface or annotation
JAVA0057 0.00JAVA0057 Unnecessary default constructor
JAVA0058 0.00JAVA0058 Constructor calls super()
JAVA0059 0.00JAVA0059 Method override only calls super()
JAVA0061 0.00JAVA0061 Inaccessible member in anonymous class
JAVA0062 0.00JAVA0062 Public class missing public member or protected constructor
JAVA0063 0.00JAVA0063 Identifier name should not contain '$'
JAVA0064 0.00JAVA0064 N variations of identifier name (maximum: M)
JAVA0065 0.00JAVA0065 Unnecessary final modifier for method in final class
JAVA0066 0.00JAVA0066 Unnecessary modifier for interface nested type
JAVA0067 0.00JAVA0067 Array descriptor on identifier name
JAVA0068 0.00JAVA0068 Modifiers not declared in recommended order
JAVA0071 0.00JAVA0071 Strings compared with ==
JAVA0073 0.00JAVA0073 Integer division in floating-point context
JAVA0074 0.00JAVA0074 Use of Object.notify()
JAVA0075 0.00JAVA0075 Method parameter hides field
JAVA0076 3.00JAVA0076 Use of magic number
JAVA0077 0.00JAVA0077 Private field not used in declaring class
JAVA0078 1.00JAVA0078 Floating point values compared with ==
JAVA0079 0.00JAVA0079 Use of instance to reference static member
JAVA0080 0.00JAVA0080 Import declaration not used
JAVA0081 0.00JAVA0081 Boolean literal in comparison
JAVA0082 0.00JAVA0082 Unnecessary widening cast
JAVA0083 0.00JAVA0083 Unnecessary instanceof test
JAVA0084 0.00JAVA0084 Should use compound assignment operator
JAVA0085 0.00JAVA0085 Use of sun.* class
JAVA0087 0.00JAVA0087 Use of Thread.sleep()
JAVA0089 0.00JAVA0089 Use of restricted package
JAVA0092 0.00JAVA0092 Use of restricted type
JAVA0093 0.00JAVA0093 Redundant assignment
JAVA0094 0.00JAVA0094 Field hides a superclass field
JAVA0095 0.00JAVA0095 Uninitialized private field
JAVA0096 0.00JAVA0096 Field in nested class hides outer field
JAVA0098 1.00JAVA0098 Minimize use of implicit field initializers
JAVA0100 0.00JAVA0100 Class contains N non-final fields (maximum: M)
JAVA0101 0.00JAVA0101 Unnecessary modifier for field in interface
JAVA0102 0.00JAVA0102 Last statement in finalize() not super.finalize()
JAVA0103 0.00JAVA0103 Explicit call to finalize()
JAVA0104 0.00JAVA0104 finalize() only calls super.finalize()
JAVA0105 0.00JAVA0105 Duplicate import declaration
JAVA0106 0.00JAVA0106 Unnecessary import from current package
JAVA0108 0.00JAVA0108 Incorrect javadoc: no @param tag for 'parameter'
JAVA0109 0.00JAVA0109 Incorrect javadoc: no parameter 'parameter'
JAVA0110 0.00JAVA0110 Incorrect javadoc: no @return tag
JAVA0111 0.00JAVA0111 Incorrect javadoc: @return tag for void method
JAVA0112 1.00JAVA0112 Incorrect javadoc: no exception 'exception' in throws
JAVA0113 0.00JAVA0113 Incorrect javadoc: no @author tag
JAVA0114 0.00JAVA0114 Incorrect javadoc: no @version tag
JAVA0115 0.00JAVA0115 Incorrect javadoc: no @throws or @exception tag for 'exception'
JAVA0116 0.00JAVA0116 Missing javadoc: field 'field'
JAVA0117 0.00JAVA0117 Missing javadoc: method 'method'
JAVA0118 0.00JAVA0118 Missing javadoc: type 'type'
JAVA0119 0.00JAVA0119 Control variable changed within body of for loop
JAVA0123 0.00JAVA0123 Use all three components of for loop
JAVA0125 0.00JAVA0125 Continue statement with label
JAVA0126 0.00JAVA0126 Method declares unchecked exception in throws
JAVA0128 0.00JAVA0128 Public constructor in non-public class
JAVA0130 0.00JAVA0130 Non-static method does not use instance fields
JAVA0131 0.00JAVA0131 Compatible method does not override base
JAVA0132 0.00JAVA0132 Method overload with compatible signature
JAVA0133 0.00JAVA0133 Non-synchronized method overrides synchronized method
JAVA0135 0.00JAVA0135 Only one of Object.equals and Object.hashCode defined: missing 'method'
JAVA0136 1.00JAVA0136 N methods defined in class (maximum: M)
JAVA0137 0.00JAVA0137 Non-abstract class missing constructor
JAVA0138 0.00JAVA0138 N parameters defined for method (maximum: M)
JAVA0139 0.00JAVA0139 Definition of main other than public static void main(java.lang.String[])
JAVA0141 0.00JAVA0141 Unnecessary modifier for method in interface
JAVA0143 0.00JAVA0143 Synchronized method
JAVA0144 0.00JAVA0144 Line exceeds maximum M characters
JAVA014537.00JAVA0145 Tab character used in source file
JAVA0150 0.00JAVA0150 java.lang.Error (or subclass) thrown
JAVA0153 0.00JAVA0153 Inefficient conversion of integer to string
JAVA0159 0.00JAVA0159 Inefficient conversion of string to integer
JAVA0160 0.00JAVA0160 Method does not throw specified exception
JAVA0161 0.00JAVA0161 Conditional wait() not in loop
JAVA0163 0.00JAVA0163 Empty statement
JAVA0165 0.00JAVA0165 Conflicting return statement in finally block
JAVA0166 1.00JAVA0166 Generic exception caught
JAVA0167 0.00JAVA0167 ThreadDeath not rethrown
JAVA0169 0.00JAVA0169 Unnecessary catch block: exception 'exception'
JAVA0170 0.00JAVA0170 Caught exception not derived from java.lang.Exception
JAVA0171 0.00JAVA0171 Unused local variable
JAVA0173 0.00JAVA0173 Unused method parameter
JAVA0174 0.00JAVA0174 Assigned local variable never used
JAVA0175 0.00JAVA0175 Successive assignment to variable
JAVA0176 0.00JAVA0176 Local variable name does not have required form
JAVA0177 1.00JAVA0177 Variable declaration missing initializer
JAVA0179 0.00JAVA0179 Local variable hides visible field
JAVA0233 1.00JAVA0233 Definition of serialVersionUID other than 'private static final long serialVersionUID'
JAVA0234 0.00JAVA0234 Class is Serializable but does not define serialVersionUID
JAVA0235 0.00JAVA0235 Class defines serialVersionUID but does not implement Serializable
JAVA0236 0.00JAVA0236 Attempt to clone an object which does not implement Cloneable
JAVA0237 0.00JAVA0237 Class implements Cloneable but does not have public clone method
JAVA0238 0.00JAVA0238 Clone method does not call super.clone()
JAVA0239 0.00JAVA0239 Class declares 'readObject' or 'writeObject' but does not implement Serializable
JAVA0240 0.00JAVA0240 Serializable class which declares readObject or writeObject but not both
JAVA0241 0.00JAVA0241 'readObject' or 'writeObject' should be declared private in Serializable class
JAVA0242 0.00JAVA0242 Transient field in non-Serializable class
JAVA0243 0.00JAVA0243 'readResolve' or 'writeReplace' should be declared private or protected
JAVA0244 0.00JAVA0244 Field or method name in subclass differs only by case from inherited field or method
JAVA0245 0.00JAVA0245 JUnit TestCase with non-trivial constructor
JAVA0246 0.00JAVA0246 JUnit assertXXX statement missing message parameter
JAVA0247 0.00JAVA0247 JUnit 'setUp()' and 'tearDown()' should call super method
JAVA0248 0.00JAVA0248 JUnit method 'setUp' or 'tearDown' with incorrect signature
JAVA0249 0.00JAVA0249 JUnit TestCase 'suite()' should be declared static
JAVA0250 0.00JAVA0250 JUnit TestCase declares testXXX method with incorrect signature
JAVA0251 0.00JAVA0251 Use '%n' for line breaks in printf/format for platform independence
JAVA0252 0.00JAVA0252 'enum' is a Java 1.5 reserved word
JAVA0253 0.00JAVA0253 Not all enum constants consumed in switch statement
JAVA0254 0.00JAVA0254 Use enhanced for loop construct instead of Iterator
JAVA0255 0.00JAVA0255 Result of method invocation not used
JAVA0256 1.00JAVA0256 Assignment of external collection/array to field
JAVA0257 0.00JAVA0257 Use of 'Constant Interface' anti-pattern
JAVA0258 0.00JAVA0258 Implement Iterable for foreach compatibility
JAVA0259 0.00JAVA0259 Return of collection/array field
JAVA0260 0.00JAVA0260 Use 'enum' instead of Enumerated Type pattern
JAVA0261 0.00JAVA0261 Use specialized Enum collection types
JAVA0262 0.00JAVA0262 Use of char in integer context
JAVA0263 0.00JAVA0263 Long literal ends with 'l' instead of 'L'
JAVA0264 0.00JAVA0264 Integer math in long context - check for overflow
JAVA0265 1.00JAVA0265 Use of Throwable.printStackTrace()
JAVA026634.00JAVA0266 Use of System.out
JAVA0267 0.00JAVA0267 Use of System.err
JAVA0269 0.00JAVA0269 Contents of StringBuffer never used
JAVA0270 0.00JAVA0270 Use Java 5.0 enhanced for loop construct to iterate over all elements in an array
JAVA0271 0.00JAVA0271 Minimize use of on-demand (.*) static imports
JAVA0272 0.00JAVA0272 Thread.run() called
JAVA0273 0.00JAVA0273 Non-final derivative of Thread calls start() in constructor
JAVA0274 0.00JAVA0274 Serializable class has a synchronized readObject()
JAVA0275 0.00JAVA0275 Serializable class has a synchronized writeObject() and no other synchronized methods
JAVA0276 0.00JAVA0276 Unnecessary use of String constructor
JAVA0277 0.00JAVA0277 Iterator.next() implementation does not throw NoSuchElementException
JAVA0278 0.00JAVA0278 Unnecessary use of Boolean constructor
JAVA0279 0.00JAVA0279 Serialization method readObject or readObjectNoData calls an overridable method
JAVA0280 0.00JAVA0280 IllegalMonitorStateException caught
JAVA0281 0.00JAVA0281 Iterator.next() not called in loop
JAVA0282 0.00JAVA0282 Call to Iterator.next() in loop which does not test Iterator.hasNext()
JAVA0283 0.00JAVA0283 Control variable not updated in loop body
JAVA0284 0.00JAVA0284 Explicit garbage collection
JAVA0285 0.00JAVA0285 Dereference of potentially null variable
JAVA0286 0.00JAVA0286 Dereference of null variable
JAVA0287 0.00JAVA0287 Unnecessary null check
JAVA0288 0.00JAVA0288 Inconsistent null check
LINES1206.00Number of lines in the source file
LINE_COMMENT55.00Number of line comments
LOC454.00Lines of code
LOGICAL_LINES226.00Number of statements
LOOPS 7.00Number of loops
NEST_DEPTH 4.00Maximum nesting depth
OPERANDS1003.00Number of operands
OPERATORS2052.00Number of operators
PARAMS46.00Number of formal parameter declarations
PROGRAM_LENGTH3055.00Halstead program length
PROGRAM_VOCAB322.00Halstead program vocabulary
PROGRAM_VOLUME 0.00Halstead program volume
RETURNS90.00Number of return points from functions
SIZE38503.00Size of the file in bytes
UNIQUE_OPERANDS270.00Number of unique operands
UNIQUE_OPERATORS52.00Number of unique operators
WHITESPACE156.00Number of whitespace lines