Skip to content

Commit 2c63a5d

Browse files
committed
DecimalFormat, NumberFormat
-- adds BigDecimal and BigInteger for DecimalFormat -- reworks NumberFormat and DecimalFormat to work with String[] instead of StringBuilder
1 parent 06f9e82 commit 2c63a5d

File tree

7 files changed

+1218
-1099
lines changed

7 files changed

+1218
-1099
lines changed

sources/net.sf.j2s.java.core/src/java/text/CharacterIteratorFieldDelegate.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*/
2828
package java.text;
2929

30+
import java.text.Format.Field;
3031
import java.util.ArrayList;
3132

3233
/**
@@ -130,4 +131,50 @@ public AttributedCharacterIterator getIterator(String string) {
130131
}
131132
return new AttributedString(iterators).getIterator();
132133
}
134+
135+
@Override
136+
public void formatted(Field attr, Object value, int start, int end, String[] buffer) {
137+
if (start != end) {
138+
if (start < size) {
139+
// Adjust attributes of existing runs
140+
int index = size;
141+
int asIndex = attributedStrings.size() - 1;
142+
143+
while (start < index) {
144+
AttributedString as = (AttributedString)attributedStrings.
145+
get(asIndex--);
146+
int newIndex = index - as.length();
147+
int aStart = Math.max(0, start - newIndex);
148+
149+
as.addAttribute(attr, value, aStart, Math.min(
150+
end - start, as.length() - aStart) +
151+
aStart);
152+
index = newIndex;
153+
}
154+
}
155+
if (size < start) {
156+
// Pad attributes
157+
attributedStrings.add(new AttributedString(
158+
buffer[0].substring(size, start)));
159+
size = start;
160+
}
161+
if (size < end) {
162+
// Add new string
163+
int aStart = Math.max(start, size);
164+
AttributedString string = new AttributedString(
165+
buffer[0].substring(aStart, end));
166+
167+
string.addAttribute(attr, value);
168+
attributedStrings.add(string);
169+
size = end;
170+
}
171+
}
172+
}
173+
174+
@Override
175+
public void formatted(int fieldID, Field attr, Object value, int start, int end, String[] buffer) {
176+
formatted(attr, value, start, end, buffer);
177+
}
178+
179+
133180
}

sources/net.sf.j2s.java.core/src/java/text/ChoiceFormat.java

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,11 @@ public class ChoiceFormat extends NumberFormat {
174174
* @param newPattern See the class description.
175175
*/
176176
public void applyPattern(String newPattern) {
177-
StringBuffer[] segments = new StringBuffer[2];
178-
for (int i = 0; i < segments.length; ++i) {
179-
segments[i] = new StringBuffer();
180-
}
177+
String[] segments = new String[] {"",""};
178+
// StringBuffer[] segments = new StringBuffer[2];
179+
// for (int i = 0; i < segments.length; ++i) {
180+
// segments[i] = new StringBuffer();
181+
// }
181182
double[] newChoiceLimits = new double[30];
182183
String[] newChoiceFormats = new String[30];
183184
int count = 0;
@@ -190,13 +191,13 @@ public void applyPattern(String newPattern) {
190191
if (ch=='\'') {
191192
// Check for "''" indicating a literal quote
192193
if ((i+1)<newPattern.length() && newPattern.charAt(i+1)==ch) {
193-
segments[part].append(ch);
194+
segments[part] += ch;
194195
++i;
195196
} else {
196197
inQuote = !inQuote;
197198
}
198199
} else if (inQuote) {
199-
segments[part].append(ch);
200+
segments[part] += ch;
200201
} else if (ch == '<' || ch == '#' || ch == '\u2264') {
201202
if (segments[0].length() == 0) {
202203
throw new IllegalArgumentException();
@@ -220,7 +221,7 @@ public void applyPattern(String newPattern) {
220221
if (startValue <= oldStartValue) {
221222
throw new IllegalArgumentException();
222223
}
223-
segments[0].setLength(0);
224+
segments[0] = "";
224225
part = 1;
225226
} else if (ch == '|') {
226227
if (count == newChoiceLimits.length) {
@@ -231,10 +232,10 @@ public void applyPattern(String newPattern) {
231232
newChoiceFormats[count] = segments[1].toString();
232233
++count;
233234
oldStartValue = startValue;
234-
segments[1].setLength(0);
235+
segments[1] = "";
235236
part = 0;
236237
} else {
237-
segments[part].append(ch);
238+
segments[part] += ch;
238239
}
239240
}
240241
// clean up last one
@@ -259,10 +260,11 @@ public void applyPattern(String newPattern) {
259260
* @return the pattern string
260261
*/
261262
public String toPattern() {
262-
StringBuffer result = new StringBuffer();
263+
String result = "";
264+
// StringBuffer result = new StringBuffer();
263265
for (int i = 0; i < choiceLimits.length; ++i) {
264266
if (i != 0) {
265-
result.append('|');
267+
result += '|';
266268
}
267269
// choose based upon which has less precision
268270
// approximate that by choosing the closest one to an integer.
@@ -272,17 +274,16 @@ public String toPattern() {
272274
double tryLess = Math.abs(Math.IEEEremainder(less, 1.0d));
273275

274276
if (tryLessOrEqual < tryLess) {
275-
result.append(""+choiceLimits[i]);
276-
result.append('#');
277+
result += choiceLimits[i] + "#";
277278
} else {
278279
if (choiceLimits[i] == Double.POSITIVE_INFINITY) {
279-
result.append("\u221E");
280+
result += ("\u221E");
280281
} else if (choiceLimits[i] == Double.NEGATIVE_INFINITY) {
281-
result.append("-\u221E");
282+
result += ("-\u221E");
282283
} else {
283-
result.append(""+less);
284+
result += (""+less);
284285
}
285-
result.append('<');
286+
result += ('<');
286287
}
287288
// Append choiceFormats[i], using quotes if there are special characters.
288289
// Single quotes themselves must be escaped in either case.
@@ -291,18 +292,18 @@ public String toPattern() {
291292
|| text.indexOf('#') >= 0
292293
|| text.indexOf('\u2264') >= 0
293294
|| text.indexOf('|') >= 0;
294-
if (needQuote) result.append('\'');
295-
if (text.indexOf('\'') < 0) result.append(text);
295+
if (needQuote) result += ('\'');
296+
if (text.indexOf('\'') < 0) result += (text);
296297
else {
297298
for (int j=0; j<text.length(); ++j) {
298299
char c = text.charAt(j);
299-
result.append(c);
300-
if (c == '\'') result.append(c);
300+
result += (c);
301+
if (c == '\'') result += (c);
301302
}
302303
}
303-
if (needQuote) result.append('\'');
304+
if (needQuote) result += ('\'');
304305
}
305-
return result.toString();
306+
return result;
306307
}
307308

308309
/**
@@ -367,16 +368,15 @@ public Object[] getFormats() {
367368
return newFormats;
368369
}
369370

370-
// Overrides
371-
372371
/**
373372
* Specialization of format. This method really calls
374373
* <code>format(double, StringBuffer, FieldPosition)</code>
375374
* thus the range of longs that are supported is only equal to
376375
* the range that can be stored by double. This will never be
377376
* a practical limitation.
378377
*/
379-
public StringBuffer format(long number, StringBuffer toAppendTo,
378+
@Override
379+
public StringBuffer format(long number, StringBuffer toAppendTo,
380380
FieldPosition status) {
381381
return format((double)number, toAppendTo, status);
382382
}
@@ -387,6 +387,7 @@ public StringBuffer format(long number, StringBuffer toAppendTo,
387387
* @param toAppendTo where text is appended.
388388
* @param status ignore no useful status is returned.
389389
*/
390+
@Override
390391
public StringBuffer format(double number, StringBuffer toAppendTo,
391392
FieldPosition status) {
392393
// find the number
@@ -399,8 +400,16 @@ public StringBuffer format(double number, StringBuffer toAppendTo,
399400
}
400401
--i;
401402
if (i < 0) i = 0;
403+
String s = choiceFormats[i];
402404
// return either a formatted number, or a string
403-
return toAppendTo.append(choiceFormats[i]);
405+
/**
406+
* @j2sNative
407+
*
408+
* if (Array.isArray(toAppendTo)) {
409+
* toAppendTo[0] += s;
410+
* return toAppendTo;
411+
*/
412+
return toAppendTo.append(s);
404413
}
405414

406415
/**
@@ -415,7 +424,8 @@ public StringBuffer format(double number, StringBuffer toAppendTo,
415424
* first index of the character that caused the parse to fail.
416425
* @return A Number representing the value of the number parsed.
417426
*/
418-
public Number parse(String text, ParsePosition status) {
427+
@Override
428+
public Number parse(String text, ParsePosition status) {
419429
// find the best number (defined as the one with the longest parse)
420430
int start = status.index;
421431
int furthest = start;
@@ -468,7 +478,8 @@ public static final double previousDouble (double d) {
468478
/**
469479
* Overrides Cloneable
470480
*/
471-
public Object clone()
481+
@Override
482+
public Object clone()
472483
{
473484
ChoiceFormat other = (ChoiceFormat) super.clone();
474485
// for primitives or immutables, shallow clone is enough
@@ -480,7 +491,8 @@ public Object clone()
480491
/**
481492
* Generates a hash code for the message format object.
482493
*/
483-
public int hashCode() {
494+
@Override
495+
public int hashCode() {
484496
int result = choiceLimits.length;
485497
if (choiceFormats.length > 0) {
486498
// enough for reasonable distribution
@@ -492,7 +504,8 @@ public int hashCode() {
492504
/**
493505
* Equality comparision between two
494506
*/
495-
public boolean equals(Object obj) {
507+
@Override
508+
public boolean equals(Object obj) {
496509
if (obj == null) return false;
497510
if (this == obj) // quick check
498511
return true;

0 commit comments

Comments
 (0)