mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-08 06:53:45 +00:00
ICU-3487 add javax.xml.datasource.Duration as a formattable object
X-SVN-Rev: 22413
This commit is contained in:
parent
3c035f1d12
commit
96d422e5ab
3 changed files with 182 additions and 19 deletions
|
@ -8,6 +8,12 @@ package com.ibm.icu.dev.test.duration;
|
|||
|
||||
import java.util.Date;
|
||||
|
||||
// BEGIN JDK>1.5
|
||||
import javax.xml.datatype.DatatypeConfigurationException;
|
||||
import javax.xml.datatype.DatatypeFactory;
|
||||
import javax.xml.datatype.Duration;
|
||||
// END JDK>1.5
|
||||
|
||||
import com.ibm.icu.dev.test.TestFmwk;
|
||||
import com.ibm.icu.text.DurationFormat;
|
||||
import com.ibm.icu.util.ULocale;
|
||||
|
@ -72,7 +78,91 @@ public class ICUDurationTest extends TestFmwk {
|
|||
} else {
|
||||
logln("format long obj -> " + formatted);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
// BEGIN JDK>1.5
|
||||
|
||||
public void TestSimpleXMLDuration() {
|
||||
DatatypeFactory factory = null;
|
||||
try {
|
||||
factory = DatatypeFactory.newInstance();
|
||||
} catch (DatatypeConfigurationException e) {
|
||||
errln("Error instantiating XML DatatypeFactory.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
Duration d;
|
||||
DurationFormat df;
|
||||
String out;
|
||||
String expected;
|
||||
|
||||
|
||||
// test 1
|
||||
d = factory.newDuration("PT2H46M40S");
|
||||
df = DurationFormat.getInstance(new ULocale("en"));
|
||||
expected = "2 hours, 46 minutes, and 40 seconds";
|
||||
out = df.format(d);
|
||||
if(out.equals(expected)) {
|
||||
logln("out=expected: " + expected + " from " + d);
|
||||
} else {
|
||||
errln("FAIL: got " + out + " wanted " + expected + " from " + d);
|
||||
}
|
||||
|
||||
// test 2
|
||||
d = factory.newDuration(10000);
|
||||
df = DurationFormat.getInstance(new ULocale("en"));
|
||||
expected = "10 seconds";
|
||||
out = df.format(d);
|
||||
if(out.equals(expected)) {
|
||||
logln("out=expected: " + expected + " from " + d);
|
||||
} else {
|
||||
errln("FAIL: got " + out + " wanted " + expected + " from " + d);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void TestXMLDuration() {
|
||||
DatatypeFactory factory = null;
|
||||
try {
|
||||
factory = DatatypeFactory.newInstance();
|
||||
} catch (DatatypeConfigurationException e) {
|
||||
errln("Error instantiating XML DatatypeFactory.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
String cases[] = {
|
||||
"en", "PT10.00099S", "10 seconds",
|
||||
"en", "#10000", "10 seconds",
|
||||
"en", "-PT10.00099S", "10 seconds",
|
||||
"en", "#-10000", "10 seconds",
|
||||
|
||||
// from BD req's
|
||||
"en", "PT2H46M40S", "2 hours, 46 minutes, and 40 seconds",
|
||||
"it", "PT2H46M40S", "due ore, 46 minuti e 40 secondi",
|
||||
};
|
||||
|
||||
for(int n=0;n<cases.length;n+=3) {
|
||||
String loc = cases[n+0];
|
||||
String from = cases[n+1];
|
||||
String to = cases[n+2];
|
||||
|
||||
ULocale locale = new ULocale(loc);
|
||||
Duration d;
|
||||
if(from.startsWith("#")) {
|
||||
d = factory.newDuration(Long.parseLong(from.substring(1)));
|
||||
} else {
|
||||
d = factory.newDuration(from);
|
||||
}
|
||||
|
||||
DurationFormat df = DurationFormat.getInstance(locale);
|
||||
String output = df.format(d);
|
||||
|
||||
if(output.equals(to)) {
|
||||
logln("SUCCESS: locale: " + loc + ", from " + from + " ["+d.toString()+"] " +" to " + to + "= " + output);
|
||||
} else {
|
||||
logln("FAIL: locale: " + loc + ", from " + from + " ["+d.toString()+"] " +": expected " + to + " got " + output);
|
||||
}
|
||||
}
|
||||
}
|
||||
// END JDK>1.5
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
*/
|
||||
package com.ibm.icu.impl.duration;
|
||||
|
||||
import java.text.FieldPosition;
|
||||
import java.util.Date;
|
||||
|
||||
import com.ibm.icu.text.DurationFormat;
|
||||
|
@ -13,7 +14,8 @@ import com.ibm.icu.util.ULocale;
|
|||
|
||||
/**
|
||||
* @author srl
|
||||
*
|
||||
* @internal
|
||||
* @deprecated this API is for ICU internal use only
|
||||
*/
|
||||
public class BasicDurationFormat extends DurationFormat {
|
||||
|
||||
|
@ -23,22 +25,43 @@ public class BasicDurationFormat extends DurationFormat {
|
|||
private static final long serialVersionUID = -3146984141909457700L;
|
||||
|
||||
transient DurationFormatter formatter;
|
||||
transient PeriodFormatter pformatter;
|
||||
transient PeriodFormatterService pfs = null;
|
||||
|
||||
public static DurationFormat getInstance(ULocale locale) {
|
||||
return new BasicDurationFormat(locale);
|
||||
}
|
||||
|
||||
|
||||
public StringBuffer format(Object object, StringBuffer toAppend, FieldPosition pos) {
|
||||
if(object instanceof Long) {
|
||||
String res = formatDurationFromNow(((Long)object).longValue());
|
||||
return toAppend.append(res);
|
||||
} else if(object instanceof Date) {
|
||||
String res = formatDurationFromNowTo(((Date)object));
|
||||
return toAppend.append(res);
|
||||
// BEGIN JDK>1.5
|
||||
} else if(object instanceof javax.xml.datatype.Duration) {
|
||||
String res = formatDuration(object);
|
||||
return toAppend.append(res);
|
||||
// END JDK>1.5
|
||||
} else {
|
||||
throw new IllegalArgumentException("Cannot format given Object as a Duration");
|
||||
}
|
||||
}
|
||||
|
||||
public BasicDurationFormat() {
|
||||
formatter = BasicPeriodFormatterService.getInstance().newDurationFormatterFactory().getFormatter();
|
||||
pfs = BasicPeriodFormatterService.getInstance();
|
||||
formatter = pfs.newDurationFormatterFactory().getFormatter();
|
||||
pformatter = pfs.newPeriodFormatterFactory().setDisplayPastFuture(false).getFormatter();
|
||||
}
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public BasicDurationFormat(ULocale locale) {
|
||||
super(locale);
|
||||
formatter = BasicPeriodFormatterService.getInstance().newDurationFormatterFactory().setLocale(locale.getName()).getFormatter();
|
||||
pfs = BasicPeriodFormatterService.getInstance();
|
||||
formatter = pfs.newDurationFormatterFactory().setLocale(locale.getName()).getFormatter();
|
||||
pformatter = pfs.newPeriodFormatterFactory().setDisplayPastFuture(false).setLocale(locale.getName()).getFormatter();
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
|
@ -62,4 +85,64 @@ public class BasicDurationFormat extends DurationFormat {
|
|||
return formatter.formatDurationFromNowTo(targetDate);
|
||||
}
|
||||
|
||||
// BEGIN JDK>1.5
|
||||
private static final javax.xml.datatype.DatatypeConstants.Field inFields[] = {
|
||||
javax.xml.datatype.DatatypeConstants.YEARS,
|
||||
javax.xml.datatype.DatatypeConstants.MONTHS,
|
||||
javax.xml.datatype.DatatypeConstants.DAYS,
|
||||
javax.xml.datatype.DatatypeConstants.HOURS,
|
||||
javax.xml.datatype.DatatypeConstants.MINUTES,
|
||||
javax.xml.datatype.DatatypeConstants.SECONDS,
|
||||
};
|
||||
private static final TimeUnit outFields[] = {
|
||||
TimeUnit.YEAR,
|
||||
TimeUnit.MONTH,
|
||||
TimeUnit.DAY,
|
||||
TimeUnit.HOUR,
|
||||
TimeUnit.MINUTE,
|
||||
TimeUnit.SECOND,
|
||||
};
|
||||
/**
|
||||
* JDK 1.5+ only
|
||||
* @param o
|
||||
* @return
|
||||
* @see http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/datatype/Duration.html
|
||||
*/
|
||||
public String formatDuration(Object obj) {
|
||||
javax.xml.datatype.Duration inDuration = (javax.xml.datatype.Duration)obj;
|
||||
Period p = null;
|
||||
javax.xml.datatype.Duration duration = inDuration;
|
||||
boolean inPast = false;
|
||||
if(inDuration.getSign()<0) {
|
||||
duration = inDuration.negate();
|
||||
inPast = true;
|
||||
}
|
||||
// convert a Duration to a Period
|
||||
for(int i=0;i<inFields.length;i++) {
|
||||
if(duration.isSet(inFields[i])) {
|
||||
Number n = duration.getField(inFields[i]);
|
||||
if(p == null) {
|
||||
p = Period.at(n.floatValue(), outFields[i]);
|
||||
} else {
|
||||
p = p.and(n.floatValue(), outFields[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(p == null) {
|
||||
// no fields set = 0 seconds
|
||||
return formatDurationFromNow(0);
|
||||
} else {
|
||||
if(inPast) {// was negated, above.
|
||||
p = p.inPast();
|
||||
} else {
|
||||
p = p.inFuture();
|
||||
}
|
||||
}
|
||||
|
||||
// now, format it.
|
||||
return pformatter.format(p);
|
||||
}
|
||||
// END JDK>1.5
|
||||
|
||||
}
|
||||
|
|
|
@ -51,25 +51,15 @@ public abstract class DurationFormat extends UFormat {
|
|||
/**
|
||||
* Format an arbitrary object.
|
||||
* Defaults to a call to formatDurationFromNow() for either Long or Date objects.
|
||||
* @param object the object to format. Should be either a Long or Date object.
|
||||
* @param object the object to format. Should be either a Long, Date, or javax.xml.datatype.Duration object.
|
||||
* @param toAppend the buffer to append to
|
||||
* @param pos the field position, may contain additional error messages.
|
||||
* @return the toAppend buffer
|
||||
* @draft ICU 3.8
|
||||
* @provisional This API might change or be removed in a future release.
|
||||
*/
|
||||
public StringBuffer format(Object object, StringBuffer toAppend,
|
||||
FieldPosition pos) {
|
||||
if(object instanceof Long) {
|
||||
String res = formatDurationFromNow(((Long)object).longValue());
|
||||
return toAppend.append(res);
|
||||
} else if(object instanceof Date) {
|
||||
String res = formatDurationFromNowTo(((Date)object));
|
||||
return toAppend.append(res);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Cannot format given Object as a Duration");
|
||||
}
|
||||
}
|
||||
public abstract StringBuffer format(Object object, StringBuffer toAppend,
|
||||
FieldPosition pos);
|
||||
|
||||
/**
|
||||
* DurationFormat cannot parse, by default. This method will throw an UnsupportedOperationException.
|
||||
|
|
Loading…
Add table
Reference in a new issue