📄 attribute.java
字号:
* @return a copy of this attribute with the same index */ //@ also ensures \result instanceof Attribute; public /*@ pure non_null @*/ Object copy() { Attribute copy = new Attribute(m_Name); copy.m_Index = m_Index; copy.m_Type = m_Type; copy.m_Values = m_Values; copy.m_Hashtable = m_Hashtable; copy.m_DateFormat = m_DateFormat; copy.m_Header = m_Header; copy.setMetadata(m_Metadata); return copy; } /** * Returns an enumeration of all the attribute's values if the * attribute is nominal, string, or relation-valued, null otherwise. * * @return enumeration of all the attribute's values */ public final /*@ pure @*/ Enumeration enumerateValues() { if (isNominal() || isString()) { final Enumeration ee = m_Values.elements(); return new Enumeration () { public boolean hasMoreElements() { return ee.hasMoreElements(); } public Object nextElement() { Object oo = ee.nextElement(); if (oo instanceof SerializedObject) { return ((SerializedObject)oo).getObject(); } else { return oo; } } }; } return null; } /** * Tests if given attribute is equal to this attribute. * * @param other the Object to be compared to this attribute * @return true if the given attribute is equal to this attribute */ public final /*@ pure @*/ boolean equals(Object other) { if ((other == null) || !(other.getClass().equals(this.getClass()))) { return false; } Attribute att = (Attribute) other; if (!m_Name.equals(att.m_Name)) { return false; } if (isNominal() && att.isNominal()) { if (m_Values.size() != att.m_Values.size()) { return false; } for (int i = 0; i < m_Values.size(); i++) { if (!m_Values.elementAt(i).equals(att.m_Values.elementAt(i))) { return false; } } return true; } if (isRelationValued() && att.isRelationValued()) { if (!m_Header.equalHeaders(att.m_Header)) { return false; } return true; } return (type() == att.type()); } /** * Returns the index of this attribute. * * @return the index of this attribute */ //@ ensures \result == m_Index; public final /*@ pure @*/ int index() { return m_Index; } /** * Returns the index of a given attribute value. (The index of * the first occurence of this value.) * * @param value the value for which the index is to be returned * @return the index of the given attribute value if attribute * is nominal or a string, -1 if it is not or the value * can't be found */ public final int indexOfValue(String value) { if (!isNominal() && !isString()) return -1; Object store = value; if (value.length() > STRING_COMPRESS_THRESHOLD) { try { store = new SerializedObject(value, true); } catch (Exception ex) { System.err.println("Couldn't compress string attribute value -" + " searching uncompressed."); } } Integer val = (Integer)m_Hashtable.get(store); if (val == null) return -1; else return val.intValue(); } /** * Test if the attribute is nominal. * * @return true if the attribute is nominal */ //@ ensures \result <==> (m_Type == NOMINAL); public final /*@ pure @*/ boolean isNominal() { return (m_Type == NOMINAL); } /** * Tests if the attribute is numeric. * * @return true if the attribute is numeric */ //@ ensures \result <==> ((m_Type == NUMERIC) || (m_Type == DATE)); public final /*@ pure @*/ boolean isNumeric() { return ((m_Type == NUMERIC) || (m_Type == DATE)); } /** * Tests if the attribute is relation valued. * * @return true if the attribute is relation valued */ //@ ensures \result <==> (m_Type == RELATIONAL); public final /*@ pure @*/ boolean isRelationValued() { return (m_Type == RELATIONAL); } /** * Tests if the attribute is a string. * * @return true if the attribute is a string */ //@ ensures \result <==> (m_Type == STRING); public final /*@ pure @*/ boolean isString() { return (m_Type == STRING); } /** * Tests if the attribute is a date type. * * @return true if the attribute is a date type */ //@ ensures \result <==> (m_Type == DATE); public final /*@ pure @*/ boolean isDate() { return (m_Type == DATE); } /** * Returns the attribute's name. * * @return the attribute's name as a string */ //@ ensures \result == m_Name; public final /*@ pure @*/ String name() { return m_Name; } /** * Returns the number of attribute values. Returns 0 for * attributes that are not either nominal, string, or * relation-valued. * * @return the number of attribute values */ public final /*@ pure @*/ int numValues() { if (!isNominal() && !isString() && !isRelationValued()) { return 0; } else { return m_Values.size(); } } /** * Returns a description of this attribute in ARFF format. Quotes * strings if they contain whitespace characters, or if they * are a question mark. * * @return a description of this attribute as a string */ public final String toString() { StringBuffer text = new StringBuffer(); text.append(ARFF_ATTRIBUTE).append(" ").append(Utils.quote(m_Name)).append(" "); switch (m_Type) { case NOMINAL: text.append('{'); Enumeration enu = enumerateValues(); while (enu.hasMoreElements()) { text.append(Utils.quote((String) enu.nextElement())); if (enu.hasMoreElements()) text.append(','); } text.append('}'); break; case NUMERIC: text.append(ARFF_ATTRIBUTE_NUMERIC); break; case STRING: text.append(ARFF_ATTRIBUTE_STRING); break; case DATE: text.append(ARFF_ATTRIBUTE_DATE).append(" ").append(Utils.quote(m_DateFormat.toPattern())); break; case RELATIONAL: text.append(ARFF_ATTRIBUTE_RELATIONAL).append("\n"); Enumeration enm = m_Header.enumerateAttributes(); while (enm.hasMoreElements()) { text.append(enm.nextElement()).append("\n"); } text.append(ARFF_END_SUBRELATION).append(" ").append(Utils.quote(m_Name)); break; default: text.append("UNKNOWN"); break; } return text.toString(); } /** * Returns the attribute's type as an integer. * * @return the attribute's type. */ //@ ensures \result == m_Type; public final /*@ pure @*/ int type() { return m_Type; } /** * Returns the Date format pattern in case this attribute is of type DATE, * otherwise an empty string. * * @return the date format pattern * @see SimpleDateFormat */ public final String getDateFormat() { if (isDate()) return m_DateFormat.toPattern(); else return ""; } /** * Returns a value of a nominal or string attribute. Returns an * empty string if the attribute is neither a string nor a nominal * attribute. * * @param valIndex the value's index * @return the attribute's value as a string */ public final /*@ non_null pure @*/ String value(int valIndex) { if (!isNominal() && !isString()) { return ""; } else { Object val = m_Values.elementAt(valIndex); // If we're storing strings compressed, uncompress it. if (val instanceof SerializedObject) { val = ((SerializedObject)val).getObject(); } return (String) val; } } /** * Returns the header info for a relation-valued attribute, * null if the attribute is not relation-valued. * * @return the attribute's value as an Instances object */ public final /*@ non_null pure @*/ Instances relation() { if (!isRelationValued()) { return null; } else { return m_Header; } } /** * Returns a value of a relation-valued attribute. Returns * null if the attribute is not relation-valued. * * @param valIndex the value's index * @return the attribute's value as an Instances object */ public final /*@ non_null pure @*/ Instances relation(int valIndex) { if (!isRelationValued()) { return null; } else { return (Instances) m_Values.elementAt(valIndex); } } /** * Constructor for a numeric attribute with a particular index. * * @param attributeName the name for the attribute * @param index the attribute's index */ //@ requires attributeName != null; //@ requires index >= 0; //@ ensures m_Name == attributeName; //@ ensures m_Index == index; public Attribute(String attributeName, int index) { this(attributeName); m_Index = index; } /** * Constructor for date attributes with a particular index. * * @param attributeName the name for the attribute * @param dateFormat a string suitable for use with * SimpleDateFormatter for parsing dates. Null for a default format * string. * @param index the attribute's index */ //@ requires attributeName != null; //@ requires index >= 0; //@ ensures m_Name == attributeName; //@ ensures m_Index == index; public Attribute(String attributeName, String dateFormat, int index) { this(attributeName, dateFormat); m_Index = index; } /** * Constructor for nominal attributes and string attributes with * a particular index. * If a null vector of attribute values is passed to the method, * the attribute is assumed to be a string. * * @param attributeName the name for the attribute * @param attributeValues a vector of strings denoting the attribute values. * Null if the attribute is a string attribute. * @param index the attribute's index */ //@ requires attributeName != null; //@ requires index >= 0; //@ ensures m_Name == attributeName; //@ ensures m_Index == index; public Attribute(String attributeName, FastVector attributeValues, int index) { this(attributeName, attributeValues); m_Index = index; } /** * Constructor for a relation-valued attribute with a particular index. * * @param attributeName the name for the attribute * @param header the header information for this attribute * @param index the attribute's index */ //@ requires attributeName != null; //@ requires index >= 0; //@ ensures m_Name == attributeName; //@ ensures m_Index == index; public Attribute(String attributeName, Instances header, int index) { this(attributeName, header);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -