Skip to content

Commit d13ddb9

Browse files
Add hasType method
1 parent 3df2298 commit d13ddb9

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

src/main/java/io/github/malczuuu/problem4j/core/Problem.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.Serializable;
44
import java.net.URI;
55
import java.util.Map;
6+
import java.util.Objects;
67
import java.util.Set;
78

89
/**
@@ -120,6 +121,16 @@ static Extension extension(String key, Object value) {
120121
*/
121122
ProblemBuilder toBuilder();
122123

124+
/**
125+
* A convenience method to verify if {@code type} field was assigned, as {@link #BLANK_TYPE} also
126+
* mean that type is unassigned.
127+
*
128+
* @return {@code true} if {@code type} is assigned to a non-blank value, {@code false} otherwise
129+
*/
130+
default boolean hasType() {
131+
return getType() != null && !Objects.equals(getType(), BLANK_TYPE);
132+
}
133+
123134
/** Represents a single key-value extension in a {@link Problem}. */
124135
interface Extension extends Map.Entry<String, Object> {
125136

src/test/java/io/github/malczuuu/problem4j/core/ProblemBuilderImplTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,31 @@ void givenNullURIType_shouldNotSetIt() {
2525
Problem problem = Problem.builder().type((URI) null).build();
2626

2727
assertThat(problem.getType()).isEqualTo(Problem.BLANK_TYPE);
28+
assertThat(problem.hasType()).isFalse();
2829
}
2930

3031
@Test
3132
void givenNullStringType_shouldNotSetIt() {
3233
Problem problem = Problem.builder().type((String) null).build();
3334

3435
assertThat(problem.getType()).isEqualTo(Problem.BLANK_TYPE);
36+
assertThat(problem.hasType()).isFalse();
37+
}
38+
39+
@Test
40+
void givenBlankURIType_shouldNotSetIt() {
41+
Problem problem = Problem.builder().type(Problem.BLANK_TYPE).build();
42+
43+
assertThat(problem.getType()).isEqualTo(Problem.BLANK_TYPE);
44+
assertThat(problem.hasType()).isFalse();
45+
}
46+
47+
@Test
48+
void givenBlankStringType_shouldNotSetIt() {
49+
Problem problem = Problem.builder().type(Problem.BLANK_TYPE.toString()).build();
50+
51+
assertThat(problem.getType()).isEqualTo(Problem.BLANK_TYPE);
52+
assertThat(problem.hasType()).isFalse();
3553
}
3654

3755
@Test

0 commit comments

Comments
 (0)