📄 element.java
字号:
/* * project: RebecaSim * package: graph * file: Element.java * version: 0.1 * date: 01.04.2005 * * This software is part of the diploma thesis "Ein adaptives Brokernetz * für Publish/Subscribe Systeme". */package graph;import util.*;/** * The <code>Element</code> class is the superclass of many * classes in the <code>graph</code> package, since nearly everything * is an element. * The implementation ensures that each element has an unique ID and * provides the functionality to easily attach arbitary properties mapped * to belonging values. * * @version 0.1 01.04.2005 * @author Helge Parzyjegla */public class Element implements Comparable{ /** The unique ID of this element. */ public final ID id; /** A hashtable to store property to value mappings. */ protected SimpleHashtable properties; /** * Constructs a new element with an unique ID and without any * attached properties. */ public Element() { this.id = new ID(); this.properties = new SimpleHashtable(); } /** * Compares this element to the specified object. * The result is <code>true</code> if and only if the object * <code>o<code> is not <code>null</code> and is also an element * with the same id. * * @param o the object to compare to. * @return <code>true</code> if the specified object is not * <code>null</code> and is also an element with the same id. * @throws NullPointerException if <code>o</code> is <code>null</code>. */ public boolean equals(Object o) { if (o instanceof Element) { return this.id.equals(((Element)o).id); } return false; } /** * Returns a hash code for this <code>element</code> based on its id. * * @return a hash code value for this <code>element</code>, equal to * its <code>ID</code>s hash code. */ public int hashCode() { return id.hashCode(); } /** * Attaches a <code>property</code> to <code>value</code> mapping * to this element. * The property can not be <code>null<code>. <p> * * Use the <code>getProperty</code> method with an equal key * to retrieve the mapped value again. * * @param property the property. * @param value the belonging value. * @return the previous value of the specified property or * <code>null</code> if the property was not attached before. * @throws NullPointerException if property is <code>null<code>. * @see #getProperty(Property) */ public synchronized Object setProperty(Property property, Object value) { if (property == null) { throw new NullPointerException("Null is not a valid property."); } property.addElement(this); return properties.put(property.getKey(), value); } /** * Retrives the value the attached property is mapped to. * * @param property an attached property. * @return the value to which the property is mapped or <code>null</code> * if the property is not attached to this element. * @throws NullPointerException if property is <code>null<code>. * @see #setProperty(Property) */ public synchronized Object getProperty(Property property){ if (property == null) { throw new NullPointerException("Null is not a valid property."); } return properties.get(property.getKey()); } /** * Tests whether the element has a value mapped by that property. * @param property the property. * @return true if a value is attached for this property. */ public synchronized boolean hasProperty(Property property){ if (property == null) { throw new NullPointerException("Null is not a valid property."); } return properties.containsKey(property.getKey()); } /** * Removes the property (and its corresponding value) from this element. * This method does nothing if the property is not attached to this * element. * * @param property an attached property. * @return the value to which the property was mapped or <code>null</code> * if the property is not attached to this element. * @throws NullPointerException if property is <code>null<code>. */ public synchronized Object removeProperty(Property property){ if (property == null) { throw new NullPointerException("Null is not a valid property."); } property.removeElement(this); return properties.remove(property.getKey()); } /** * Compares this <code>Element</code> object to another object. * The elements ordering is inherited from its ids. * * @param o the object to compare to. * @return <code>0</code> if both elements are equal, * a value less than <code>0</code> if this element is less * than the argument and * a value greater than <code>0</code> if this element is greater * than the argument. * @throws NullPointerException if the argument is <code>null</code>. * @throws ClassCastException if the argument is not an * <code>Element</code>. */ public int compareTo(Object o){ return id.compareTo(((Element)o).id); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -