2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / text / ChoiceFormat.java
blobf7bdde1e294401509eb8517b51d6f9a05b8801a1
1 /* ChoiceFormat.java -- Format over a range of numbers
2 Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package java.text;
41 import java.util.Vector;
43 /**
44 * This class allows a format to be specified based on a range of numbers.
45 * To use this class, first specify two lists of formats and range terminators.
46 * These lists must be arrays of equal length. The format of index
47 * <code>i</code> will be selected for value <code>X</code> if
48 * <code>terminator[i] <= X < limit[i + 1]</code>. If the value X is not
49 * included in any range, then either the first or last format will be
50 * used depending on whether the value X falls outside the range.
51 * <p>
52 * This sounds complicated, but that is because I did a poor job of
53 * explaining it. Consider the following example:
54 * <p>
56 <pre>terminators = { 1, ChoiceFormat.nextDouble(1) }
57 formats = { "file", "files" }</pre>
59 * <p>
60 * In this case if the actual number tested is one or less, then the word
61 * "file" is used as the format value. If the number tested is greater than
62 * one, then "files" is used. This allows plurals to be handled
63 * gracefully. Note the use of the method <code>nextDouble</code>. This
64 * method selects the next highest double number than its argument. This
65 * effectively makes any double greater than 1.0 cause the "files" string
66 * to be selected. (Note that all terminator values are specified as
67 * doubles.
68 * <p>
69 * Note that in order for this class to work properly, the range terminator
70 * array must be sorted in ascending order and the format string array
71 * must be the same length as the terminator array.
73 * @author Tom Tromey <tromey@cygnus.com>
74 * @author Aaron M. Renn (arenn@urbanophile.com)
75 * @date March 9, 1999
77 /* Written using "Java Class Libraries", 2nd edition, plus online
78 * API docs for JDK 1.2 from http://www.javasoft.com.
79 * Status: Believed complete and correct to 1.1.
81 public class ChoiceFormat extends NumberFormat
83 /**
84 * This method sets new range terminators and format strings for this
85 * object based on the specified pattern. This pattern is of the form
86 * "term#string|term#string...". For example "1#Sunday|2#Monday|#Tuesday".
88 * @param pattern The pattern of terminators and format strings.
90 * @exception IllegalArgumentException If the pattern is not valid
92 public void applyPattern (String newPattern)
94 // Note: we assume the same kind of quoting rules apply here.
95 // This isn't explicitly documented. But for instance we accept
96 // '#' as a literal hash in a format string.
97 int index = 0, max = newPattern.length();
98 Vector stringVec = new Vector ();
99 Vector limitVec = new Vector ();
100 StringBuffer buf = new StringBuffer ();
102 while (true)
104 // Find end of double.
105 int dstart = index;
106 while (index < max)
108 char c = newPattern.charAt(index);
109 if (c == '#' || c == '\u2064' || c == '<')
110 break;
111 ++index;
114 if (index == max)
115 throw new IllegalArgumentException ("unexpected end of text");
116 Double d = new Double (newPattern.substring(dstart, index));
118 if (newPattern.charAt(index) == '<')
119 d = new Double (nextDouble (d.doubleValue()));
121 limitVec.addElement(d);
123 // Scan text.
124 ++index;
125 buf.setLength(0);
126 while (index < max)
128 char c = newPattern.charAt(index);
129 if (c == '\'' && index < max + 1
130 && newPattern.charAt(index + 1) == '\'')
132 buf.append(c);
133 ++index;
135 else if (c == '\'' && index < max + 2)
137 buf.append(newPattern.charAt(index + 1));
138 index += 2;
140 else if (c == '|')
141 break;
142 else
143 buf.append(c);
144 ++index;
147 stringVec.addElement(buf.toString());
148 if (index == max)
149 break;
150 ++index;
153 choiceFormats = new String[stringVec.size()];
154 stringVec.copyInto(choiceFormats);
156 choiceLimits = new double[limitVec.size()];
157 for (int i = 0; i < choiceLimits.length; ++i)
159 Double d = (Double) limitVec.elementAt(i);
160 choiceLimits[i] = d.doubleValue();
165 * This method initializes a new instance of <code>ChoiceFormat</code> that
166 * generates its range terminator and format string arrays from the
167 * specified pattern. This pattern is of the form
168 * "term#string|term#string...". For example "1#Sunday|2#Monday|#Tuesday".
169 * This is the same pattern type used by the <code>applyPattern</code>
170 * method.
172 * @param pattern The pattern of terminators and format strings.
174 * @exception IllegalArgumentException If the pattern is not valid
176 public ChoiceFormat (String newPattern)
178 super ();
179 applyPattern (newPattern);
183 * This method initializes a new instance of <code>ChoiceFormat</code> that
184 * will use the specified range terminators and format strings.
186 * @param choiceLimits The array of range terminators
187 * @param choiceFormats The array of format strings
189 public ChoiceFormat (double[] choiceLimits, String[] choiceFormats)
191 super ();
192 setChoices (choiceLimits, choiceFormats);
196 * This method tests this object for equality with the specified
197 * object. This will be true if and only if:
198 * <ul>
199 * <li>The specified object is not <code>null</code>.
200 * <li>The specified object is an instance of <code>ChoiceFormat</code>.
201 * <li>The termination ranges and format strings are identical to
202 * this object's.
203 * </ul>
205 * @param obj The object to test for equality against.
207 * @return <code>true</code> if the specified object is equal to
208 * this one, <code>false</code> otherwise.
210 public boolean equals (Object obj)
212 if (! (obj instanceof ChoiceFormat))
213 return false;
214 ChoiceFormat cf = (ChoiceFormat) obj;
215 if (choiceLimits.length != cf.choiceLimits.length)
216 return false;
217 for (int i = choiceLimits.length - 1; i >= 0; --i)
219 if (choiceLimits[i] != cf.choiceLimits[i]
220 || !choiceFormats[i].equals(cf.choiceFormats[i]))
221 return false;
223 return true;
227 * This method appends the appropriate format string to the specified
228 * <code>StringBuffer</code> based on the supplied <code>long</code>
229 * argument.
231 * @param number The number used for determine (based on the range
232 * terminators) which format string to append.
233 * @param sb The <code>StringBuffer</code> to append the format string to.
234 * @param status Unused.
236 * @return The <code>StringBuffer</code> with the format string appended.
238 public StringBuffer format (long num, StringBuffer appendBuf,
239 FieldPosition pos)
241 return format ((double) num, appendBuf, pos);
245 * This method appends the appropriate format string to the specified
246 * <code>StringBuffer</code> based on the supplied <code>double</code>
247 * argument.
249 * @param number The number used for determine (based on the range
250 * terminators) which format string to append.
251 * @param sb The <code>StringBuffer</code> to append the format string to.
252 * @param status Unused.
254 * @return The <code>StringBuffer</code> with the format string appended.
256 public StringBuffer format (double num, StringBuffer appendBuf,
257 FieldPosition pos)
259 if (choiceLimits.length == 0)
260 return appendBuf;
262 int index = 0;
263 if (! Double.isNaN(num) && num >= choiceLimits[0])
265 for (; index < choiceLimits.length - 1; ++index)
267 if (choiceLimits[index] <= num && num < choiceLimits[index + 1])
268 break;
272 return appendBuf.append(choiceFormats[index]);
276 * This method returns the list of format strings in use.
278 * @return The list of format objects.
280 public Object[] getFormats ()
282 return (Object[]) choiceFormats.clone();
286 * This method returns the list of range terminators in use.
288 * @return The list of range terminators.
290 public double[] getLimits ()
292 return (double[]) choiceLimits.clone();
296 * This method returns a hash value for this object
298 * @return A hash value for this object.
300 public int hashCode ()
302 int hash = 0;
303 for (int i = 0; i < choiceLimits.length; ++i)
305 long v = Double.doubleToLongBits(choiceLimits[i]);
306 hash ^= (v ^ (v >>> 32));
307 hash ^= choiceFormats[i].hashCode();
309 return hash;
313 * This method returns the lowest possible double greater than the
314 * specified double. If the specified double value is equal to
315 * <code>Double.NaN</code> then that is the value returned.
317 * @param d The specified double
319 * @return The lowest double value greater than the specified double.
321 public static final double nextDouble (double d)
323 return nextDouble (d, true);
327 * This method returns a double that is either the next highest double
328 * or next lowest double compared to the specified double depending on the
329 * value of the passed boolean parameter. If the boolean parameter is
330 * <code>true</code>, then the lowest possible double greater than the
331 * specified double will be returned. Otherwise the highest possible
332 * double less than the specified double will be returned.
334 * @param d The specified double
335 * @param positive <code>true</code> to return the next highest
336 * double, <code>false</code> otherwise.
338 * @return The next highest or lowest double value.
340 public static double nextDouble (double d, boolean next)
342 if (Double.isInfinite(d) || Double.isNaN(d))
343 return d;
345 long bits = Double.doubleToLongBits(d);
347 long mantMask = (1L << mantissaBits) - 1;
348 long mantissa = bits & mantMask;
350 long expMask = (1L << exponentBits) - 1;
351 long exponent = (bits >>> mantissaBits) & expMask;
353 if (next ^ (bits < 0)) // Increment magnitude
355 if (mantissa == (1L << mantissaBits) - 1)
357 mantissa = 0L;
358 exponent++;
360 // Check for absolute overflow.
361 if (exponent >= (1L << mantissaBits))
362 return (bits > 0) ? Double.POSITIVE_INFINITY
363 : Double.NEGATIVE_INFINITY;
365 else
366 mantissa++;
368 else // Decrement magnitude
370 if (exponent == 0L && mantissa == 0L)
372 // The only case where there is a change of sign
373 return next ? Double.MIN_VALUE : -Double.MIN_VALUE;
375 else
377 if (mantissa == 0L)
379 mantissa = (1L << mantissaBits) - 1;
380 exponent--;
382 else
383 mantissa--;
387 long result = bits < 0 ? 1 : 0;
388 result = (result << exponentBits) | exponent;
389 result = (result << mantissaBits) | mantissa;
390 return Double.longBitsToDouble(result);
394 * I'm not sure what this method is really supposed to do, as it is
395 * not documented.
397 public Number parse (String sourceStr, ParsePosition pos)
399 int index = pos.getIndex();
400 for (int i = 0; i < choiceLimits.length; ++i)
402 if (sourceStr.startsWith(choiceFormats[i], index))
404 pos.setIndex(index + choiceFormats[i].length());
405 return new Double (choiceLimits[i]);
408 pos.setErrorIndex(index);
409 return new Double (Double.NaN);
413 * This method returns the highest possible double less than the
414 * specified double. If the specified double value is equal to
415 * <code>Double.NaN</code> then that is the value returned.
417 * @param d The specified double
419 * @return The highest double value less than the specified double.
421 public static final double previousDouble (double d)
423 return nextDouble (d, false);
427 * This method sets new range terminators and format strings for this
428 * object.
430 * @param choiceLimits The new range terminators
431 * @param choiceFormats The new choice formats
433 public void setChoices (double[] choiceLimits, String[] choiceFormats)
435 if (choiceLimits == null || choiceFormats == null)
436 throw new NullPointerException ();
437 if (choiceLimits.length != choiceFormats.length)
438 throw new IllegalArgumentException ();
439 this.choiceFormats = (String[]) choiceFormats.clone();
440 this.choiceLimits = (double[]) choiceLimits.clone();
443 private final void quoteString (StringBuffer dest, String text)
445 int max = text.length();
446 for (int i = 0; i < max; ++i)
448 char c = text.charAt(i);
449 if (c == '\'')
451 dest.append(c);
452 dest.append(c);
454 else if (c == '#' || c == '|' || c == '\u2064' || c == '<')
456 dest.append('\'');
457 dest.append(c);
458 dest.append('\'');
460 else
461 dest.append(c);
466 * This method returns the range terminator list and format string list
467 * as a <code>String</code> suitable for using with the
468 * <code>applyPattern</code> method.
470 * @return A pattern string for this object
472 public String toPattern ()
474 StringBuffer result = new StringBuffer ();
475 for (int i = 0; i < choiceLimits.length; ++i)
477 result.append(choiceLimits[i]);
478 result.append('#');
479 quoteString (result, choiceFormats[i]);
481 return result.toString();
485 * This is the list of format strings. Note that this variable is
486 * specified by the serialization spec of this class.
488 private String[] choiceFormats;
491 * This is the list of range terminator values. Note that this variable is
492 * specified by the serialization spec of this class.
494 private double[] choiceLimits;
496 // Number of mantissa bits in double.
497 private static final int mantissaBits = 52;
498 // Number of exponent bits in a double.
499 private static final int exponentBits = 11;
501 private static final long serialVersionUID = 1795184449645032964L;