Skip to content

Commit 0746ce2

Browse files
Duncan McGregordmcg
authored andcommitted
classes.0 : Baseline
1 parent 1d281b0 commit 0746ce2

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package travelator;
2+
3+
import java.util.Objects;
4+
5+
public class EmailAddress {
6+
private final String localPart; // <1>
7+
private final String domain;
8+
9+
public static EmailAddress parse(String value) { // <2>
10+
var atIndex = value.lastIndexOf('@');
11+
if (atIndex < 1 || atIndex == value.length() - 1)
12+
throw new IllegalArgumentException(
13+
"EmailAddress must be two parts separated by @"
14+
);
15+
return new EmailAddress(
16+
value.substring(0, atIndex),
17+
value.substring(atIndex + 1)
18+
);
19+
}
20+
21+
public EmailAddress(String localPart, String domain) { // <3>
22+
this.localPart = localPart;
23+
this.domain = domain;
24+
}
25+
26+
public String getLocalPart() { // <4>
27+
return localPart;
28+
}
29+
30+
public String getDomain() { // <4>
31+
return domain;
32+
}
33+
34+
@Override
35+
public boolean equals(Object o) { // <5>
36+
if (this == o) return true;
37+
if (o == null || getClass() != o.getClass()) return false;
38+
EmailAddress that = (EmailAddress) o;
39+
return localPart.equals(that.localPart) &&
40+
domain.equals(that.domain);
41+
}
42+
43+
@Override
44+
public int hashCode() { // <5>
45+
return Objects.hash(localPart, domain);
46+
}
47+
48+
@Override
49+
public String toString() { // <6>
50+
return localPart + "@" + domain;
51+
}
52+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package travelator;
2+
3+
public class Marketing {
4+
5+
public static boolean isHotmailAddress(EmailAddress address) {
6+
return address.getDomain().equalsIgnoreCase("hotmail.com");
7+
}
8+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package travelator;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
7+
8+
public class EmailAddressTests {
9+
10+
@Test
11+
public void parsing() {
12+
assertEquals(
13+
new EmailAddress("fred", "example.com"),
14+
EmailAddress.parse("fred@example.com")
15+
);
16+
}
17+
18+
@Test
19+
public void parsingFailures() {
20+
assertThrows(
21+
IllegalArgumentException.class,
22+
() -> EmailAddress.parse("@")
23+
);
24+
assertThrows(
25+
IllegalArgumentException.class,
26+
() -> EmailAddress.parse("fred@")
27+
);
28+
assertThrows(
29+
IllegalArgumentException.class,
30+
() -> EmailAddress.parse("@example.com")
31+
);
32+
assertThrows(
33+
IllegalArgumentException.class,
34+
() -> EmailAddress.parse("fred_at_example.com")
35+
);
36+
}
37+
38+
@Test
39+
public void parsingWithAtInLocalPart() {
40+
assertEquals(
41+
new EmailAddress("\"b@t\"", "example.com"),
42+
EmailAddress.parse("\"b@t\"@example.com")
43+
);
44+
}
45+
}

0 commit comments

Comments
 (0)