Skip to content

Commit

Permalink
Keep the generics
Browse files Browse the repository at this point in the history
  • Loading branch information
martijnvg committed Mar 28, 2019
1 parent d8d9ab6 commit c3746d8
Show file tree
Hide file tree
Showing 16 changed files with 36 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@
/**
* Base class for processors that manipulate source strings and require a single "fields" array config value, which
* holds a list of field names in string format.
*
* @param <T> The resultant type for the target field
*/
abstract class AbstractStringProcessor extends AbstractProcessor {
abstract class AbstractStringProcessor<T> extends AbstractProcessor {
private final String field;
private final boolean ignoreMissing;
private final String targetField;
Expand Down Expand Up @@ -68,7 +70,7 @@ public final IngestDocument execute(IngestDocument document) {
return document;
}

protected abstract Object process(String value);
protected abstract T process(String value);

abstract static class Factory implements Processor.Factory {
final String processorType;
Expand All @@ -78,16 +80,16 @@ protected Factory(String processorType) {
}

@Override
public AbstractStringProcessor create(Map<String, Processor.Factory> registry, String tag,
Map<String, Object> config) throws Exception {
public AbstractStringProcessor<?> create(Map<String, Processor.Factory> registry, String tag,
Map<String, Object> config) throws Exception {
String field = ConfigurationUtils.readStringProperty(processorType, tag, config, "field");
boolean ignoreMissing = ConfigurationUtils.readBooleanProperty(processorType, tag, config, "ignore_missing", false);
String targetField = ConfigurationUtils.readStringProperty(processorType, tag, config, "target_field", field);

return newProcessor(tag, config, field, ignoreMissing, targetField);
}

protected abstract AbstractStringProcessor newProcessor(String processorTag, Map<String, Object> config, String field,
boolean ignoreMissing, String targetField);
protected abstract AbstractStringProcessor<?> newProcessor(String processorTag, Map<String, Object> config, String field,
boolean ignoreMissing, String targetField);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* Processor that converts the content of string fields to the byte value.
* Throws exception is the field is not of type string or can not convert to the numeric byte value
*/
public final class BytesProcessor extends AbstractStringProcessor {
public final class BytesProcessor extends AbstractStringProcessor<Long> {

public static final String TYPE = "bytes";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* Processor that allows to search for patterns in field content and replace them with corresponding string replacement.
* Support fields of string type only, throws exception if a field is of a different type.
*/
public final class GsubProcessor extends AbstractStringProcessor {
public final class GsubProcessor extends AbstractStringProcessor<String> {

public static final String TYPE = "gsub";

Expand Down Expand Up @@ -67,8 +67,8 @@ public Factory() {
}

@Override
protected AbstractStringProcessor newProcessor(String processorTag, Map<String, Object> config, String field,
boolean ignoreMissing, String targetField) {
protected GsubProcessor newProcessor(String processorTag, Map<String, Object> config, String field,
boolean ignoreMissing, String targetField) {
String pattern = readStringProperty(TYPE, processorTag, config, "pattern");
String replacement = readStringProperty(TYPE, processorTag, config, "replacement");
Pattern searchPattern;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* Throws exception is the field is not of type string.
*/

public final class LowercaseProcessor extends AbstractStringProcessor {
public final class LowercaseProcessor extends AbstractStringProcessor<String> {

public static final String TYPE = "lowercase";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* Processor that trims the content of string fields.
* Throws exception is the field is not of type string.
*/
public final class TrimProcessor extends AbstractStringProcessor {
public final class TrimProcessor extends AbstractStringProcessor<String> {

public static final String TYPE = "trim";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/**
* Processor that URL-decodes a string
*/
public final class URLDecodeProcessor extends AbstractStringProcessor {
public final class URLDecodeProcessor extends AbstractStringProcessor<String> {

public static final String TYPE = "urldecode";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* Processor that converts the content of string fields to uppercase.
* Throws exception is the field is not of type string.
*/
public final class UppercaseProcessor extends AbstractStringProcessor {
public final class UppercaseProcessor extends AbstractStringProcessor<String> {

public static final String TYPE = "uppercase";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected Map<String, Object> modifyConfig(Map<String, Object> config) {
return config;
}

protected void assertProcessor(AbstractStringProcessor processor) {}
protected void assertProcessor(AbstractStringProcessor<?> processor) {}

public void testCreate() throws Exception {
AbstractStringProcessor.Factory factory = newFactory();
Expand All @@ -47,7 +47,7 @@ public void testCreate() throws Exception {
Map<String, Object> config = new HashMap<>();
config.put("field", fieldName);

AbstractStringProcessor processor = factory.create(null, processorTag, modifyConfig(config));
AbstractStringProcessor<?> processor = factory.create(null, processorTag, modifyConfig(config));
assertThat(processor.getTag(), equalTo(processorTag));
assertThat(processor.getField(), equalTo(fieldName));
assertThat(processor.isIgnoreMissing(), is(false));
Expand All @@ -64,7 +64,7 @@ public void testCreateWithIgnoreMissing() throws Exception {
config.put("field", fieldName);
config.put("ignore_missing", true);

AbstractStringProcessor processor = factory.create(null, processorTag, modifyConfig(config));
AbstractStringProcessor<?> processor = factory.create(null, processorTag, modifyConfig(config));
assertThat(processor.getTag(), equalTo(processorTag));
assertThat(processor.getField(), equalTo(fieldName));
assertThat(processor.isIgnoreMissing(), is(true));
Expand All @@ -82,7 +82,7 @@ public void testCreateWithTargetField() throws Exception {
config.put("field", fieldName);
config.put("target_field", targetFieldName);

AbstractStringProcessor processor = factory.create(null, processorTag, modifyConfig(config));
AbstractStringProcessor<?> processor = factory.create(null, processorTag, modifyConfig(config));
assertThat(processor.getTag(), equalTo(processorTag));
assertThat(processor.getField(), equalTo(fieldName));
assertThat(processor.isIgnoreMissing(), is(false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;

public abstract class AbstractStringProcessorTestCase extends ESTestCase {
public abstract class AbstractStringProcessorTestCase<T> extends ESTestCase {

protected abstract AbstractStringProcessor newProcessor(String field, boolean ignoreMissing, String targetField);
protected abstract AbstractStringProcessor<T> newProcessor(String field, boolean ignoreMissing, String targetField);

protected String modifyInput(String input) {
return input;
}

protected abstract Object expectedResult(String input);
protected abstract T expectedResult(String input);

protected Class<?> expectedResultType(){
return String.class; // most results types are Strings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@

import static org.hamcrest.Matchers.equalTo;

public class BytesProcessorTests extends AbstractStringProcessorTestCase {
public class BytesProcessorTests extends AbstractStringProcessorTestCase<Long> {

private String modifiedInput;

@Override
protected AbstractStringProcessor newProcessor(String field, boolean ignoreMissing, String targetField) {
protected AbstractStringProcessor<Long> newProcessor(String field, boolean ignoreMissing, String targetField) {
return new BytesProcessor(randomAlphaOfLength(10), field, ignoreMissing, targetField);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected Map<String, Object> modifyConfig(Map<String, Object> config) {
}

@Override
protected void assertProcessor(AbstractStringProcessor processor) {
protected void assertProcessor(AbstractStringProcessor<?> processor) {
GsubProcessor gsubProcessor = (GsubProcessor) processor;
assertThat(gsubProcessor.getPattern().toString(), equalTo("\\."));
assertThat(gsubProcessor.getReplacement(), equalTo("-"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

import java.util.regex.Pattern;

public class GsubProcessorTests extends AbstractStringProcessorTestCase {
public class GsubProcessorTests extends AbstractStringProcessorTestCase<String> {

@Override
protected AbstractStringProcessor newProcessor(String field, boolean ignoreMissing, String targetField) {
protected AbstractStringProcessor<String> newProcessor(String field, boolean ignoreMissing, String targetField) {
return new GsubProcessor(randomAlphaOfLength(10), field, Pattern.compile("\\."), "-", ignoreMissing, targetField);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

import java.util.Locale;

public class LowercaseProcessorTests extends AbstractStringProcessorTestCase {
public class LowercaseProcessorTests extends AbstractStringProcessorTestCase<String> {
@Override
protected AbstractStringProcessor newProcessor(String field, boolean ignoreMissing, String targetField) {
protected AbstractStringProcessor<String> newProcessor(String field, boolean ignoreMissing, String targetField) {
return new LowercaseProcessor(randomAlphaOfLength(10), field, ignoreMissing, targetField);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

package org.elasticsearch.ingest.common;

public class TrimProcessorTests extends AbstractStringProcessorTestCase {
public class TrimProcessorTests extends AbstractStringProcessorTestCase<String> {

@Override
protected AbstractStringProcessor newProcessor(String field, boolean ignoreMissing, String targetField) {
protected AbstractStringProcessor<String> newProcessor(String field, boolean ignoreMissing, String targetField) {
return new TrimProcessor(randomAlphaOfLength(10), field, ignoreMissing, targetField);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class URLDecodeProcessorTests extends AbstractStringProcessorTestCase {
public class URLDecodeProcessorTests extends AbstractStringProcessorTestCase<String> {
@Override
protected String modifyInput(String input) {
return "Hello%20G%C3%BCnter" + input;
}

@Override
protected AbstractStringProcessor newProcessor(String field, boolean ignoreMissing, String targetField) {
protected AbstractStringProcessor<String> newProcessor(String field, boolean ignoreMissing, String targetField) {
return new URLDecodeProcessor(randomAlphaOfLength(10), field, ignoreMissing, targetField);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@

import java.util.Locale;

public class UppercaseProcessorTests extends AbstractStringProcessorTestCase {
public class UppercaseProcessorTests extends AbstractStringProcessorTestCase<String> {

@Override
protected AbstractStringProcessor newProcessor(String field, boolean ignoreMissing, String targetField) {
protected AbstractStringProcessor<String> newProcessor(String field, boolean ignoreMissing, String targetField) {
return new UppercaseProcessor(randomAlphaOfLength(10), field, ignoreMissing, targetField);
}

Expand Down

0 comments on commit c3746d8

Please sign in to comment.