Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b89e56a
commit f3f2709
Showing
3 changed files
with
104 additions
and
0 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/us/ajg0702/leaderboards/formatting/formats/ColonTime.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 us.ajg0702.leaderboards.formatting.formats; | ||
|
||
import us.ajg0702.leaderboards.formatting.Format; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class ColonTime extends Format { | ||
|
||
private final Pattern pattern = Pattern.compile("([0-9]*)?:([0-9]*):([0-9]*)(:|.)?([0-9]*)?"); | ||
@Override | ||
public boolean matches(String output, String placeholder) { | ||
if(output == null) return false; | ||
return pattern.matcher(output).matches(); | ||
} | ||
|
||
@Override | ||
public double toDouble(String input) throws NumberFormatException { | ||
Matcher matcher = pattern.matcher(input); | ||
|
||
int hours = Integer.parseInt(matcher.group(1)); | ||
int minutes = Integer.parseInt(matcher.group(2)); | ||
int seconds = Integer.parseInt(matcher.group(3)); | ||
|
||
String secondSeperator = matcher.group(4); | ||
int miliSeconds = matcher.group(5) != null ? Integer.parseInt(matcher.group(5)) : -1; | ||
|
||
double result = 0; | ||
|
||
result += seconds; | ||
result += minutes * 60; | ||
result += hours * 60 * 60; | ||
|
||
if(secondSeperator != null && miliSeconds != -1) { | ||
if(secondSeperator.equals(":")) { | ||
result += miliSeconds / 1000d; | ||
} else if(secondSeperator.equals(".")) { | ||
result += Integer.parseInt("0." + miliSeconds); | ||
} | ||
} | ||
|
||
return result; | ||
|
||
} | ||
|
||
@Override | ||
public String toFormat(double input) { | ||
int hours = (int) (input / (60 * 60)); | ||
int minutes = (int) ((input % (60 * 60)) / 60); | ||
double seconds = input % 60d; | ||
|
||
return | ||
hours == 0 ? "00" : hours + | ||
minutes == 0 ? "00" : minutes + | ||
seconds == 0 ? "00" : seconds + ""; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/test/java/us/ajg0702/leaderboards/formatting/formats/ColonTimeTest.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,45 @@ | ||
package us.ajg0702.leaderboards.formatting.formats; | ||
|
||
import junit.framework.TestCase; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class ColonTimeTest extends TestCase { | ||
|
||
public void testMatches() throws Exception { | ||
List<String> shouldMatch = Arrays.asList( | ||
"00:00:00.00", | ||
"00:00:00:00", | ||
"1:2:3:4", | ||
"1:2:3.4", | ||
"11:22:33:44", | ||
"11:22:33.44", | ||
"12:3:15.735" | ||
); | ||
List<String> shouldNotMatch = Arrays.asList( | ||
"hello there", | ||
"15e", | ||
"12n 15b", | ||
"[60]", | ||
"123456", | ||
"13h 21m" | ||
); | ||
|
||
ColonTime colonTimeFormat = new ColonTime(); | ||
|
||
for (String match : shouldMatch) { | ||
boolean res = colonTimeFormat.matches(match, ""); | ||
if(!res) { | ||
throw new Exception(match + " did not match when it should have!"); | ||
} | ||
} | ||
|
||
for (String notMatch : shouldNotMatch) { | ||
if(colonTimeFormat.matches(notMatch, "")) { | ||
throw new Exception(notMatch + " did match when it should not have!"); | ||
} | ||
} | ||
} | ||
|
||
} |