| 1 | /* ChoiceFormat.java -- Format over a range of numbers
|
|---|
| 2 | Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
|---|
| 3 |
|
|---|
| 4 | This file is part of GNU Classpath.
|
|---|
| 5 |
|
|---|
| 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.
|
|---|
| 10 |
|
|---|
| 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.
|
|---|
| 15 |
|
|---|
| 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.
|
|---|
| 20 |
|
|---|
| 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.
|
|---|
| 25 |
|
|---|
| 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. */
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | package java.text;
|
|---|
| 40 |
|
|---|
| 41 | import java.util.Vector;
|
|---|
| 42 |
|
|---|
| 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>
|
|---|
| 55 | *
|
|---|
| 56 | <pre>terminators = { 1, ChoiceFormat.nextDouble(1) }
|
|---|
| 57 | formats = { "file", "files" }</pre>
|
|---|
| 58 | *
|
|---|
| 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.
|
|---|
| 72 | *
|
|---|
| 73 | * @author Tom Tromey <[email protected]>
|
|---|
| 74 | * @author Aaron M. Renn ([email protected])
|
|---|
| 75 | * @date March 9, 1999
|
|---|
| 76 | */
|
|---|
| 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.
|
|---|
| 80 | */
|
|---|
| 81 | public class ChoiceFormat extends NumberFormat
|
|---|
| 82 | {
|
|---|
| 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".
|
|---|
| 87 | *
|
|---|
| 88 | * @param pattern The pattern of terminators and format strings.
|
|---|
| 89 | *
|
|---|
| 90 | * @exception IllegalArgumentException If the pattern is not valid
|
|---|
| 91 | */
|
|---|
| 92 | public void applyPattern (String newPattern)
|
|---|
| 93 | {
|
|---|
| 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 ();
|
|---|
| 101 |
|
|---|
| 102 | while (true)
|
|---|
| 103 | {
|
|---|
| 104 | // Find end of double.
|
|---|
| 105 | int dstart = index;
|
|---|
| 106 | while (index < max)
|
|---|
| 107 | {
|
|---|
| 108 | char c = newPattern.charAt(index);
|
|---|
| 109 | if (c == '#' || c == '\u2064' || c == '<')
|
|---|
| 110 | break;
|
|---|
| 111 | ++index;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | if (index == max)
|
|---|
| 115 | throw new IllegalArgumentException ("unexpected end of text");
|
|---|
| 116 | Double d = new Double (newPattern.substring(dstart, index));
|
|---|
| 117 |
|
|---|
| 118 | if (newPattern.charAt(index) == '<')
|
|---|
| 119 | d = new Double (nextDouble (d.doubleValue()));
|
|---|
| 120 |
|
|---|
| 121 | limitVec.addElement(d);
|
|---|
| 122 |
|
|---|
| 123 | // Scan text.
|
|---|
| 124 | ++index;
|
|---|
| 125 | buf.setLength(0);
|
|---|
| 126 | while (index < max)
|
|---|
| 127 | {
|
|---|
| 128 | char c = newPattern.charAt(index);
|
|---|
| 129 | if (c == '\'' && index < max + 1
|
|---|
| 130 | && newPattern.charAt(index + 1) == '\'')
|
|---|
| 131 | {
|
|---|
| 132 | buf.append(c);
|
|---|
| 133 | ++index;
|
|---|
| 134 | }
|
|---|
| 135 | else if (c == '\'' && index < max + 2)
|
|---|
| 136 | {
|
|---|
| 137 | buf.append(newPattern.charAt(index + 1));
|
|---|
| 138 | index += 2;
|
|---|
| 139 | }
|
|---|
| 140 | else if (c == '|')
|
|---|
| 141 | break;
|
|---|
| 142 | else
|
|---|
| 143 | buf.append(c);
|
|---|
| 144 | ++index;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | stringVec.addElement(buf.toString());
|
|---|
| 148 | if (index == max)
|
|---|
| 149 | break;
|
|---|
| 150 | ++index;
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | choiceFormats = new String[stringVec.size()];
|
|---|
| 154 | stringVec.copyInto(choiceFormats);
|
|---|
| 155 |
|
|---|
| 156 | choiceLimits = new double[limitVec.size()];
|
|---|
| 157 | for (int i = 0; i < choiceLimits.length; ++i)
|
|---|
| 158 | {
|
|---|
| 159 | Double d = (Double) limitVec.elementAt(i);
|
|---|
| 160 | choiceLimits[i] = d.doubleValue();
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | /**
|
|---|
| 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.
|
|---|
| 171 | *
|
|---|
| 172 | * @param pattern The pattern of terminators and format strings.
|
|---|
| 173 | *
|
|---|
| 174 | * @exception IllegalArgumentException If the pattern is not valid
|
|---|
| 175 | */
|
|---|
| 176 | public ChoiceFormat (String newPattern)
|
|---|
| 177 | {
|
|---|
| 178 | super ();
|
|---|
| 179 | applyPattern (newPattern);
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /**
|
|---|
| 183 | * This method initializes a new instance of <code>ChoiceFormat</code> that
|
|---|
| 184 | * will use the specified range terminators and format strings.
|
|---|
| 185 | *
|
|---|
| 186 | * @param choiceLimits The array of range terminators
|
|---|
| 187 | * @param choiceFormats The array of format strings
|
|---|
| 188 | */
|
|---|
| 189 | public ChoiceFormat (double[] choiceLimits, String[] choiceFormats)
|
|---|
| 190 | {
|
|---|
| 191 | super ();
|
|---|
| 192 | setChoices (choiceLimits, choiceFormats);
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /**
|
|---|
| 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>
|
|---|
| 204 | *
|
|---|
| 205 | * @param obj The object to test for equality against.
|
|---|
| 206 | *
|
|---|
| 207 | * @return <code>true</code> if the specified object is equal to
|
|---|
| 208 | * this one, <code>false</code> otherwise.
|
|---|
| 209 | */
|
|---|
| 210 | public boolean equals (Object obj)
|
|---|
| 211 | {
|
|---|
| 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)
|
|---|
| 218 | {
|
|---|
| 219 | if (choiceLimits[i] != cf.choiceLimits[i]
|
|---|
| 220 | || !choiceFormats[i].equals(cf.choiceFormats[i]))
|
|---|
| 221 | return false;
|
|---|
| 222 | }
|
|---|
| 223 | return true;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | /**
|
|---|
| 227 | * This method appends the appropriate format string to the specified
|
|---|
| 228 | * <code>StringBuffer</code> based on the supplied <code>long</code>
|
|---|
| 229 | * argument.
|
|---|
| 230 | *
|
|---|
| 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.
|
|---|
| 235 | *
|
|---|
| 236 | * @return The <code>StringBuffer</code> with the format string appended.
|
|---|
| 237 | */
|
|---|
| 238 | public StringBuffer format (long num, StringBuffer appendBuf,
|
|---|
| 239 | FieldPosition pos)
|
|---|
| 240 | {
|
|---|
| 241 | return format ((double) num, appendBuf, pos);
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | /**
|
|---|
| 245 | * This method appends the appropriate format string to the specified
|
|---|
| 246 | * <code>StringBuffer</code> based on the supplied <code>double</code>
|
|---|
| 247 | * argument.
|
|---|
| 248 | *
|
|---|
| 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.
|
|---|
| 253 | *
|
|---|
| 254 | * @return The <code>StringBuffer</code> with the format string appended.
|
|---|
| 255 | */
|
|---|
| 256 | public StringBuffer format (double num, StringBuffer appendBuf,
|
|---|
| 257 | FieldPosition pos)
|
|---|
| 258 | {
|
|---|
| 259 | if (choiceLimits.length == 0)
|
|---|
| 260 | return appendBuf;
|
|---|
| 261 |
|
|---|
| 262 | int index = 0;
|
|---|
| 263 | if (! Double.isNaN(num) && num >= choiceLimits[0])
|
|---|
| 264 | {
|
|---|
| 265 | for (; index < choiceLimits.length - 1; ++index)
|
|---|
| 266 | {
|
|---|
| 267 | if (choiceLimits[index] <= num && num < choiceLimits[index + 1])
|
|---|
| 268 | break;
|
|---|
| 269 | }
|
|---|
| 270 | }
|
|---|
| 271 |
|
|---|
| 272 | return appendBuf.append(choiceFormats[index]);
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
|
|---|