http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/StringQueryResult.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/StringQueryResult.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/StringQueryResult.java new file mode 100644 index 0000000..c010bb1 --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/StringQueryResult.java @@ -0,0 +1,43 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation; + +import org.apache.nifi.expression.AttributeExpression.ResultType; + +public class StringQueryResult implements QueryResult<String> { + + private final String value; + + public StringQueryResult(final String value) { + this.value = value; + } + + @Override + public String getValue() { + return value; + } + + @Override + public ResultType getResultType() { + return ResultType.STRING; + } + + @Override + public String toString() { + return String.valueOf(getValue()); + } +}
http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/cast/BooleanCastEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/cast/BooleanCastEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/cast/BooleanCastEvaluator.java new file mode 100644 index 0000000..a88b72b --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/cast/BooleanCastEvaluator.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.cast; + +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.BooleanQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator; + +public class BooleanCastEvaluator extends BooleanEvaluator { + + private final StringEvaluator subjectEvaluator; + + public BooleanCastEvaluator(final StringEvaluator subjectEvaluator) { + this.subjectEvaluator = subjectEvaluator; + } + + @Override + public QueryResult<Boolean> evaluate(final Map<String, String> attributes) { + final QueryResult<String> result = subjectEvaluator.evaluate(attributes); + if (result.getValue() == null) { + return new BooleanQueryResult(null); + } + + return new BooleanQueryResult(Boolean.valueOf(result.getValue().trim())); + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subjectEvaluator; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/cast/DateCastEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/cast/DateCastEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/cast/DateCastEvaluator.java new file mode 100644 index 0000000..b6d758c --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/cast/DateCastEvaluator.java @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.cast; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.nifi.attribute.expression.language.evaluation.DateEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.DateQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult; +import org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException; +import org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException; +import org.apache.nifi.expression.AttributeExpression.ResultType; + +public class DateCastEvaluator extends DateEvaluator { + + public static final String DATE_TO_STRING_FORMAT = "EEE MMM dd HH:mm:ss zzz yyyy"; + public static final Pattern DATE_TO_STRING_PATTERN = Pattern.compile("(?:[a-zA-Z]{3} ){2}\\d{2} \\d{2}\\:\\d{2}\\:\\d{2} (?:.*?) \\d{4}"); + + public static final String ALTERNATE_FORMAT_WITHOUT_MILLIS = "yyyy/MM/dd HH:mm:ss"; + public static final String ALTERNATE_FORMAT_WITH_MILLIS = "yyyy/MM/dd HH:mm:ss.SSS"; + public static final Pattern ALTERNATE_PATTERN = Pattern.compile("\\d{4}/\\d{2}/\\d{2} \\d{2}\\:\\d{2}\\:\\d{2}(\\.\\d{3})?"); + + public static final Pattern NUMBER_PATTERN = Pattern.compile("\\d+"); + + private final Evaluator<?> subjectEvaluator; + + public DateCastEvaluator(final Evaluator<?> subjectEvaluator) { + if (subjectEvaluator.getResultType() == ResultType.BOOLEAN) { + throw new AttributeExpressionLanguageParsingException("Cannot implicitly convert Data Type " + subjectEvaluator.getResultType() + " to " + ResultType.DATE); + } + + this.subjectEvaluator = subjectEvaluator; + } + + @Override + public QueryResult<Date> evaluate(final Map<String, String> attributes) { + final QueryResult<?> result = subjectEvaluator.evaluate(attributes); + if (result.getValue() == null) { + return new DateQueryResult(null); + } + + switch (result.getResultType()) { + case DATE: + return (DateQueryResult) result; + case STRING: + final String value = ((StringQueryResult) result).getValue().trim(); + if (DATE_TO_STRING_PATTERN.matcher(value).matches()) { + final SimpleDateFormat sdf = new SimpleDateFormat(DATE_TO_STRING_FORMAT); + + try { + final Date date = sdf.parse(value); + return new DateQueryResult(date); + } catch (final ParseException pe) { + throw new AttributeExpressionLanguageException("Could not parse input as date", pe); + } + } else if (NUMBER_PATTERN.matcher(value).matches()) { + return new DateQueryResult(new Date(Long.valueOf(value))); + } else { + final Matcher altMatcher = ALTERNATE_PATTERN.matcher(value); + if (altMatcher.matches()) { + final String millisValue = altMatcher.group(1); + + final String format; + if (millisValue == null) { + format = ALTERNATE_FORMAT_WITHOUT_MILLIS; + } else { + format = ALTERNATE_FORMAT_WITH_MILLIS; + } + + final SimpleDateFormat sdf = new SimpleDateFormat(format); + + try { + final Date date = sdf.parse(value); + return new DateQueryResult(date); + } catch (final ParseException pe) { + throw new AttributeExpressionLanguageException("Could not parse input as date", pe); + } + } else { + throw new AttributeExpressionLanguageException("Could not implicitly convert input to DATE: " + value); + } + } + case NUMBER: + return new DateQueryResult(new Date((Long) result.getValue())); + default: + return new DateQueryResult(null); + } + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subjectEvaluator; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/cast/NumberCastEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/cast/NumberCastEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/cast/NumberCastEvaluator.java new file mode 100644 index 0000000..0447d48 --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/cast/NumberCastEvaluator.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.cast; + +import java.util.Map; +import java.util.regex.Pattern; + +import org.apache.nifi.attribute.expression.language.evaluation.DateQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.NumberEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.NumberQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult; +import org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException; +import org.apache.nifi.expression.AttributeExpression.ResultType; + +public class NumberCastEvaluator extends NumberEvaluator { + + private final Evaluator<?> subjectEvaluator; + private static final Pattern NUMBER_PATTERN = Pattern.compile("-?\\d+"); + + public NumberCastEvaluator(final Evaluator<?> subjectEvaluator) { + if (subjectEvaluator.getResultType() == ResultType.BOOLEAN) { + throw new AttributeExpressionLanguageParsingException("Cannot implicitly convert Data Type " + subjectEvaluator.getResultType() + " to " + ResultType.NUMBER); + } + this.subjectEvaluator = subjectEvaluator; + } + + @Override + public QueryResult<Long> evaluate(final Map<String, String> attributes) { + final QueryResult<?> result = subjectEvaluator.evaluate(attributes); + if (result.getValue() == null) { + return new NumberQueryResult(null); + } + + switch (result.getResultType()) { + case NUMBER: + return (NumberQueryResult) result; + case STRING: + final String trimmed = ((StringQueryResult) result).getValue().trim(); + if (NUMBER_PATTERN.matcher(trimmed).matches()) { + return new NumberQueryResult(Long.valueOf(trimmed)); + } else { + return new NumberQueryResult(null); + } + case DATE: + return new NumberQueryResult(((DateQueryResult) result).getValue().getTime()); + default: + return new NumberQueryResult(null); + } + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subjectEvaluator; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/cast/StringCastEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/cast/StringCastEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/cast/StringCastEvaluator.java new file mode 100644 index 0000000..1a3732a --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/cast/StringCastEvaluator.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.cast; + +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult; + +public class StringCastEvaluator extends StringEvaluator { + + private final Evaluator<?> subjectEvaluator; + + public StringCastEvaluator(final Evaluator<?> subjectEvaluator) { + this.subjectEvaluator = subjectEvaluator; + } + + @Override + public QueryResult<String> evaluate(final Map<String, String> attributes) { + final QueryResult<?> result = subjectEvaluator.evaluate(attributes); + if (result.getValue() == null) { + return new StringQueryResult(null); + } + + return new StringQueryResult(String.valueOf(result.getValue())); + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subjectEvaluator; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/AndEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/AndEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/AndEvaluator.java new file mode 100644 index 0000000..9ed8f06 --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/AndEvaluator.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.BooleanQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; + +public class AndEvaluator extends BooleanEvaluator { + + private final BooleanEvaluator subjectEvaluator; + private final BooleanEvaluator rhsEvaluator; + + public AndEvaluator(final BooleanEvaluator subjectEvaluator, final BooleanEvaluator rhsEvaluator) { + this.subjectEvaluator = subjectEvaluator; + this.rhsEvaluator = rhsEvaluator; + } + + @Override + public QueryResult<Boolean> evaluate(final Map<String, String> attributes) { + final QueryResult<Boolean> subjectValue = subjectEvaluator.evaluate(attributes); + if (subjectValue == null) { + return new BooleanQueryResult(null); + } + + if (Boolean.FALSE.equals(subjectValue.getValue())) { + return new BooleanQueryResult(false); + } + + final QueryResult<Boolean> rhsValue = rhsEvaluator.evaluate(attributes); + if (rhsValue == null) { + return new BooleanQueryResult(false); + } + + return new BooleanQueryResult(rhsValue.getValue()); + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subjectEvaluator; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/AppendEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/AppendEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/AppendEvaluator.java new file mode 100644 index 0000000..bd5e81a --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/AppendEvaluator.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult; + +public class AppendEvaluator extends StringEvaluator { + + private final StringEvaluator subject; + private final StringEvaluator appendEvaluator; + + public AppendEvaluator(final StringEvaluator subject, final StringEvaluator append) { + this.subject = subject; + this.appendEvaluator = append; + } + + @Override + public QueryResult<String> evaluate(final Map<String, String> attributes) { + final String subjectValue = subject.evaluate(attributes).getValue(); + final String appendValue = appendEvaluator.evaluate(attributes).getValue(); + + final String result = (subjectValue == null ? "" : subjectValue) + + (appendValue == null ? "" : appendValue); + return new StringQueryResult(result); + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subject; + } +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/AttributeEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/AttributeEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/AttributeEvaluator.java new file mode 100644 index 0000000..1b4b74e --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/AttributeEvaluator.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult; + +public class AttributeEvaluator extends StringEvaluator { + + private final StringEvaluator nameEvaluator; + + public AttributeEvaluator(final StringEvaluator nameEvaluator) { + this.nameEvaluator = nameEvaluator; + } + + @Override + public QueryResult<String> evaluate(final Map<String, String> attributes) { + final String nameValue = nameEvaluator.evaluate(attributes).getValue(); + final String attributeValue = attributes.get(nameValue); + return new StringQueryResult(attributeValue); + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return null; + } +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/ContainsEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/ContainsEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/ContainsEvaluator.java new file mode 100644 index 0000000..99802f6 --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/ContainsEvaluator.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.BooleanQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator; + +public class ContainsEvaluator extends BooleanEvaluator { + + private final StringEvaluator subject; + private final StringEvaluator search; + + public ContainsEvaluator(final StringEvaluator subject, final StringEvaluator search) { + this.subject = subject; + this.search = search; + } + + @Override + public QueryResult<Boolean> evaluate(final Map<String, String> attributes) { + final String subjectValue = subject.evaluate(attributes).getValue(); + if (subjectValue == null) { + return new BooleanQueryResult(false); + } + + final String searchString = search.evaluate(attributes).getValue(); + return new BooleanQueryResult(searchString == null ? false : subjectValue.contains(searchString)); + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subject; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/DateToNumberEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/DateToNumberEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/DateToNumberEvaluator.java new file mode 100644 index 0000000..0594c4a --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/DateToNumberEvaluator.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.util.Date; +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.DateEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.NumberEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.NumberQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; + +public class DateToNumberEvaluator extends NumberEvaluator { + + private final DateEvaluator subjectEvaluator; + + public DateToNumberEvaluator(final DateEvaluator subjectEvaluator) { + this.subjectEvaluator = subjectEvaluator; + } + + @Override + public QueryResult<Long> evaluate(final Map<String, String> attributes) { + final QueryResult<Date> subjectResult = subjectEvaluator.evaluate(attributes); + if (subjectResult.getValue() == null) { + return new NumberQueryResult(null); + } + + return new NumberQueryResult(subjectResult.getValue().getTime()); + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subjectEvaluator; + } +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/DivideEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/DivideEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/DivideEvaluator.java new file mode 100644 index 0000000..c7583cb --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/DivideEvaluator.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.NumberEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.NumberQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; + +public class DivideEvaluator extends NumberEvaluator { + + private final NumberEvaluator subject; + private final NumberEvaluator divideValue; + + public DivideEvaluator(final NumberEvaluator subject, final NumberEvaluator divideValue) { + this.subject = subject; + this.divideValue = divideValue; + } + + @Override + public QueryResult<Long> evaluate(final Map<String, String> attributes) { + final Long subjectValue = subject.evaluate(attributes).getValue(); + if (subjectValue == null) { + return new NumberQueryResult(null); + } + + final Long divide = divideValue.evaluate(attributes).getValue(); + if (divide == null) { + return new NumberQueryResult(null); + } + + final long result = subjectValue / divide; + return new NumberQueryResult(result); + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subject; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/EndsWithEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/EndsWithEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/EndsWithEvaluator.java new file mode 100644 index 0000000..c31c2b1 --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/EndsWithEvaluator.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.BooleanQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator; + +public class EndsWithEvaluator extends BooleanEvaluator { + + private final StringEvaluator subject; + private final StringEvaluator search; + + public EndsWithEvaluator(final StringEvaluator subject, final StringEvaluator search) { + this.subject = subject; + this.search = search; + } + + @Override + public QueryResult<Boolean> evaluate(final Map<String, String> attributes) { + final String subjectValue = subject.evaluate(attributes).getValue(); + if (subjectValue == null) { + return new BooleanQueryResult(false); + } + + final String searchString = search.evaluate(attributes).getValue(); + return new BooleanQueryResult(searchString == null ? false : subjectValue.endsWith(searchString)); + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subject; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/EqualsEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/EqualsEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/EqualsEvaluator.java new file mode 100644 index 0000000..ec67b0f --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/EqualsEvaluator.java @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.util.Date; +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.BooleanQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; +import org.apache.nifi.expression.AttributeExpression.ResultType; + +public class EqualsEvaluator extends BooleanEvaluator { + + private final Evaluator<?> subject; + private final Evaluator<?> compareTo; + + public EqualsEvaluator(final Evaluator<?> subject, final Evaluator<?> compareTo) { + this.subject = subject; + this.compareTo = compareTo; + } + + @Override + public QueryResult<Boolean> evaluate(final Map<String, String> attributes) { + final Object a = subject.evaluate(attributes).getValue(); + if (a == null) { + return new BooleanQueryResult(false); + } + + final Object b = compareTo.evaluate(attributes).getValue(); + if (b == null) { + return new BooleanQueryResult(false); + } + + if (subject.getResultType() == compareTo.getResultType()) { + return new BooleanQueryResult(a.equals(b)); + } + + final String normalizedSubjectValue = normalizeValue(subject.getResultType(), a); + if (normalizedSubjectValue == null) { + return new BooleanQueryResult(false); + } + + final String normalizedCompareToValue = normalizeValue(compareTo.getResultType(), b); + if (normalizedCompareToValue == null) { + return new BooleanQueryResult(false); + } + + return new BooleanQueryResult(normalizedSubjectValue.equals(normalizedCompareToValue)); + } + + private String normalizeValue(final ResultType type, final Object value) { + if (value == null) { + return null; + } + + switch (type) { + case STRING: + return (String) value; + case DATE: + return String.valueOf(((Date) value).getTime()); + case BOOLEAN: + case NUMBER: + default: + return String.valueOf(value); + } + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subject; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/EqualsIgnoreCaseEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/EqualsIgnoreCaseEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/EqualsIgnoreCaseEvaluator.java new file mode 100644 index 0000000..344395b --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/EqualsIgnoreCaseEvaluator.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.BooleanQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; + +public class EqualsIgnoreCaseEvaluator extends BooleanEvaluator { + + private final Evaluator<?> subject; + private final Evaluator<?> compareTo; + + public EqualsIgnoreCaseEvaluator(final Evaluator<?> subject, final Evaluator<?> compareTo) { + this.subject = subject; + this.compareTo = compareTo; + } + + @Override + public QueryResult<Boolean> evaluate(final Map<String, String> attributes) { + final Object a = subject.evaluate(attributes).getValue(); + if (a == null) { + return new BooleanQueryResult(false); + } + + final Object b = compareTo.evaluate(attributes).getValue(); + if (b == null) { + return new BooleanQueryResult(false); + } + + if (a instanceof String && b instanceof String) { + return new BooleanQueryResult(((String) a).equalsIgnoreCase((String) b)); + } + + return new BooleanQueryResult(a.equals(b)); + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subject; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/FindEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/FindEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/FindEvaluator.java new file mode 100644 index 0000000..b377c4a --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/FindEvaluator.java @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.util.Map; +import java.util.regex.Pattern; + +import org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.BooleanQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.literals.StringLiteralEvaluator; + +public class FindEvaluator extends BooleanEvaluator { + + private final StringEvaluator subject; + private final StringEvaluator search; + + private final Pattern compiledPattern; + + public FindEvaluator(final StringEvaluator subject, final StringEvaluator search) { + this.subject = subject; + this.search = search; + + // if the search string is a literal, we don't need to evaluate it each time; we can just + // pre-compile it. Otherwise, it must be compiled every time. + if (search instanceof StringLiteralEvaluator) { + this.compiledPattern = Pattern.compile(search.evaluate(null).getValue()); + } else { + this.compiledPattern = null; + } + } + + @Override + public QueryResult<Boolean> evaluate(final Map<String, String> attributes) { + final String subjectValue = subject.evaluate(attributes).getValue(); + if (subjectValue == null) { + return new BooleanQueryResult(false); + } + final Pattern pattern; + if (compiledPattern == null) { + pattern = Pattern.compile(search.evaluate(attributes).getValue()); + } else { + pattern = compiledPattern; + } + + final boolean found = pattern.matcher(subjectValue).find(); + + return new BooleanQueryResult(found); + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subject; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/FormatEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/FormatEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/FormatEvaluator.java new file mode 100644 index 0000000..ccb1a89 --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/FormatEvaluator.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.DateEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult; + +public class FormatEvaluator extends StringEvaluator { + + private final DateEvaluator subject; + private final StringEvaluator format; + + public FormatEvaluator(final DateEvaluator subject, final StringEvaluator format) { + this.subject = subject; + this.format = format; + } + + @Override + public QueryResult<String> evaluate(final Map<String, String> attributes) { + final Date subjectValue = subject.evaluate(attributes).getValue(); + if (subjectValue == null) { + return new StringQueryResult(null); + } + + final QueryResult<String> formatResult = format.evaluate(attributes); + final String format = formatResult.getValue(); + if (format == null) { + return null; + } + + return new StringQueryResult(new SimpleDateFormat(format).format(subjectValue)); + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subject; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/GreaterThanEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/GreaterThanEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/GreaterThanEvaluator.java new file mode 100644 index 0000000..6c712bb --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/GreaterThanEvaluator.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.BooleanQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.NumberEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; + +public class GreaterThanEvaluator extends BooleanEvaluator { + + private final NumberEvaluator subject; + private final NumberEvaluator comparison; + + public GreaterThanEvaluator(final NumberEvaluator subject, final NumberEvaluator comparison) { + this.subject = subject; + this.comparison = comparison; + } + + @Override + public QueryResult<Boolean> evaluate(final Map<String, String> attributes) { + final Long subjectValue = subject.evaluate(attributes).getValue(); + if (subjectValue == null) { + return new BooleanQueryResult(false); + } + + final Long comparisonValue = comparison.evaluate(attributes).getValue(); + if (comparisonValue == null) { + return new BooleanQueryResult(false); + } + + return new BooleanQueryResult(subjectValue > comparisonValue); + } + + ; + + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subject; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/GreaterThanOrEqualEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/GreaterThanOrEqualEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/GreaterThanOrEqualEvaluator.java new file mode 100644 index 0000000..98951f2 --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/GreaterThanOrEqualEvaluator.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.BooleanQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.NumberEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; + +public class GreaterThanOrEqualEvaluator extends BooleanEvaluator { + + private final NumberEvaluator subject; + private final NumberEvaluator comparison; + + public GreaterThanOrEqualEvaluator(final NumberEvaluator subject, final NumberEvaluator comparison) { + this.subject = subject; + this.comparison = comparison; + } + + @Override + public QueryResult<Boolean> evaluate(final Map<String, String> attributes) { + final Long subjectValue = subject.evaluate(attributes).getValue(); + if (subjectValue == null) { + return new BooleanQueryResult(false); + } + + final Long comparisonValue = comparison.evaluate(attributes).getValue(); + if (comparisonValue == null) { + return new BooleanQueryResult(false); + } + + return new BooleanQueryResult(subjectValue >= comparisonValue); + } + + ; + + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subject; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/HostnameEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/HostnameEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/HostnameEvaluator.java new file mode 100644 index 0000000..c0b1134 --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/HostnameEvaluator.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult; + +public class HostnameEvaluator extends StringEvaluator { + + private final StringQueryResult hostname; + + public HostnameEvaluator(final boolean preferFQDN) throws UnknownHostException { + String address; + + if (preferFQDN) { + try { + address = InetAddress.getLocalHost().getCanonicalHostName(); + } catch (final Exception e) { + address = InetAddress.getLocalHost().getHostName(); + } + } else { + address = InetAddress.getLocalHost().getHostName(); + } + + hostname = new StringQueryResult(address); + } + + @Override + public QueryResult<String> evaluate(final Map<String, String> attributes) { + return hostname; + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return null; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/IPEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/IPEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/IPEvaluator.java new file mode 100644 index 0000000..21f9fe8 --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/IPEvaluator.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.StringQueryResult; + +public class IPEvaluator extends StringEvaluator { + + private final StringQueryResult ipAddress; + + public IPEvaluator() throws UnknownHostException { + ipAddress = new StringQueryResult(InetAddress.getLocalHost().getHostAddress()); + } + + @Override + public QueryResult<String> evaluate(final Map<String, String> attributes) { + return ipAddress; + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return null; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/IndexOfEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/IndexOfEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/IndexOfEvaluator.java new file mode 100644 index 0000000..80422cb --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/IndexOfEvaluator.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.NumberEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.NumberQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator; + +public class IndexOfEvaluator extends NumberEvaluator { + + private final StringEvaluator subject; + private final StringEvaluator indexEvaluator; + + public IndexOfEvaluator(final StringEvaluator subject, final StringEvaluator indexEvaluator) { + this.subject = subject; + this.indexEvaluator = indexEvaluator; + } + + @Override + public QueryResult<Long> evaluate(final Map<String, String> attributes) { + final String subjectValue = subject.evaluate(attributes).getValue(); + if (subjectValue == null) { + return new NumberQueryResult(-1L); + } + final String indexEvalValue = indexEvaluator.evaluate(attributes).getValue(); + + return new NumberQueryResult((long) subjectValue.indexOf(indexEvalValue)); + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subject; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/IsNullEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/IsNullEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/IsNullEvaluator.java new file mode 100644 index 0000000..a9d678c --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/IsNullEvaluator.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.BooleanQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; + +public class IsNullEvaluator extends BooleanEvaluator { + + private final Evaluator<?> subject; + + public IsNullEvaluator(final Evaluator<?> subject) { + this.subject = subject; + } + + @Override + public QueryResult<Boolean> evaluate(final Map<String, String> attributes) { + final Object subjectValue = subject.evaluate(attributes).getValue(); + return new BooleanQueryResult(subjectValue == null); + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subject; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/LastIndexOfEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/LastIndexOfEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/LastIndexOfEvaluator.java new file mode 100644 index 0000000..2b67093 --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/LastIndexOfEvaluator.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.NumberEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.NumberQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator; + +public class LastIndexOfEvaluator extends NumberEvaluator { + + private final StringEvaluator subject; + private final StringEvaluator indexEvaluator; + + public LastIndexOfEvaluator(final StringEvaluator subject, final StringEvaluator indexEvaluator) { + this.subject = subject; + this.indexEvaluator = indexEvaluator; + } + + @Override + public QueryResult<Long> evaluate(final Map<String, String> attributes) { + final String subjectValue = subject.evaluate(attributes).getValue(); + if (subjectValue == null) { + return new NumberQueryResult(-1L); + } + final String indexEvalValue = indexEvaluator.evaluate(attributes).getValue(); + + return new NumberQueryResult((long) subjectValue.lastIndexOf(indexEvalValue)); + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subject; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/LengthEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/LengthEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/LengthEvaluator.java new file mode 100644 index 0000000..dec1f8f --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/LengthEvaluator.java @@ -0,0 +1,46 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.NumberEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.NumberQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator; + +public class LengthEvaluator extends NumberEvaluator { + + private final StringEvaluator subject; + + public LengthEvaluator(final StringEvaluator subject) { + this.subject = subject; + } + + @Override + public QueryResult<Long> evaluate(final Map<String, String> attributes) { + final String subjectValue = subject.evaluate(attributes).getValue(); + return new NumberQueryResult((long) ((subjectValue == null) ? 0 : subjectValue.length())); + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subject; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/LessThanEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/LessThanEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/LessThanEvaluator.java new file mode 100644 index 0000000..9a58910 --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/LessThanEvaluator.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.BooleanQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.NumberEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; + +public class LessThanEvaluator extends BooleanEvaluator { + + private final NumberEvaluator subject; + private final NumberEvaluator comparison; + + public LessThanEvaluator(final NumberEvaluator subject, final NumberEvaluator comparison) { + this.subject = subject; + this.comparison = comparison; + } + + @Override + public QueryResult<Boolean> evaluate(final Map<String, String> attributes) { + final Long subjectValue = subject.evaluate(attributes).getValue(); + if (subjectValue == null) { + return new BooleanQueryResult(false); + } + + final Long comparisonValue = comparison.evaluate(attributes).getValue(); + if (comparisonValue == null) { + return new BooleanQueryResult(false); + } + + return new BooleanQueryResult(subjectValue < comparisonValue); + } + + ; + + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subject; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/LessThanOrEqualEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/LessThanOrEqualEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/LessThanOrEqualEvaluator.java new file mode 100644 index 0000000..10f3f6a --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/LessThanOrEqualEvaluator.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.BooleanQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.NumberEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; + +public class LessThanOrEqualEvaluator extends BooleanEvaluator { + + private final NumberEvaluator subject; + private final NumberEvaluator comparison; + + public LessThanOrEqualEvaluator(final NumberEvaluator subject, final NumberEvaluator comparison) { + this.subject = subject; + this.comparison = comparison; + } + + @Override + public QueryResult<Boolean> evaluate(final Map<String, String> attributes) { + final Long subjectValue = subject.evaluate(attributes).getValue(); + if (subjectValue == null) { + return new BooleanQueryResult(false); + } + + final Long comparisonValue = comparison.evaluate(attributes).getValue(); + if (comparisonValue == null) { + return new BooleanQueryResult(false); + } + + return new BooleanQueryResult(subjectValue <= comparisonValue); + } + + ; + + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subject; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/MatchesEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/MatchesEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/MatchesEvaluator.java new file mode 100644 index 0000000..1bb86a5 --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/MatchesEvaluator.java @@ -0,0 +1,71 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.util.Map; +import java.util.regex.Pattern; + +import org.apache.nifi.attribute.expression.language.evaluation.BooleanEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.BooleanQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.StringEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.literals.StringLiteralEvaluator; + +public class MatchesEvaluator extends BooleanEvaluator { + + private final StringEvaluator subject; + private final StringEvaluator search; + + private final Pattern compiledPattern; + + public MatchesEvaluator(final StringEvaluator subject, final StringEvaluator search) { + this.subject = subject; + this.search = search; + + // if the search string is a literal, we don't need to evaluate it each time; we can just + // pre-compile it. Otherwise, it must be compiled every time. + if (search instanceof StringLiteralEvaluator) { + this.compiledPattern = Pattern.compile(search.evaluate(null).getValue()); + } else { + this.compiledPattern = null; + } + } + + @Override + public QueryResult<Boolean> evaluate(final Map<String, String> attributes) { + final String subjectValue = subject.evaluate(attributes).getValue(); + if (subjectValue == null) { + return new BooleanQueryResult(false); + } + final Pattern pattern; + if (compiledPattern == null) { + pattern = Pattern.compile(search.evaluate(attributes).getValue()); + } else { + pattern = compiledPattern; + } + + final boolean matches = pattern.matcher(subjectValue).matches(); + return new BooleanQueryResult(matches); + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subject; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/4d998c12/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/MinusEvaluator.java ---------------------------------------------------------------------- diff --git a/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/MinusEvaluator.java b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/MinusEvaluator.java new file mode 100644 index 0000000..0665979 --- /dev/null +++ b/commons/nifi-expression-language/src/main/java/org/apache/nifi/attribute/expression/language/evaluation/functions/MinusEvaluator.java @@ -0,0 +1,57 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.attribute.expression.language.evaluation.functions; + +import java.util.Map; + +import org.apache.nifi.attribute.expression.language.evaluation.Evaluator; +import org.apache.nifi.attribute.expression.language.evaluation.NumberEvaluator; +import org.apache.nifi.attribute.expression.language.evaluation.NumberQueryResult; +import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; + +public class MinusEvaluator extends NumberEvaluator { + + private final NumberEvaluator subject; + private final NumberEvaluator minusValue; + + public MinusEvaluator(final NumberEvaluator subject, final NumberEvaluator minusValue) { + this.subject = subject; + this.minusValue = minusValue; + } + + @Override + public QueryResult<Long> evaluate(final Map<String, String> attributes) { + final Long subjectValue = subject.evaluate(attributes).getValue(); + if (subjectValue == null) { + return new NumberQueryResult(null); + } + + final Long minus = minusValue.evaluate(attributes).getValue(); + if (minus == null) { + return new NumberQueryResult(null); + } + + final long result = subjectValue - minus; + return new NumberQueryResult(result); + } + + @Override + public Evaluator<?> getSubjectEvaluator() { + return subject; + } + +}