Skip to content

Commit eb261ba

Browse files
committed
adding hashCode() and equals() to Regexp implementations
1 parent 6ed2cc4 commit eb261ba

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

core/src/main/java/org/everit/json/schema/regexp/JavaUtilRegexpFactory.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.everit.json.schema.regexp;
22

3+
import java.util.Objects;
34
import java.util.Optional;
45
import java.util.regex.Pattern;
56

@@ -20,6 +21,18 @@ class JavaUtilRegexp extends AbstractRegexp {
2021
}
2122
}
2223

24+
@Override public boolean equals(Object o) {
25+
if (this == o)
26+
return true;
27+
if (!(o instanceof JavaUtilRegexp))
28+
return false;
29+
JavaUtilRegexp that = (JavaUtilRegexp) o;
30+
return Objects.equals(pattern, that.pattern);
31+
}
32+
33+
@Override public int hashCode() {
34+
return Objects.hash(pattern);
35+
}
2336
}
2437

2538
public class JavaUtilRegexpFactory implements RegexpFactory {

core/src/main/java/org/everit/json/schema/regexp/RE2JRegexpFactory.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package org.everit.json.schema.regexp;
22

3+
import java.util.Objects;
34
import java.util.Optional;
45

56
import com.google.re2j.Pattern;
67

78
class RE2JRegexp extends AbstractRegexp {
89

9-
private Pattern pattern;
10+
private final Pattern pattern;
1011

1112
RE2JRegexp(String pattern) {
1213
super(pattern);
@@ -21,6 +22,18 @@ class RE2JRegexp extends AbstractRegexp {
2122
}
2223
}
2324

25+
@Override public boolean equals(Object o) {
26+
if (this == o)
27+
return true;
28+
if (!(o instanceof RE2JRegexp))
29+
return false;
30+
RE2JRegexp that = (RE2JRegexp) o;
31+
return Objects.equals(pattern, that.pattern);
32+
}
33+
34+
@Override public int hashCode() {
35+
return Objects.hash(pattern);
36+
}
2437
}
2538

2639
public class RE2JRegexpFactory implements RegexpFactory {

core/src/test/java/org/everit/json/schema/regexp/JavaUtilRegexpTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
import org.junit.Test;
99

10+
import java.util.regex.Pattern;
11+
12+
import nl.jqno.equalsverifier.EqualsVerifier;
13+
import nl.jqno.equalsverifier.Warning;
14+
1015
public class JavaUtilRegexpTest {
1116

1217
static final String PATTERN = "^aa.*b$";
@@ -30,4 +35,13 @@ public void asString() {
3035
assertEquals(PATTERN, createHandler().toString());
3136
}
3237

38+
@Test
39+
public void equalsVerifier() {
40+
EqualsVerifier.forClass(JavaUtilRegexp.class)
41+
.withPrefabValues(Pattern.class, Pattern.compile("red"), Pattern.compile("black"))
42+
.withIgnoredFields("asString")
43+
.suppress(Warning.STRICT_INHERITANCE)
44+
.verify();
45+
}
46+
3347
}

core/src/test/java/org/everit/json/schema/regexp/RE2JRegexpTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
import org.junit.Test;
99

10+
import com.google.re2j.Pattern;
11+
12+
import nl.jqno.equalsverifier.EqualsVerifier;
13+
import nl.jqno.equalsverifier.Warning;
14+
1015
public class RE2JRegexpTest {
1116

1217
static final String PATTERN = "^aa.*b$";
@@ -30,4 +35,13 @@ public void asString() {
3035
assertEquals(PATTERN, createHandler().toString());
3136
}
3237

38+
@Test
39+
public void equalsVerifier() {
40+
EqualsVerifier.forClass(RE2JRegexp.class)
41+
.withPrefabValues(Pattern.class, Pattern.compile("red"), Pattern.compile("black"))
42+
.withIgnoredFields("asString")
43+
.suppress(Warning.STRICT_INHERITANCE)
44+
.verify();
45+
}
46+
3347
}

0 commit comments

Comments
 (0)