001/** 002 * TextParam -- A parameter that contains a single text string. 003 * 004 * Copyright (C) 2003-2025, 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 February 23, 2025 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 * @return A new instance of this class with the specified name. 049 */ 050 public static TextParam newInstance(CharSequence name) { 051 TextParam o = FACTORY.object(); 052 try { 053 o.setName(name); 054 o._value = Text.EMPTY; 055 } catch (NullPointerException e) { 056 FACTORY.recycle(o); 057 throw e; 058 } 059 return o; 060 } 061 062 private static final ObjectFactory<TextParam> FACTORY = new ObjectFactory<TextParam>() { 063 @Override 064 protected TextParam create() { 065 return new TextParam(); 066 } 067 068 @Override 069 protected void cleanup(TextParam obj) { 070 obj.reset(); 071 } 072 }; 073 074 /** 075 * Resets the internal state of this object to its default values. 076 */ 077 @Override 078 protected void reset() { 079 super.reset(); 080 _value = null; 081 } 082 083 /** 084 * Recycles a parameter instance immediately (on the stack when executing in a 085 * StackContext). 086 * 087 * @param instance The instance of this class to recycle. 088 */ 089 public static void recycle(TextParam instance) { 090 FACTORY.recycle(instance); 091 } 092 093 /** 094 * Do not allow the default constructor to be used except by subclasses. 095 */ 096 private TextParam() { } 097 098 /** 099 * Returns a text parameter with the specified name and text (which may be null). 100 * 101 * @param name The name of the parameter. 102 * @param text The text to be stored with this parameter. 103 * @return A new instance of a text parameter with the specified name and text. 104 */ 105 public static TextParam valueOf(CharSequence name, CharSequence text) { 106 107 TextParam param = TextParam.newInstance(name); 108 param._value = Text.valueOf(text); 109 110 return param; 111 } 112 113 /** 114 * Return the text of this text parameter. Could return an empty string if there is no 115 * text. 116 * 117 * @return The text of this text parameter 118 */ 119 public Text getText() { 120 return _value; 121 } 122 123 /** 124 * Change the text contained in this parameter. The text can not be null. 125 * 126 * @param text The text to be stored in this parameter. 127 */ 128 public void setText(CharSequence text) { 129 if (text == null) 130 throw new NullPointerException(MessageFormat.format( 131 RESOURCES.getString("paramNullErr"),"text")); 132 _value = Text.valueOf(text); 133 } 134 135 /** 136 * Compares the specified object with this parameter for strict equality same text, 137 * same name, and same user data. 138 * 139 * @param obj the object to compare with. 140 * @return <code>true</code> if this parameter is identical to that parameter; 141 * <code>false</code> otherwise. 142 */ 143 @Override 144 public boolean equals(Object obj) { 145 if (this == obj) 146 return true; 147 if ((obj == null) || (obj.getClass() != this.getClass())) 148 return false; 149 150 TextParam that = (TextParam)obj; 151 if (!this._value.equals(that._value)) 152 return false; 153 154 return super.equals(obj); 155 } 156 157 /** 158 * Returns the hash code for this <code>UnitParameter</code>. 159 * 160 * @return the hash code value. 161 */ 162 @Override 163 public int hashCode() { 164 int hash = 7; 165 166 hash = hash * 31 + (_value != null ? _value.hashCode() : 0); 167 hash = hash * 31 + super.hashCode(); 168 169 return hash; 170 } 171 172 /** 173 * Create a Text representation of this parameter which consists of the parameter's 174 * name and text. 175 */ 176 @Override 177 public Text toText() { 178 TextBuilder buffer = TextBuilder.newInstance(); 179 180 buffer.append(getName()); 181 buffer.append(" = "); 182 buffer.append(getText()); 183 184 Text text = buffer.toText(); 185 TextBuilder.recycle(buffer); 186 187 return text; 188 } 189 190 /** 191 * Create a string representation of this parameter which consists of the parameter's 192 * name and text. 193 */ 194 @Override 195 public String toString() { 196 return toText().toString(); 197 } 198}