001/**
002 * DataParam -- A data parameter or variable in a case.
003 *
004 * Copyright (C) 2003-2015, by Joseph A. Huwaldt. All rights reserved.
005 *
006 * This library is free software; you can redistribute it and/or modify it under the terms
007 * of the GNU Lesser General Public License as published by the Free Software Foundation;
008 * either version 2 of the License, or (at your option) any later version.
009 *
010 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
011 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
012 * PARTICULAR PURPOSE. See the GNU Library General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public License along with
015 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place -
016 * Suite 330, Boston, MA 02111-1307, USA. Or visit: http://www.gnu.org/licenses/lgpl.html
017 */
018package jahuwaldt.js.datareader;
019
020import java.text.MessageFormat;
021import javolution.text.Text;
022
023/**
024 * A data parameter or variable in a {@link DataCase case} or run.
025 *
026 * <p> Modified by: Joseph A. Huwaldt </p>
027 *
028 * @author Joseph A. Huwaldt, Date: March 5, 2003
029 * @version October 15, 2015
030 */
031public abstract class DataParam implements DataElement {
032
033    //  The name of the parameter.
034    private CharSequence _name;
035
036    //  Reference data for this element.
037    private Object _userData;
038
039    /**
040     * Do not allow the default constructor to be used except by subclasses.
041     */
042    protected DataParam() { }
043
044    /**
045     * Return the name of this parameter.
046     */
047    @Override
048    public CharSequence getName() {
049        return _name;
050    }
051
052    /**
053     * Change the name of this parameter to the specified name (may <em>not</em> be
054     * <code>null</code>).
055     */
056    @Override
057    public void setName(CharSequence name) {
058        if (name == null)
059            throw new NullPointerException(MessageFormat.format(
060                    RESOURCES.getString("paramNullErr"),"name"));
061        _name = name;
062    }
063
064    /**
065     * Return any user defined object associated with this data element. If there is no
066     * user data, then <code>null</code> is returned.
067     */
068    @Override
069    public Object getUserObject() {
070        return _userData;
071    }
072
073    /**
074     * Set the user defined object associated with this data element. This can be used to
075     * store any type of information with a data element that could be useful. Storing
076     * <code>null</code> for no user object is fine.
077     */
078    @Override
079    public void setUserObject(Object data) {
080        _userData = data;
081    }
082
083    /**
084     * Compares the specified object with this parameter for strict equality, same value,
085     * same name, same user data.
086     *
087     * @param obj the object to compare with.
088     * @return <code>true</code> if this parameter is identical to that parameter;
089     *         <code>false</code> otherwise.
090     */
091    @Override
092    public boolean equals(Object obj) {
093        if (this == obj)
094            return true;
095        if ((obj == null) || (obj.getClass() != this.getClass()))
096            return false;
097
098        DataParam that = (DataParam)obj;
099        if (!this._name.equals(that._name))
100            return false;
101        if (this._userData == null) {
102            if (that._userData != null)
103                return false;
104        } else if (!this._userData.equals(that._userData))
105            return false;
106
107        return true;
108    }
109
110    /**
111     * Returns the hash code for this <code>DataParam</code>.
112     *
113     * @return the hash code value.
114     */
115    @Override
116    public int hashCode() {
117        int hash = 7;
118
119        hash = hash * 31 + _name.hashCode();
120        hash = hash * 31 + (_userData != null ? _userData.hashCode() : 0);
121
122        return hash;
123    }
124
125    /**
126     * Resets the internal state of this object to its default values.
127     */
128    protected void reset() {
129        _name = null;
130        _userData = null;
131    }
132
133    /**
134     * Create a Text representation of this parameter which simply consists of the
135     * parameter's name.
136     */
137    public Text toText() {
138        return Text.valueOf(getName());
139    }
140
141    /**
142     * Create a string representation of this parameter which simply consists of the
143     * parameter's name.
144     */
145    @Override
146    public String toString() {
147        return getName().toString();
148    }
149
150    /*
151     *  Compares this data element with the specified element for order (where
152     *  order is determined by the element's name).
153     *  Returns a negative integer, zero, or a positive integer as this
154     *  object is less than, equal to, or greater than the specified object.
155     *  This method delegates to the Text.compareTo() method using
156     *  this object's name.
157     *
158     *  @param otherElement  The data element this one is being compared to.
159     *  @throw ClassCastException if the specified object's type prevents
160     *         it from being compared to this Object.
161     */
162    @Override
163    public int compareTo(DataElement otherElement) {
164        Text thisName = Text.valueOf(getName());
165        Text otherName = Text.valueOf(otherElement.getName());
166        return thisName.compareTo(otherName);
167    }
168}