add equals and hashcode to GsubExpression

This commit is contained in:
javanna 2015-11-11 10:17:32 +01:00 committed by Luca Cavanna
parent 2b31f4fff7
commit c12c9e6e29
2 changed files with 24 additions and 7 deletions

View File

@ -19,6 +19,7 @@
package org.elasticsearch.ingest.processor.mutate;
import java.util.Objects;
import java.util.regex.Pattern;
/**
@ -31,9 +32,9 @@ public class GsubExpression {
private final String replacement;
public GsubExpression(String fieldName, Pattern pattern, String replacement) {
this.fieldName = fieldName;
this.pattern = pattern;
this.replacement = replacement;
this.fieldName = Objects.requireNonNull(fieldName);
this.pattern = Objects.requireNonNull(pattern);
this.replacement = Objects.requireNonNull(replacement);
}
public String getFieldName() {
@ -47,4 +48,23 @@ public class GsubExpression {
public String getReplacement() {
return replacement;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
GsubExpression that = (GsubExpression) o;
return Objects.equals(fieldName, that.fieldName) &&
Objects.equals(pattern.pattern(), that.pattern.pattern()) &&
Objects.equals(replacement, that.replacement);
}
@Override
public int hashCode() {
return Objects.hash(fieldName, pattern, replacement);
}
}

View File

@ -205,10 +205,7 @@ public class MutateProcessorFactoryTests extends ESTestCase {
MutateProcessor processor = factory.create(config);
assertThat(processor.getGsub().size(), equalTo(1));
GsubExpression gsubExpression = processor.getGsub().get(0);
assertThat(gsubExpression.getFieldName(), equalTo("foo"));
assertThat(gsubExpression.getPattern().pattern(), equalTo(Pattern.compile("\\s.*e\\s").pattern()));
assertThat(gsubExpression.getReplacement(), equalTo("<word_ending_with_e>"));
assertThat(processor.getGsub().get(0), equalTo(new GsubExpression("foo", Pattern.compile("\\s.*e\\s"), "<word_ending_with_e>")));
}
public void testCreateGsubPatternInvalidFormat() throws Exception {