Skip to content

Commit

Permalink
ICU-12811 Fix cldrUtils (add ElapsedTime and UnicodeMap*)
Browse files Browse the repository at this point in the history
  • Loading branch information
mihnita committed Mar 14, 2023
1 parent 2c20fa4 commit b399c67
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 3 deletions.
7 changes: 4 additions & 3 deletions icu4j/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@
</icu-junit>
</target>

<target name="translitCheck" depends="info, core, translit, langdata, translit-tests" description="Run the ICU4J Translit test suite">
<target name="translitCheck" depends="info, core, translit, langdata, core-tests, translit-tests" description="Run the ICU4J Translit test suite">
<antcall target="_translitCheck"/>
<fail message="test failed" if="icu-junit-failure" />
</target>
Expand Down Expand Up @@ -1263,7 +1263,7 @@
</ant>
</target>

<target name="charset-tests" depends="charset, test-framework" description="Build charset tests">
<target name="charset-tests" depends="charset, core-tests, test-framework" description="Build charset tests">
<ant dir="${icu4j.charset-tests.dir}" inheritAll="false">
<reference refid="junit.jars"/>
</ant>
Expand All @@ -1281,7 +1281,7 @@
</ant>
</target>

<target name="translit-tests" depends="translit, test-framework" description="Build translit tests">
<target name="translit-tests" depends="translit, core-tests, test-framework" description="Build translit tests">
<ant dir="${icu4j.translit-tests.dir}" inheritAll="false">
<reference refid="junit.jars"/>
</ant>
Expand Down Expand Up @@ -1874,6 +1874,7 @@

<src path="${icu4j.tools.dir}/src"/>
<src path="${icu4j.test-framework.dir}/src"/>
<src path="${icu4j.translit-tests.dir}/src"/>

<include name="com/ibm/icu/dev/util/CollectionUtilities.java" />
<include name="com/ibm/icu/dev/util/ElapsedTimer.java" />
Expand Down
110 changes: 110 additions & 0 deletions icu4j/main/tests/translit/src/com/ibm/icu/dev/util/ElapsedTimer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
//
// ElapsedTimer.java
//
// Created by Steven R. Loomis on 11/11/2005.
// Copyright 2005-2012 IBM. All rights reserved.
//

package com.ibm.icu.dev.util;

import java.util.Locale;

import com.ibm.icu.text.MessageFormat;
import com.ibm.icu.text.NumberFormat;
import com.ibm.icu.text.RuleBasedNumberFormat;


/**
* Simple stopwatch timer.
* Usage: { ElapsedTimer et = new ElapsedTimer();
* do_some_stuff;
* System.out.println("It took " + et + " to do stuff."); }
*
* Advanced: { ElapsedTimer et = new ElapsedTimer("Thing2's time: {0}"); // messageformat pattern
* do_thing_2();
* System.out.println(et.toString()); }
*
* More advanced: NumberFormat and/or MessageFormat can be provided in the constructor
*
* @internal CLDR
*/
public final class ElapsedTimer {

/**
* Convenience method to print the elasped time (in milliseconds)
*/
public static String elapsedTime(long start, long end) {
return diffTime(getFormat(), start, end);
}

public static String elapsedTime(long start) {
return diffTime(getFormat(), start, System.currentTimeMillis());
}

// class

private long startTime = System.currentTimeMillis();
private NumberFormat myDurationFormat = null;
private MessageFormat myMsgFormat = null;

public ElapsedTimer() {
}

public ElapsedTimer(MessageFormat aMsgFmt) {
myMsgFormat = aMsgFmt;
}

public ElapsedTimer(NumberFormat aNumFmt) {
myDurationFormat = aNumFmt;
}

public ElapsedTimer(MessageFormat aMsgFmt, NumberFormat aNumFmt) {
myMsgFormat = aMsgFmt;
myDurationFormat = aNumFmt;
}

public ElapsedTimer(String pattern) {
myMsgFormat = new MessageFormat(pattern);
}

public ElapsedTimer(String pattern, NumberFormat aNumFmt) {
myMsgFormat = new MessageFormat(pattern);
myDurationFormat = aNumFmt;
}

/**
* @return elapsed time in seconds since object creation
*/
public final String toString() {
long endTime = System.currentTimeMillis();
String duration = diffTime(myDurationFormat, startTime, endTime);
if(myMsgFormat == null) {
return duration;
} else {
return myMsgFormat.format(new Object[] {duration});
}
}

private static NumberFormat gFormat = null;

private static NumberFormat getFormat() {
if(gFormat == null) {
gFormat = new RuleBasedNumberFormat(Locale.US,
RuleBasedNumberFormat.DURATION);
}
return gFormat;
}

private static String diffTime(NumberFormat fmt, long start, long end) {
if(fmt==null) {
fmt = getFormat();
}
synchronized(fmt) {
long age = end - start;
long diff = age/1000; // millis per second. Workaround ticket:7936 by using whole number seconds.
return fmt.format(diff);
}
}
}

0 comments on commit b399c67

Please sign in to comment.