001/**
002 * TextParam -- A parameter that contains a single text string.
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 Library 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 General Public License along with this
015 * program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite
016 * 330, Boston, MA 02111-1307, USA. Or visit: http://www.gnu.org/licenses/gpl.html
017 */
018package jahuwaldt.js.datareader;
019
020import java.text.MessageFormat;
021import javolution.context.ObjectFactory;
022import javolution.text.Text;
023import javolution.text.TextBuilder;
024
025/**
026 * A class that represents a text/note parameter.
027 *
028 * <p> Modified by: Joseph A. Huwaldt </p>
029 *
030 * @author Joseph A. Huwaldt, Date: March 5, 2003
031 * @version October 15, 2015
032 */
033public final class TextParam extends DataParam {
034
035    //  The text held by this parameter.
036    private Text _value;
037
038    //////////////////////
039    // Factory Creation //
040    //////////////////////
041    /**
042     * Returns a new, preallocated or recycled <code>TextParam</code> instance (on the
043     * stack when executing in a <code>StackContext</code>) with the specified name and
044     * containing no text.
045     *
046     * @param name The name to be assigned to this parameter (may not be
047     *             <code>null</code>).
048     */
049    public static TextParam newInstance(CharSequence name) {
050        TextParam o = FACTORY.object();
051        try {
052            o.setName(name);
053            o._value = Text.EMPTY;
054        } catch (NullPointerException e) {
055            FACTORY.recycle(o);
056            throw e;
057        }
058        return o;
059    }
060
061    private static final ObjectFactory<TextParam> FACTORY = new ObjectFactory<TextParam>() {
062        @Override
063        protected TextParam create() {
064            return new TextParam();
065        }
066
067        @Override
068        protected void cleanup(TextParam obj) {
069            obj.reset();
070        }
071    };
072
073    /**
074     * Resets the internal state of this object to its default values.
075     */
076    @Override
077    protected void reset() {
078        super.reset();
079        _value = null;
080    }
081
082    /**
083     * Recycles a parameter instance immediately (on the stack when executing in a
084     * StackContext).
085     */
086    public static void recycle(TextParam instance) {
087        FACTORY.recycle(instance);
088    }
089
090    /**
091     * Do not allow the default constructor to be used except by subclasses.
092     */
093    private TextParam() { }
094
095    /**
096     * Returns a text parameter with the specified name and text (which may be null).
097     */
098    public static TextParam valueOf(CharSequence name, CharSequence text) {
099
100        TextParam param = TextParam.newInstance(name);
101        param._value = Text.valueOf(text);
102
103        return param;
104    }
105
106    /**
107     * Return the text of this text parameter. Could return an empty string if there is no
108     * text.
109     */
110    public Text getText() {
111        return _value;
112    }
113
114    /**
115     * Change the text contained in this parameter. The text can not be null.
116     */
117    public void setText(CharSequence text) {
118        if (text == null)
119            throw new NullPointerException(MessageFormat.format(
120                    RESOURCES.getString("paramNullErr"),"text"));
121        _value = Text.valueOf(text);
122    }
123
124    /**
125     * Compares the specified object with this parameter for strict equality same text,
126     * same name, and same user data.
127     *
128     * @param obj the object to compare with.
129     * @return <code>true</code> if this parameter is identical to that parameter;
130     *         <code>false</code> otherwise.
131     */
132    @Override
133    public boolean equals(Object obj) {
134        if (this == obj)
135            return true;
136        if ((obj == null) || (obj.getClass() != this.getClass()))
137            return false;
138
139        TextParam that = (TextParam)obj;
140        if (!this._value.equals(that._value))
141            return false;
142
143        return super.equals(obj);
144    }
145
146    /**
147     * Returns the hash code for this <code>UnitParameter</code>.
148     *
149     * @return the hash code value.
150     */
151    @Override
152    public int hashCode() {
153        int hash = 7;
154
155        hash = hash * 31 + (_value != null ? _value.hashCode() : 0);
156        hash = hash * 31 + super.hashCode();
157
158        return hash;
159    }
160
161    /**
162     * Create a Text representation of this parameter which consists of the parameter's
163     * name and text.
164     */
165    @Override
166    public Text toText() {
167        TextBuilder buffer = TextBuilder.newInstance();
168
169        buffer.append(getName());
170        buffer.append(" = ");
171        buffer.append(getText());
172
173        Text text = buffer.toText();
174        TextBuilder.recycle(buffer);
175
176        return text;
177    }
178
179    /**
180     * Create a string representation of this parameter which consists of the parameter's
181     * name and text.
182     */
183    @Override
184    public String toString() {
185        return toText().toString();
186    }
187}