Source code: com/hartmath/expression/HString.java
1 /*
2 * HString.java
3 * Copyright (C) 2000 Klaus Hartlage
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 package com.hartmath.expression;
20
21 import java.lang.*;
22 import java.io.*;
23 import com.hartmath.lib.C;
24 import com.hartmath.lib.Session;
25
26 /**
27 * The HString class represents character strings. This class adopts much of
28 * its functionality from the <code>java.lang.String</code> class
29 *
30 *@see HSymbol
31 */
32 public class HString extends HExpression {
33 /**
34 * Description of the Field
35 */
36 protected String str;
37
38
39 /**
40 * Constructs a new empty HString.
41 */
42 public HString() {
43 this.str = new String();
44 }
45
46
47 /**
48 * Constructs a new HString that is a copy of the specified String.
49 *
50 *@param value the initial value of the HString
51 */
52 public HString(String value) {
53 this.str = new String(value);
54 }
55
56
57 /**
58 * Constructs a new HString whose initial value is the specified array of
59 * characters.
60 *
61 *@param value the initial value of the HString
62 */
63 public HString(char value[]) {
64 this.str = new String(value);
65 }
66
67
68 /**
69 * Constructs a new HString whose initial value is the specified sub array of
70 * characters. The length of the new string will be count characters starting
71 * at offset within the specified character array.
72 *
73 *@param value the initial value of the HString, an array of characters
74 *@param offset the offset into the value of the HString
75 *@param count the length of the value of the HString
76 */
77 public HString(char value[], int offset, int count) {
78 this.str = new String(value, offset, count);
79 }
80
81
82 public HString(StringBuffer buffer) {
83 this.str = new String(buffer);
84 }
85
86 /**
87 * Method Declaration.
88 *
89 *@param obj
90 *@return
91 *@see
92 */
93 public boolean isMember(HObject obj) {
94 return equals(obj);
95 }
96
97
98 /**
99 * Method Declaration.
100 *
101 *@param sym
102 *@return
103 *@see
104 */
105 public boolean isHeadMember(HSymbol sym) {
106 return head().equals(sym);
107 }
108
109 /**
110 * Transform this in a <code>String</code> object
111 *
112 *@return the internal <code>String</code> object
113 */
114 public String toString() {
115 return str;
116 }
117
118 /**
119 * Returns a hash code value for the object. This method is
120 * supported for the benefit of hashtables such as those provided by
121 * <code>java.util.Hashtable</code>.
122 * <p>
123 *
124 * @return a hash code value for this object.
125 * @see java.lang.Object#equals(java.lang.Object)
126 * @see java.util.Hashtable
127 */
128 public int hashCode() {
129 return (str.hashCode() & 0x55555555);
130 }
131
132
133 /**
134 * Method Declaration.
135 *
136 *@param obj
137 *@return
138 *@see
139 */
140 public boolean equals(Object obj) {
141 if (obj instanceof HString) {
142 return str.equals(((HString) obj).str);
143 }
144
145 return false;
146 }
147
148
149 /**
150 * Method Declaration.
151 *
152 *@param obj
153 *@return
154 *@see
155 */
156 public int compareTo(HString obj) {
157 return str.compareTo(obj.str);
158 }
159
160
161 /**
162 * Method Declaration.
163 *
164 *@param obj
165 *@param session Description of Parameter
166 *@return
167 *@see
168 */
169 public boolean less(Session session, Object obj) {
170 if (obj instanceof HString) {
171 HString temp = (HString) obj;
172
173 return (str.compareTo(temp.str) < 0);
174 }
175 if (obj instanceof HObject) {
176 return (hierarchy() < ((HObject) obj).hierarchy());
177 }
178
179 return false;
180 }
181
182
183 /**
184 * Method Declaration.
185 *
186 *@param obj
187 *@param session Description of Parameter
188 *@return
189 *@see
190 */
191 public boolean greater(Session session, Object obj) {
192 if (obj instanceof HObject) {
193 return ((HObject) obj).less(session, this);
194 }
195
196 return false;
197 }
198
199
200 /**
201 * Returns an specific integer value for each class in the math-objects
202 * hierarchy
203 *
204 *@return Description of the Returned Value
205 */
206 public int hierarchy() {
207 return HObject.STRINGID;
208 }
209
210
211 /**
212 * Method Declaration.
213 *
214 *@return Description of the Returned Value
215 *@see
216 */
217 /*
218 * public void writeTo(DataOutputStream os) throws IOException {
219 * os.writeByte(HObject.STRINGFILEID);
220 * os.writeUTF(str);
221 * }
222 */
223 public HSymbol head() {
224 return C.String;
225 }
226
227 }
228