📄 attribute.java
字号:
m_Index = index; } /** * Adds a string value to the list of valid strings for attributes * of type STRING and returns the index of the string. * * @param value The string value to add * @return the index assigned to the string, or -1 if the attribute is not * of type Attribute.STRING */ /*@ requires value != null; ensures isString() && 0 <= \result && \result < m_Values.size() || ! isString() && \result == -1; */ public int addStringValue(String value) { if (!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 -" + " storing uncompressed."); } } Integer index = (Integer)m_Hashtable.get(store); if (index != null) { return index.intValue(); } else { int intIndex = m_Values.size(); m_Values.addElement(store); m_Hashtable.put(store, new Integer(intIndex)); return intIndex; } } /** * Adds a string value to the list of valid strings for attributes * of type STRING and returns the index of the string. This method is * more efficient than addStringValue(String) for long strings. * * @param src The Attribute containing the string value to add. * @param index the index of the string value in the source attribute. * @return the index assigned to the string, or -1 if the attribute is not * of type Attribute.STRING */ /*@ requires src != null; requires 0 <= index && index < src.m_Values.size(); ensures isString() && 0 <= \result && \result < m_Values.size() || ! isString() && \result == -1; */ public int addStringValue(Attribute src, int index) { if (!isString()) { return -1; } Object store = src.m_Values.elementAt(index); Integer oldIndex = (Integer)m_Hashtable.get(store); if (oldIndex != null) { return oldIndex.intValue(); } else { int intIndex = m_Values.size(); m_Values.addElement(store); m_Hashtable.put(store, new Integer(intIndex)); return intIndex; } } /** * Adds a relation to a relation-valued attribute. * * @param value The value to add * @return the index assigned to the value, or -1 if the attribute is not * of type Attribute.RELATIONAL */ public int addRelation(Instances value) { if (!isRelationValued()) { return -1; } if (!m_Header.equalHeaders(value)) { throw new IllegalArgumentException("Incompatible value for " + "relation-valued attribute."); } Integer index = (Integer)m_Hashtable.get(value); if (index != null) { return index.intValue(); } else { int intIndex = m_Values.size(); m_Values.addElement(value); m_Hashtable.put(value, new Integer(intIndex)); return intIndex; } } /** * Adds an attribute value. Creates a fresh list of attribute * values before adding it. * * @param value the attribute value */ final void addValue(String value) { m_Values = (FastVector)m_Values.copy(); m_Hashtable = (Hashtable)m_Hashtable.clone(); forceAddValue(value); } /** * Produces a shallow copy of this attribute with a new name. * * @param newName the name of the new attribute * @return a copy of this attribute with the same index */ //@ requires newName != null; //@ ensures \result.m_Name == newName; //@ ensures \result.m_Index == m_Index; //@ ensures \result.m_Type == m_Type; public final /*@ pure non_null @*/ Attribute copy(String newName) { Attribute copy = new Attribute(newName); copy.m_Index = m_Index; copy.m_DateFormat = m_DateFormat; copy.m_Type = m_Type; copy.m_Values = m_Values; copy.m_Hashtable = m_Hashtable; copy.m_Header = m_Header; copy.setMetadata(m_Metadata); return copy; } /** * Removes a value of a nominal, string, or relation-valued * attribute. Creates a fresh list of attribute values before * removing it. * * @param index the value's index * @throws IllegalArgumentException if the attribute is not * of the correct type */ //@ requires isNominal() || isString() || isRelationValued(); //@ requires 0 <= index && index < m_Values.size(); final void delete(int index) { if (!isNominal() && !isString() && !isRelationValued()) throw new IllegalArgumentException("Can only remove value of " + "nominal, string or relation-" + " valued attribute!"); else { m_Values = (FastVector)m_Values.copy(); m_Values.removeElementAt(index); if (!isRelationValued()) { Hashtable hash = new Hashtable(m_Hashtable.size()); Enumeration enu = m_Hashtable.keys(); while (enu.hasMoreElements()) { Object string = enu.nextElement(); Integer valIndexObject = (Integer)m_Hashtable.get(string); int valIndex = valIndexObject.intValue(); if (valIndex > index) { hash.put(string, new Integer(valIndex - 1)); } else if (valIndex < index) { hash.put(string, valIndexObject); } } m_Hashtable = hash; } } } /** * Adds an attribute value. * * @param value the attribute value */ //@ requires value != null; //@ ensures m_Values.size() == \old(m_Values.size()) + 1; final void forceAddValue(String value) { 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 -" + " storing uncompressed."); } } m_Values.addElement(store); m_Hashtable.put(store, new Integer(m_Values.size() - 1)); } /** * Sets the index of this attribute. * * @param index the index of this attribute */ //@ requires 0 <= index; //@ assignable m_Index; //@ ensures m_Index == index; final void setIndex(int index) { m_Index = index; } /** * Sets a value of a nominal attribute or string attribute. * Creates a fresh list of attribute values before it is set. * * @param index the value's index * @param string the value * @throws IllegalArgumentException if the attribute is not nominal or * string. */ //@ requires string != null; //@ requires isNominal() || isString(); //@ requires 0 <= index && index < m_Values.size(); final void setValue(int index, String string) { switch (m_Type) { case NOMINAL: case STRING: m_Values = (FastVector)m_Values.copy(); m_Hashtable = (Hashtable)m_Hashtable.clone(); Object store = string; if (string.length() > STRING_COMPRESS_THRESHOLD) { try { store = new SerializedObject(string, true); } catch (Exception ex) { System.err.println("Couldn't compress string attribute value -" + " storing uncompressed."); } } m_Hashtable.remove(m_Values.elementAt(index)); m_Values.setElementAt(store, index); m_Hashtable.put(store, new Integer(index)); break; default: throw new IllegalArgumentException("Can only set values for nominal" + " or string attributes!"); } } /** * Sets a value of a relation-valued attribute. * Creates a fresh list of attribute values before it is set. * * @param index the value's index * @param data the value * @throws IllegalArgumentException if the attribute is not * relation-valued. */ final void setValue(int index, Instances data) { if (isRelationValued()) { if (!data.equalHeaders(m_Header)) { throw new IllegalArgumentException("Can't set relational value. " + "Headers not compatible."); } m_Values = (FastVector)m_Values.copy(); m_Values.setElementAt(data, index); } else { throw new IllegalArgumentException("Can only set value for" + " relation-valued attributes!"); } } /** * Returns the given amount of milliseconds formatted according to the * current Date format. * * @param date the date, represented in milliseconds since * January 1, 1970, 00:00:00 GMT, to return as string * @return the formatted date */ //@ requires isDate(); public /*@pure@*/ String formatDate(double date) { switch (m_Type) { case DATE: return m_DateFormat.format(new Date((long)date)); default: throw new IllegalArgumentException("Can only format date values for date" + " attributes!"); } } /** * Parses the given String as Date, according to the current format and * returns the corresponding amount of milliseconds. * * @param string the date to parse * @return the date in milliseconds since January 1, 1970, 00:00:00 GMT * @throws ParseException if parsing fails */ //@ requires isDate(); //@ requires string != null; public double parseDate(String string) throws ParseException { switch (m_Type) { case DATE: long time = m_DateFormat.parse(string).getTime(); // TODO put in a safety check here if we can't store the value in a double. return (double)time; default: throw new IllegalArgumentException("Can only parse date values for date" + " attributes!"); } } /** * Returns the properties supplied for this attribute. * * @return metadata for this attribute */ public final /*@ pure @*/ ProtectedProperties getMetadata() { return m_Metadata; } /** * Returns the ordering of the attribute. One of the following: * * ORDERING_SYMBOLIC - attribute values should be treated as symbols. * ORDERING_ORDERED - attribute values have a global ordering. * ORDERING_MODULO - attribute values have an ordering which wraps. * * @return the ordering type of the attribute */ public final /*@ pure @*/ int ordering() { return m_Ordering; } /** * Returns whether the attribute values are equally spaced. * * @return whether the attribute is regular or not */ public final /*@ pure @*/ boolean isRegular() { return m_IsRegular; } /** * Returns whether the attribute can be averaged meaningfully. * * @return whether the attribute can be averaged or not */ public final /*@ pure @*/ boolean isAveragable() { return m_IsAveragable; } /** * Returns whether the attribute has a zeropoint and may be * added meaningfully. * * @return whether the attribute has a zeropoint or not */ public final /*@ pure @*/ boolean hasZeropoint() { return m_HasZeropoint; } /** * Returns the attribute's weight. * * @return the attribute's weight as a double */ public final /*@ pure @*/ double weight() { return m_Weight; } /** * Sets the new attribute's weight * * @param value the new weight */ public void setWeight(double value) { Properties props; Enumeration names; String name; m_Weight = value; // generate new metadata object props = new Properties(); names = m_Metadata.propertyNames(); while (names.hasMoreElements()) { name = (String) names.nextElement(); if (!name.equals("weight")) props.setProperty(name, m_Metadata.getProperty(name)); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -