|
| 1 | +/* |
| 2 | + * Copyright 2017 Crown Copyright |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package stroom.query.language.functions; |
| 18 | + |
| 19 | +import stroom.query.language.functions.ref.StoredValues; |
| 20 | +import stroom.query.language.token.Param; |
| 21 | + |
| 22 | +import java.text.ParseException; |
| 23 | +import java.util.function.Supplier; |
| 24 | + |
| 25 | +@SuppressWarnings("unused") //Used by FunctionFactory |
| 26 | +@FunctionDef( |
| 27 | + name = Contains.NAME, |
| 28 | + commonCategory = FunctionCategory.STRING, |
| 29 | + commonReturnType = ValBoolean.class, |
| 30 | + commonReturnDescription = "True if string contains substring", |
| 31 | + signatures = @FunctionSignature( |
| 32 | + description = "Tests if inputString contains subString.", |
| 33 | + args = { |
| 34 | + @FunctionArg( |
| 35 | + name = "inputString", |
| 36 | + description = "The string to find subString in.", |
| 37 | + argType = ValString.class), |
| 38 | + @FunctionArg( |
| 39 | + name = "subString", |
| 40 | + description = "The string to find in inputString.", |
| 41 | + argType = ValString.class) |
| 42 | + })) |
| 43 | +class Contains extends AbstractManyChildFunction { |
| 44 | + |
| 45 | + static final String NAME = "contains"; |
| 46 | + private Generator gen; |
| 47 | + private boolean simple; |
| 48 | + |
| 49 | + public Contains(final String name) { |
| 50 | + super(name, 2, 2); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void setParams(final Param[] params) throws ParseException { |
| 55 | + super.setParams(params); |
| 56 | + |
| 57 | + // See if this is a static computation. |
| 58 | + simple = true; |
| 59 | + for (Param param : params) { |
| 60 | + if (!(param instanceof Val)) { |
| 61 | + simple = false; |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (simple) { |
| 67 | + // Static computation. |
| 68 | + final String string = params[0].toString(); |
| 69 | + final String substring = params[1].toString(); |
| 70 | + final boolean result = string.contains(substring); |
| 71 | + gen = new StaticValueFunction(ValBoolean.create(result)).createGenerator(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public Generator createGenerator() { |
| 77 | + if (gen != null) { |
| 78 | + return gen; |
| 79 | + } |
| 80 | + return super.createGenerator(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected Generator createGenerator(final Generator[] childGenerators) { |
| 85 | + return new Gen(childGenerators); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public boolean hasAggregate() { |
| 90 | + if (simple) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + return super.hasAggregate(); |
| 94 | + } |
| 95 | + |
| 96 | + private static final class Gen extends AbstractManyChildGenerator { |
| 97 | + |
| 98 | + Gen(final Generator[] childGenerators) { |
| 99 | + super(childGenerators); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public Val eval(final StoredValues storedValues, final Supplier<ChildData> childDataSupplier) { |
| 104 | + final Val val = childGenerators[0].eval(storedValues, childDataSupplier); |
| 105 | + if (!val.type().isValue()) { |
| 106 | + return val; |
| 107 | + } |
| 108 | + final Val valSubString = childGenerators[1].eval(storedValues, childDataSupplier); |
| 109 | + if (!valSubString.type().isValue()) { |
| 110 | + return ValErr.wrap(valSubString); |
| 111 | + } |
| 112 | + |
| 113 | + try { |
| 114 | + final String string = val.toString(); |
| 115 | + final String substring = valSubString.toString(); |
| 116 | + return ValBoolean.create(string.contains(substring)); |
| 117 | + |
| 118 | + } catch (final RuntimeException e) { |
| 119 | + return ValErr.create(e.getMessage()); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments