Skip to content

Commit 59a2d0f

Browse files
Fixed-formatting-issues
1 parent c5c0d23 commit 59a2d0f

File tree

2 files changed

+17
-48
lines changed

2 files changed

+17
-48
lines changed

src/main/java/com/thealgorithms/conversions/TimeConverter.java

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ private enum TimeUnit {
3939
HOURS(3600.0),
4040
DAYS(86400.0),
4141
WEEKS(604800.0),
42-
MONTHS(2629800.0), // 30.44 days
43-
YEARS(31557600.0); // 365.25 days
42+
MONTHS(2629800.0), // 30.44 days
43+
YEARS(31557600.0); // 365.25 days
4444

4545
private final double seconds;
4646

@@ -57,15 +57,8 @@ public double fromSeconds(double secondsValue) {
5757
}
5858
}
5959

60-
private static final Map<String, TimeUnit> UNIT_LOOKUP =
61-
Map.ofEntries(
62-
Map.entry("seconds", TimeUnit.SECONDS),
63-
Map.entry("minutes", TimeUnit.MINUTES),
64-
Map.entry("hours", TimeUnit.HOURS),
65-
Map.entry("days", TimeUnit.DAYS),
66-
Map.entry("weeks", TimeUnit.WEEKS),
67-
Map.entry("months", TimeUnit.MONTHS),
68-
Map.entry("years", TimeUnit.YEARS));
60+
private static final Map<String, TimeUnit> UNIT_LOOKUP
61+
= Map.ofEntries(Map.entry("seconds", TimeUnit.SECONDS), Map.entry("minutes", TimeUnit.MINUTES), Map.entry("hours", TimeUnit.HOURS), Map.entry("days", TimeUnit.DAYS), Map.entry("weeks", TimeUnit.WEEKS), Map.entry("months", TimeUnit.MONTHS), Map.entry("years", TimeUnit.YEARS));
6962

7063
/**
7164
* Converts a time value from one unit to another.
@@ -97,8 +90,7 @@ private static TimeUnit resolveUnit(String unit) {
9790
}
9891
TimeUnit resolved = UNIT_LOOKUP.get(unit.toLowerCase(Locale.ROOT));
9992
if (resolved == null) {
100-
throw new IllegalArgumentException(
101-
"Invalid unit '" + unit + "'. Supported units are: " + UNIT_LOOKUP.keySet());
93+
throw new IllegalArgumentException("Invalid unit '" + unit + "'. Supported units are: " + UNIT_LOOKUP.keySet());
10294
}
10395
return resolved;
10496
}
Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,21 @@
11
package com.thealgorithms.conversions;
22

3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.util.stream.Stream;
36
import org.junit.jupiter.api.DisplayName;
47
import org.junit.jupiter.api.Test;
58
import org.junit.jupiter.params.ParameterizedTest;
69
import org.junit.jupiter.params.provider.CsvSource;
710
import org.junit.jupiter.params.provider.MethodSource;
811

9-
import java.util.stream.Stream;
10-
11-
import static org.junit.jupiter.api.Assertions.*;
12-
1312
class TimeConverterTest {
1413

1514
@ParameterizedTest(name = "{0} {1} -> {2} {3}")
16-
@CsvSource({
17-
"60, seconds, minutes, 1",
18-
"120, seconds, minutes, 2",
19-
"2, minutes, seconds, 120",
20-
"2, hours, minutes, 120",
21-
"1, days, hours, 24",
22-
"1, weeks, days, 7",
23-
"1, months, days, 30.438",
24-
"1, years, days, 365.25",
25-
"3600, seconds, hours, 1",
26-
"86400, seconds, days, 1",
27-
"604800, seconds, weeks, 1",
28-
"31557600, seconds, years, 1"
29-
})
30-
void testValidConversions(double value, String from, String to, double expected) {
15+
@CsvSource({"60, seconds, minutes, 1", "120, seconds, minutes, 2", "2, minutes, seconds, 120", "2, hours, minutes, 120", "1, days, hours, 24", "1, weeks, days, 7", "1, months, days, 30.438", "1, years, days, 365.25", "3600, seconds, hours, 1", "86400, seconds, days, 1",
16+
"604800, seconds, weeks, 1", "31557600, seconds, years, 1"})
17+
void
18+
testValidConversions(double value, String from, String to, double expected) {
3119
assertEquals(expected, TimeConverter.convertTime(value, from, to));
3220
}
3321

@@ -46,25 +34,17 @@ void testSameUnitConversion() {
4634
@Test
4735
@DisplayName("Negative value throws exception")
4836
void testNegativeValue() {
49-
assertThrows(IllegalArgumentException.class, () ->
50-
TimeConverter.convertTime(-5, "seconds", "minutes"));
37+
assertThrows(IllegalArgumentException.class, () -> TimeConverter.convertTime(-5, "seconds", "minutes"));
5138
}
5239

5340
@ParameterizedTest
54-
@CsvSource({
55-
"lightyears, seconds",
56-
"minutes, centuries"
57-
})
41+
@CsvSource({"lightyears, seconds", "minutes, centuries"})
5842
void testInvalidUnits(String from, String to) {
59-
assertThrows(IllegalArgumentException.class, () ->
60-
TimeConverter.convertTime(10, from, to));
43+
assertThrows(IllegalArgumentException.class, () -> TimeConverter.convertTime(10, from, to));
6144
}
6245

6346
static Stream<org.junit.jupiter.params.provider.Arguments> roundTripCases() {
64-
return Stream.of(
65-
org.junit.jupiter.params.provider.Arguments.of(1.0, "hours", "minutes"),
66-
org.junit.jupiter.params.provider.Arguments.of(2.5, "days", "hours"),
67-
org.junit.jupiter.params.provider.Arguments.of(1000, "seconds", "minutes"));
47+
return Stream.of(org.junit.jupiter.params.provider.Arguments.of(1.0, "hours", "minutes"), org.junit.jupiter.params.provider.Arguments.of(2.5, "days", "hours"), org.junit.jupiter.params.provider.Arguments.of(1000, "seconds", "minutes"));
6848
}
6949

7050
@ParameterizedTest
@@ -73,9 +53,6 @@ static Stream<org.junit.jupiter.params.provider.Arguments> roundTripCases() {
7353
void testRoundTripConversion(double value, String from, String to) {
7454
double converted = TimeConverter.convertTime(value, from, to);
7555
double roundTrip = TimeConverter.convertTime(converted, to, from);
76-
assertEquals(
77-
Math.round(value * 1000.0) / 1000.0,
78-
Math.round(roundTrip * 1000.0) / 1000.0,
79-
0.05);
56+
assertEquals(Math.round(value * 1000.0) / 1000.0, Math.round(roundTrip * 1000.0) / 1000.0, 0.05);
8057
}
8158
}

0 commit comments

Comments
 (0)