Failing test + gentle refactor

This commit is contained in:
dotasek 2022-06-23 12:32:02 -04:00
parent 45efd0afaf
commit c6a69e81ae
4 changed files with 73 additions and 36 deletions

View File

@ -1,11 +1,7 @@
package org.hl7.fhir.utilities;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hl7.fhir.utilities.TimeTracker.Counter;
public class TimeTracker {
@ -72,29 +68,29 @@ public class TimeTracker {
CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder();
for (Counter c : records) {
if (c.count == 1) {
b.append(c.name+": "+Utilities.presentDuration(c.length));
b.append(c.name+": "+ TimeUtil.presentDuration(c.length));
}
}
for (Counter c : records) {
if (c.count > 1) {
b.append(c.name+": "+Utilities.presentDuration(c.length)+" (#"+c.count+")");
b.append(c.name+": "+ TimeUtil.presentDuration(c.length)+" (#"+c.count+")");
}
}
return "Times: "+b.toString();
}
public String clock() {
return Utilities.presentDuration(System.nanoTime() - globalStart);
return TimeUtil.presentDuration(System.nanoTime() - globalStart);
}
public String instant() {
return Utilities.presentDuration(System.nanoTime() - globalStart);
return TimeUtil.presentDuration(System.nanoTime() - globalStart);
}
public String milestone() {
long start = milestone == 0 ? globalStart : milestone ;
milestone = System.nanoTime();
return Utilities.presentDuration(milestone - start);
return TimeUtil.presentDuration(milestone - start);
}
}

View File

@ -0,0 +1,29 @@
package org.hl7.fhir.utilities;
import java.util.concurrent.TimeUnit;
public class TimeUtil {
public static String presentDuration(long duration) {
duration = duration / 1000000;
String res = ""; // ;
long days = TimeUnit.MILLISECONDS.toDays(duration);
long hours = TimeUnit.MILLISECONDS.toHours(duration) -
TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration));
long minutes = TimeUnit.MILLISECONDS.toMinutes(duration) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(duration));
long seconds = TimeUnit.MILLISECONDS.toSeconds(duration) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration));
long millis = TimeUnit.MILLISECONDS.toMillis(duration) -
TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(duration));
if (days > 0)
res = String.format("%dd %02d:%02d:%02d.%04d", days, hours, minutes, seconds, millis);
else if (hours > 0)
res = String.format("%02d:%02d:%02d.%04d", hours, minutes, seconds, millis);
else //
res = String.format("%02d:%02d.%04d", minutes, seconds, millis);
// else
// res = String.format("%02d.%04d", seconds, millis);
return res;
}
}

View File

@ -22,14 +22,12 @@ import java.nio.file.StandardCopyOption;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
@ -92,7 +90,7 @@ public class Utilities {
*
* @param word the word that is to be pluralized.
* @return the pluralized form of the word, or the word itself if it could not be pluralized
* @see #singularize(Object)
* @see Inflector#singularize(Object)
*/
public static String pluralizeMe(String word) {
Inflector inf = new Inflector();
@ -1424,30 +1422,6 @@ public class Utilities {
return byteArrays;
}
public static String presentDuration(long duration) {
duration = duration / 1000000;
String res = ""; // ;
long days = TimeUnit.MILLISECONDS.toDays(duration);
long hours = TimeUnit.MILLISECONDS.toHours(duration) -
TimeUnit.DAYS.toHours(TimeUnit.MILLISECONDS.toDays(duration));
long minutes = TimeUnit.MILLISECONDS.toMinutes(duration) -
TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours(duration));
long seconds = TimeUnit.MILLISECONDS.toSeconds(duration) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(duration));
long millis = TimeUnit.MILLISECONDS.toMillis(duration) -
TimeUnit.SECONDS.toMillis(TimeUnit.MILLISECONDS.toSeconds(duration));
if (days > 0)
res = String.format("%dd %02d:%02d:%02d.%04d", days, hours, minutes, seconds, millis);
else if (hours > 0)
res = String.format("%02d:%02d:%02d.%04d", hours, minutes, seconds, millis);
else //
res = String.format("%02d:%02d.%04d", minutes, seconds, millis);
// else
// res = String.format("%02d.%04d", seconds, millis);
return res;
}
public static void unzip(InputStream zip, Path target) throws IOException {
try (ZipInputStream zis = new ZipInputStream(zip)) {
ZipEntry zipEntry = zis.getNextEntry();

View File

@ -0,0 +1,38 @@
package org.hl7.fhir.utilities;
import org.hl7.fhir.utilities.xml.XMLUtil;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TimeUtilTest {
public static Stream<Arguments> data() throws ParserConfigurationException, SAXException, IOException {
List<Arguments> objects = new ArrayList<>();
objects.add(Arguments.of("PT0.001S", "00:00.001"));
return objects.stream();
}
@ParameterizedTest
@MethodSource("data")
public void testPresentDuration(String iso8601String, String expectedPresentation) {
assertEquals(TimeUtil.presentDuration(Duration.parse(iso8601String).toNanos()), expectedPresentation);
}
}