001/* A base class for graph elements (nodes and edges).
002
003 Copyright (c) 2001-2014 The University of Maryland
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF MARYLAND BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF MARYLAND HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF MARYLAND SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 MARYLAND HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 */
025package ptolemy.graph;
026
027////////////////////////////////////////////////////////////////////////// //
028//Element
029
030/**
031 A base class for graph elements (nodes and edges).
032 A graph element consists of an optional <i>weight</i> (an arbitrary
033 object that is associated with the element).  We say that an element is
034 <i>unweighted</i> if it does not have an assigned weight. It is an error to
035 attempt to access the weight of an unweighted element. Element weights must
036 be non-null objects.
037
038 @author Shuvra S. Bhattacharyya
039 @version $Id$
040 @since Ptolemy II 2.0
041 @Pt.ProposedRating Red (cxh)
042 @Pt.AcceptedRating Red (cxh)
043 @see ptolemy.graph.Edge
044 @see ptolemy.graph.Node
045 */
046public abstract class Element {
047    /** Construct an unweighted element.
048     */
049    public Element() {
050        _weight = null;
051    }
052
053    /** Construct an element with a given weight.
054     *  @param weight The given weight.
055     *  @exception IllegalArgumentException If the specified weight is
056     *  <code>null</code>.
057     */
058    public Element(Object weight) {
059        setWeight(weight);
060    }
061
062    ///////////////////////////////////////////////////////////////////
063    ////                         public methods                    ////
064
065    /** A one-word description of the type of this graph element.
066     *  @return The description.
067     */
068    public String descriptor() {
069        return "element";
070    }
071
072    /** Return the weight that has been associated with this element.
073     *  @return The associated weight.
074     *  @exception IllegalStateException If this is an unweighted element.
075     *  @see #setWeight(Object)
076     */
077    public final Object getWeight() {
078        if (!hasWeight()) {
079            throw new IllegalStateException("Attempt to access the weight "
080                    + "of the following unweighted " + descriptor() + ": "
081                    + this + "\n");
082        } else {
083            return _weight;
084        }
085    }
086
087    /** Return <code>true</code> if and only if this is a weighted element.
088     *  @return True if and only if this is a weighted element.
089     */
090    public final boolean hasWeight() {
091        return _weight != null;
092    }
093
094    /** Make the element unweighted. This method should be used with
095     *  caution since it may make the element incompatible with graphs that
096     *  already contain it. The method has no effect if the element is already
097     *  unweighted.
098     *  @see Graph#validEdgeWeight(Object)
099     *  @see Graph#validNodeWeight(Object)
100     *  @see Graph#validateWeight(Node)
101     */
102    public final void removeWeight() {
103        // FIXME: add @see Graph#validateWeight(Edge)
104        _weight = null;
105    }
106
107    /** Set or change the weight of an element. This method should be used with
108     *  caution since it may make the element incompatible with graphs that
109     *  already contain it.
110     *  @param weight The new weight.
111     *  @exception IllegalArgumentException If the object that is passed as
112     *   argument is null.
113     *  @see Graph#validEdgeWeight(Object)
114     *  @see Graph#validNodeWeight(Object)
115     *  @see Graph#validateWeight(Node)
116     *  @see #getWeight()
117     */
118    public final void setWeight(Object weight) {
119        // FIXME: add @see Graph#validateWeight(Edge)
120        if (weight == null) {
121            throw new IllegalArgumentException(
122                    "Attempt to assign a null " + "weight to the following "
123                            + descriptor() + ": " + this + "\n");
124        } else {
125            _weight = weight;
126        }
127    }
128
129    ///////////////////////////////////////////////////////////////////
130    ////                         protected methods                 ////
131    ///////////////////////////////////////////////////////////////////
132    ////                         protected variables               ////
133
134    /** The weight that is associated with the element if the element is
135     *  weighted. If the element is not weighted,  the value of this
136     *  field is null.
137     */
138    protected Object _weight;
139}