-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from mawHBT/develop
Started with "Fix Unit of Measurement Values" (issue #84)
- Loading branch information
Showing
5 changed files
with
132 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/de/dagere/peass/ci/helper/UnitConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package de.dagere.peass.ci.helper; | ||
|
||
public class UnitConverter { | ||
|
||
public static final int NANOSECONDS_TO_MICROSECONDS = 1000; | ||
public static final int NANOSECONDS_TO_MILLISECONDS = 1000 * 1000; | ||
public static final int NANOSECONDS_TO_SECONDS = 1000 * 1000 * 1000; | ||
|
||
public static final String NANOSECONDS = "ns"; | ||
public static final String MICROSECONDS = "\u00B5s"; | ||
public static final String MILLISECONDS = "ms"; | ||
public static final String SECONDS = "s"; | ||
|
||
public static int getFactorByMean(double mean) { | ||
int count = 0; | ||
|
||
while (count < 3 && mean >= 1000) { | ||
mean = mean / 1000; | ||
count++; | ||
} | ||
|
||
return getFactorByCount(count); | ||
} | ||
|
||
private static int getFactorByCount(final int count) { | ||
switch (count) { | ||
case 0: | ||
return 1; | ||
case 1: | ||
return NANOSECONDS_TO_MICROSECONDS; | ||
case 2: | ||
return NANOSECONDS_TO_MILLISECONDS; | ||
case 3: | ||
return NANOSECONDS_TO_SECONDS; | ||
default: | ||
// this should not happen! | ||
return 0; | ||
} | ||
} | ||
|
||
public static String getUnitByFactor(final int factor) { | ||
switch (factor) { | ||
case 1: | ||
return "ns"; | ||
case NANOSECONDS_TO_MICROSECONDS: | ||
return "\u00B5s"; | ||
case NANOSECONDS_TO_MILLISECONDS: | ||
return "ms"; | ||
case NANOSECONDS_TO_SECONDS: | ||
return "s"; | ||
default: | ||
// this should not happen! | ||
return "unknown factor provided!"; | ||
} | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/de/dagere/peass/ci/helper/ValuesAndUnit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package de.dagere.peass.ci.helper; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
|
||
public class ValuesAndUnit { | ||
|
||
private final double[] values; | ||
private final String unit; | ||
|
||
@SuppressFBWarnings | ||
public ValuesAndUnit(final double[] values, final String unit) { | ||
this.values = values; | ||
this.unit = unit; | ||
} | ||
|
||
@SuppressFBWarnings | ||
public double[] getValues() { | ||
return values; | ||
} | ||
|
||
public String getUnit() { | ||
return unit; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package de.peass.ci.helper; | ||
|
||
import de.dagere.peass.ci.helper.UnitConverter; | ||
import org.junit.Assert; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class TestUnitConverter { | ||
|
||
final double nanoSecondMean = 123; | ||
final double mikroSecondMean = 1234; | ||
final double milliSecondMean = 1234567; | ||
final double secondMean = 12345678E6; | ||
|
||
@Test | ||
public void testGetFactorByMean() { | ||
Assert.assertEquals(1, UnitConverter.getFactorByMean(nanoSecondMean)); | ||
Assert.assertEquals(UnitConverter.NANOSECONDS_TO_MICROSECONDS, UnitConverter.getFactorByMean(mikroSecondMean)); | ||
Assert.assertEquals(UnitConverter.NANOSECONDS_TO_MILLISECONDS, UnitConverter.getFactorByMean(milliSecondMean)); | ||
Assert.assertEquals(UnitConverter.NANOSECONDS_TO_SECONDS, UnitConverter.getFactorByMean(secondMean)); | ||
} | ||
|
||
@Test | ||
public void testGetUnitByFactor() { | ||
Assert.assertEquals(UnitConverter.NANOSECONDS, UnitConverter.getUnitByFactor(1)); | ||
Assert.assertEquals(UnitConverter.MICROSECONDS, UnitConverter.getUnitByFactor(UnitConverter.NANOSECONDS_TO_MICROSECONDS)); | ||
Assert.assertEquals(UnitConverter.MILLISECONDS, UnitConverter.getUnitByFactor(UnitConverter.NANOSECONDS_TO_MILLISECONDS)); | ||
Assert.assertEquals(UnitConverter.SECONDS, UnitConverter.getUnitByFactor(UnitConverter.NANOSECONDS_TO_SECONDS)); | ||
} | ||
|
||
} |